-
Notifications
You must be signed in to change notification settings - Fork 1
/
ABM Compiler.py
197 lines (174 loc) · 4.5 KB
/
ABM Compiler.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
#open file
fpin = open('input.abm', 'r')
fpout = open('compile.out', 'w')
from collections import namedtuple
stack = [] #stack array
command = [] #command array
mapLabel = {} #label index map(for goto)
link = {} #
A = ''
B = ''
Pair = namedtuple("Pair", ["first", "second"])
def getPQ(a):
global A, B
a = a.lstrip(' ')
a = a.lstrip('\t')
if len(a) > 1:
a = a[:-1]
else :
A= ''
return
sp = a.split(' ', 1)
A = sp[0]
B = '';
if len(sp) > 1:
B = sp[1]
B = B.lstrip(' ')
B = B.strip(' ')
def run_command(idx, ram, localram):
global A, B
if idx >= len(command):
return;
com = command[idx]
getPQ(com)
p = A
q = B
if p == 'push':
stack.append(Pair("$", int(q)))
if p == 'pop':
stack.pop();
if p == 'rvalue':
if q in ram:
stack.append(Pair(q, ram[q]))
else:
stack.append(Pair(q, 0))
if p == 'lvalue':
stack.append(Pair(q, int(0)))
localram[q] = 0
if p == ':=':
r = stack.pop()
l = stack.pop()
ram[l.first] = r.second
localram[l.first] = r.second
if p == 'copy':
stack.append(stack[-1]);
if p == 'goto':
run_command(mapLabel[q], ram, localram)
return;
if p == 'gofalse':
logicState = (stack.pop()).second
if logicState == 0:
run_command(mapLabel[q], ram, localram)
return
if p == 'gotrue':
logicState = (stack.pop()).second
if logicState != 0:
run_command(mapLabel[q], ram, localram)
return
if p == 'halt':
return
if p == '+':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l + r));
if p == '-':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l - r));
if p == '*':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l * r));
if p == '/':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', int(l / r)));
if p == 'div':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l % r));
if p == '&':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l & r));
if p == '|':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l | r));
if p == '!':
l = (stack.pop()).second
stack.append(Pair('$', not(l)));
if p == '<>':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l != r));
if p == '<':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l < r));
if p == '>':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l > r));
if p == '<=':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l <= r));
if p == '>=':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l >= r));
if p == '=':
r = (stack.pop()).second
l = (stack.pop()).second
stack.append(Pair('$', l = r));
if p == 'print':
fpout.write(str(stack[-1].second) + '\n')
if p == 'show':
fpout.write(q + '\n')
if p == 'call':
emptyram = {}
run_command(mapLabel[q], localram, emptyram)
for t in localram:
if t in ram:
ram[t] = localram[t]
if p == 'return':
return
if p == 'begin':
emptyram = {}
nram = {}
for t in ram:
nram[t] = ram[t]
run_command(idx + 1, nram, emptyram)
for t in nram:
if t in ram:
ram[t] = nram[t]
run_command(link[idx], ram, localram)
return
if p == 'end':
return
run_command(idx+1, ram, localram)
#reading input data
while True:
txt = fpin.readline()
if txt == '':
break
command.append(txt)
#hasing label to line and link
lvs = []
for i in range(len(command)):
getPQ(command[i])
if A == 'label':
mapLabel[B] = i + 1
if A == 'begin':
lvs.append(i)
if A == 'end':
link[lvs.pop()] = i + 1
#run
ram = {}
localram = {}
run_command(0, ram, localram)
print("ok")
#close file
fpin.close()
fpout.close()