-
Notifications
You must be signed in to change notification settings - Fork 0
/
csb.py
154 lines (140 loc) · 3.34 KB
/
csb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def add(a, b):
return a+b
def sub(a, b):
return a-b
def mul(a, b):
return a*b
def div(a, b):
return a//b
def mod(a, b):
return a%b
instruction2 = {
'ADD':add,
'SUB':sub,
'MUL':mul,
'DIV':div,
'MOD':mod,
}
def if_f(s):
return pop(s) != 0
def pop(s):
return s.pop()
def dup(s):
s.append(s[-1])
def swp(s):
s[-1],s[-2] = s[-2],s[-1]
def rot(s):
s[-3],s[-2] = s[-2],s[-3]
swp(s)
def ovr(s):
s.append(s[-2])
def pos(s):
s.append(pop(s) >= 0 and 1 or 0)
def not_f(s):
s.append(pop(s) == 0 and 1 or 0)
def out(s):
print(pop(s))
instruction1 = {
'POP':pop,
'DUP':dup,
'SWP':swp,
'ROT':rot,
'OVR':ovr,
'POS':pos,
'NOT':not_f,
'OUT':out,
}
functions = {}
def_func = ''
stack_data = []
def extract_if(i, instructions, data, is_if):
l = len(instructions)
if_count = 0 if is_if else 1
while i < l:
instruction = instructions[i]
i += 1
if instruction == 'IF':
if_count += 1
if instruction == 'FI':
if_count -= 1
if if_count <= 0:
if is_if:
i -= 1
break
if instruction != 'ELS':
data.append(instruction)
else:
if is_if:
i -= 1
break
return i
def exec_if(s, i, instructions):
f = if_f(s)
if_l = []
else_l = []
l = len(instructions)
j = extract_if(i+1, instructions, if_l, True)
def had_els(k):
# return False
# if f: return False
while k < l:
if instructions[k] == 'ELS':
return True
k += 1
return False
if had_els(j):
j = extract_if(j+1, instructions, else_l, False)
exec_l = if_l if f else else_l
process_nest(exec_l, s)
return j
def process_nest(instructions, s):
j = 0
while j < len(instructions):
ins = instructions[j]
if ins == 'IF':
j = exec_if(s, j, instructions)
else:
process(ins, s)
j += 1
def process(instruction, s=[]):
if instruction.lstrip('-').isdigit():
s.append(int(instruction))
elif instruction in instruction2.keys():
a = s.pop()
b = s.pop()
s.append(instruction2[instruction](b, a))
elif instruction in instruction1.keys():
instruction1[instruction](s)
elif instruction in functions:
functions_ins = functions[instruction]
process_nest(functions_ins, s)
with open('csb.txt') as file:
lines = file.readlines()[1:]
lines = [line.strip().split() for line in lines]
# n = int(input())
# lines = [input().split() for _ in range(n)]
# print(*lines)
instructions = []
for line in lines:
instructions += line + ['\n']
print(instructions)
i = 0
instructions_len = len(instructions)
while i < instructions_len:
instruction = instructions[i]
if instruction == 'DEF':
def_func = instructions[i+1]
functions[def_func] = []
i += 1
elif instruction == 'END':
def_func = ''
elif def_func != '':
functions[def_func].append(instruction)
# elif instruction == '\n':
# stack_data = []
elif def_func == '':
if instruction == 'IF':
i = exec_if(stack_data, i, instructions)
else:
process(instruction, stack_data)
i += 1