-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·259 lines (259 loc) · 6.21 KB
/
test.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/python3
a, b = [int(i) for i in input().split()]
asm = '''
func Add:
alc b
pop b
alc a
pop a
push a
push b
add
ret
func_back
alc a
alc b
push a
push {}
{}
pop a
pop
push b
push {}
{}
pop b
pop
va_split
va_split
push a
push b
call Add
call exit
pop
exit
'''.format(a if a >= 0 else -a, '' if a >= 0 else 'neg',
b if b >= 0 else -b, '' if b >= 0 else 'neg')
import re
import sys
labels = {'main':[]}
exit_val = 0
def ProgExit(val):
exit_val = val
functions = {}
jumps = {'main':{}}
# 'function_name': codes
label_id = 'main'
cmd_pos = 0
def Nothing():
pass
debug = 'F'
Break = ipdb.set_trace if debug == 'T' else Nothing
reg = r'((?:"(?:(?:\\.)|[^"])*")|\d+|(?::{0,1}\w+:{0,1}))'
for line in asm.split('\n'):
cmd = re.findall(reg, line)
#print(cmd)
if len(cmd) == 0:
continue
if cmd[0][-1] == ':':
# label
label_id = cmd[0][:-1]
labels[label_id] = []
cmd_pos = 0
elif cmd[0][0] == ':':
# jump tag
if label_id not in jumps:
jumps[label_id] = {}
jumps[label_id][cmd[0][1:]] = cmd_pos
elif cmd[0] == 'func':
func_name = cmd[1][:-1]
functions[func_name] = None
label_id = func_name
labels[label_id] = []
cmd_pos = 0
elif 'back' in cmd[0]:
labels[label_id].append(cmd)
label_id = 'main'
cmd_pos = 0
else:
labels[label_id].append(cmd)
cmd_pos += 1
if debug == 'T':
print('functions:', functions)
print('labels :', labels)
print('jumps :', jumps)
tmp_stack = [None, None]
prog_stack = [{}]
label_stack = [['main', 0]]
ret = None
class Spliter:
def __init__(self):
pass
def __repr__(self):
return '|'
while True:
cmd = labels[label_stack[-1][0]][label_stack[-1][1]]
if debug == 'T':
print('label_stack :', label_stack)
print('prog_stack :', prog_stack)
print('tmp_stack :', tmp_stack)
print(cmd)
print()
label_stack[-1][1] += 1
T0 = tmp_stack[-1]
T1 = tmp_stack[-2]
if cmd[0] == 'exit':
Break()
break
elif cmd[0] == 'va_split':
Break()
tmp_stack.append(Spliter())
elif cmd[0] == 'pop':
Break()
if len(cmd) == 2:
prog_stack[-1][cmd[1]] = T0
tmp_stack.pop()
elif cmd[0] == 'alc':
Break()
prog_stack[-1][cmd[1]] = None
elif cmd[0] == 'ret':
Break()
ret = T0
while type(tmp_stack[-1]) != Spliter:
tmp_stack.pop()
while type(label_stack[-1]) != Spliter:
label_stack.pop()
while type(prog_stack[-1]) != Spliter:
prog_stack.pop()
tmp_stack[-1] = ret
label_stack.pop()
prog_stack.pop()
# TODO: give ret back to stack.
elif cmd[0] == 'func_back':
Break()
while type(tmp_stack[-1]) != Spliter:
tmp_stack.pop()
while type(label_stack[-1]) != Spliter:
label_stack.pop()
while type(prog_stack[-1]) != Spliter:
prog_stack.pop()
tmp_stack[-1] = None
label_stack.pop()
prog_stack.pop()
elif cmd[0] == 'if':
Break()
if bool(T0):
label_id = cmd[1]
label_stack.append([label_id, 0])
elif cmd[0] == 'ifn':
Break()
if not bool(T0):
label_id = cmd[1]
label_stack.append([label_id, 0])
elif cmd[0] == 'while':
Break()
if bool(T0):
label_id = cmd[1]
label_stack.append([label_id, 0])
elif cmd[0] == 'while_if':
Break()
if bool(T0):
label_id = label_stack[-1][0]
jump_to = jumps[label_id][cmd[1]]
label_stack[-1][1] = jump_to
tmp_stack.pop()
# TODO: JUMP
elif cmd[0] == 'push':
Break()
if cmd[1].isdigit():
tmp_stack.append(int(cmd[1]))
elif cmd[1][0] == '"':
tmp_stack.append(eval(cmd[1]))
else:
tmp_stack.append(prog_stack[-1][cmd[1]])
elif cmd[0] == 'back':
Break()
label_stack.pop()
elif cmd[0] == 'and':
Break()
tmp_stack.pop()
T1 = bool(T1 and T0)
tmp_stack[-1] = int(T1)
elif cmd[0] == 'or':
Break()
tmp_stack.pop()
T1 = bool(T1 or T0)
tmp_stack[-1] = int(T1)
elif cmd[0] == 'lt':
Break()
tmp_stack.pop()
tmp_stack[-1] = int(T1 < T0)
elif cmd[0] == 'gt':
Break()
tmp_stack.pop()
tmp_stack[-1] = int(T1 > T0)
elif cmd[0] == 'ge':
Break()
tmp_stack.pop()
tmp_stack[-1] = int(T1 >= T0)
elif cmd[0] == 'le':
Break()
tmp_stack.pop()
tmp_stack[-1] = int(T1 <= T0)
elif cmd[0] == 'eq':
Break()
tmp_stack.pop()
tmp_stack[-1] = int(T1 == T0)
elif cmd[0] == 'ne':
Break()
tmp_stack.pop()
tmp_stack[-1] = int(T1 != T0)
elif cmd[0] == 'add':
Break()
tmp_stack.pop()
T1 = T1 + T0
tmp_stack[-1] = T1
elif cmd[0] == 'min':
Break()
tmp_stack.pop()
T1 = T1 - T0
tmp_stack[-1] = T1
elif cmd[0] == 'mul':
Break()
tmp_stack.pop()
T1 = T1 * T0
tmp_stack[-1] = T1
elif cmd[0] == 'div':
Break()
tmp_stack.pop()
T1 = T1 // T0
tmp_stack[-1] = T1
elif cmd[0] == 'mod':
Break()
tmp_stack.pop()
T1 = T1 % T0
tmp_stack[-1] = T1
elif cmd[0] == 'neg':
Break()
tmp_stack[-1] = -tmp_stack[-1]
elif cmd[0] == 'call':
Break()
if cmd[1] == 'print':
ret = print(T0, end = '\n' if debug == 'T' else '')
while type(tmp_stack[-1]) != Spliter:
tmp_stack.pop()
tmp_stack[-1] = ret
if cmd[1] == 'exit':
exit_val = T0
break
else:
label_stack.append(Spliter())
label_stack.append([cmd[1], 0])
prog_stack.append(Spliter())
prog_stack.append({})
elif cmd[0] == 'jmp':
Break()
# TODO jump instruction
else:
print('Unknow instruction:', cmd)
exit(0)
print(exit_val)