Skip to content

Commit 090f899

Browse files
committed
first commit
0 parents  commit 090f899

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

ABM Compiler.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#open file
2+
fpin = open('input.abm', 'r')
3+
fpout = open('compile.out', 'w')
4+
5+
from collections import namedtuple
6+
7+
stack = [] #stack array
8+
command = [] #command array
9+
mapLabel = {} #label index map(for goto)
10+
link = {} #
11+
12+
A = ''
13+
B = ''
14+
15+
16+
Pair = namedtuple("Pair", ["first", "second"])
17+
18+
19+
def getPQ(a):
20+
global A, B
21+
a = a.lstrip(' ')
22+
a = a.lstrip('\t')
23+
if len(a) > 1:
24+
a = a[:-1]
25+
else :
26+
A= ''
27+
return
28+
sp = a.split(' ', 1)
29+
A = sp[0]
30+
B = '';
31+
if len(sp) > 1:
32+
B = sp[1]
33+
B = B.lstrip(' ')
34+
B = B.strip(' ')
35+
36+
37+
38+
def run_command(idx, ram, localram):
39+
global A, B
40+
if idx >= len(command):
41+
return;
42+
com = command[idx]
43+
getPQ(com)
44+
p = A
45+
q = B
46+
47+
if p == 'push':
48+
stack.append(Pair("$", int(q)))
49+
if p == 'pop':
50+
stack.pop();
51+
if p == 'rvalue':
52+
if q in ram:
53+
stack.append(Pair(q, ram[q]))
54+
else:
55+
stack.append(Pair(q, 0))
56+
if p == 'lvalue':
57+
stack.append(Pair(q, int(0)))
58+
localram[q] = 0
59+
if p == ':=':
60+
r = stack.pop()
61+
l = stack.pop()
62+
ram[l.first] = r.second
63+
localram[l.first] = r.second
64+
if p == 'copy':
65+
stack.append(stack[-1]);
66+
67+
if p == 'goto':
68+
run_command(mapLabel[q], ram, localram)
69+
return;
70+
if p == 'gofalse':
71+
logicState = (stack.pop()).second
72+
if logicState == 0:
73+
run_command(mapLabel[q], ram, localram)
74+
return
75+
if p == 'gotrue':
76+
logicState = (stack.pop()).second
77+
if logicState != 0:
78+
run_command(mapLabel[q], ram, localram)
79+
return
80+
if p == 'halt':
81+
return
82+
83+
if p == '+':
84+
r = (stack.pop()).second
85+
l = (stack.pop()).second
86+
stack.append(Pair('$', l + r));
87+
if p == '-':
88+
r = (stack.pop()).second
89+
l = (stack.pop()).second
90+
stack.append(Pair('$', l - r));
91+
if p == '*':
92+
r = (stack.pop()).second
93+
l = (stack.pop()).second
94+
stack.append(Pair('$', l * r));
95+
if p == '/':
96+
r = (stack.pop()).second
97+
l = (stack.pop()).second
98+
stack.append(Pair('$', int(l / r)));
99+
if p == 'div':
100+
r = (stack.pop()).second
101+
l = (stack.pop()).second
102+
stack.append(Pair('$', l % r));
103+
if p == '&':
104+
r = (stack.pop()).second
105+
l = (stack.pop()).second
106+
stack.append(Pair('$', l & r));
107+
if p == '|':
108+
r = (stack.pop()).second
109+
l = (stack.pop()).second
110+
stack.append(Pair('$', l | r));
111+
if p == '!':
112+
l = (stack.pop()).second
113+
stack.append(Pair('$', not(l)));
114+
115+
if p == '<>':
116+
r = (stack.pop()).second
117+
l = (stack.pop()).second
118+
stack.append(Pair('$', l != r));
119+
if p == '<':
120+
r = (stack.pop()).second
121+
l = (stack.pop()).second
122+
stack.append(Pair('$', l < r));
123+
if p == '>':
124+
r = (stack.pop()).second
125+
l = (stack.pop()).second
126+
stack.append(Pair('$', l > r));
127+
if p == '<=':
128+
r = (stack.pop()).second
129+
l = (stack.pop()).second
130+
stack.append(Pair('$', l <= r));
131+
if p == '>=':
132+
r = (stack.pop()).second
133+
l = (stack.pop()).second
134+
stack.append(Pair('$', l >= r));
135+
if p == '=':
136+
r = (stack.pop()).second
137+
l = (stack.pop()).second
138+
stack.append(Pair('$', l = r));
139+
140+
if p == 'print':
141+
fpout.write(str(stack[-1].second) + '\n')
142+
if p == 'show':
143+
fpout.write(q + '\n')
144+
if p == 'call':
145+
emptyram = {}
146+
run_command(mapLabel[q], localram, emptyram)
147+
for t in localram:
148+
if t in ram:
149+
ram[t] = localram[t]
150+
if p == 'return':
151+
return
152+
if p == 'begin':
153+
emptyram = {}
154+
nram = {}
155+
for t in ram:
156+
nram[t] = ram[t]
157+
run_command(idx + 1, nram, emptyram)
158+
for t in nram:
159+
if t in ram:
160+
ram[t] = nram[t]
161+
run_command(link[idx], ram, localram)
162+
return
163+
if p == 'end':
164+
return
165+
166+
run_command(idx+1, ram, localram)
167+
168+
169+
#reading input data
170+
while True:
171+
txt = fpin.readline()
172+
if txt == '':
173+
break
174+
command.append(txt)
175+
176+
#hasing label to line and link
177+
lvs = []
178+
for i in range(len(command)):
179+
getPQ(command[i])
180+
if A == 'label':
181+
mapLabel[B] = i + 1
182+
if A == 'begin':
183+
lvs.append(i)
184+
if A == 'end':
185+
link[lvs.pop()] = i + 1
186+
187+
188+
#run
189+
ram = {}
190+
localram = {}
191+
run_command(0, ram, localram)
192+
193+
print("ok")
194+
195+
#close file
196+
fpin.close()
197+
fpout.close()

0 commit comments

Comments
 (0)