-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpreter.py
393 lines (338 loc) · 17.9 KB
/
interpreter.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#-------------------------------------------------------------------------------
# Name: interpreter
# Purpose: IPPe final project - Three code address interpreter
#
# Author: Guillermo Montes Martos (xmonte03)
# Created: April 18
#-------------------------------------------------------------------------------
import sys
import xml.etree.ElementTree as etree
from _elementtree import ParseError
class Interpreter:
def __init__(self, input):
try:
self.program = etree.parse(input).getroot()
except ParseError:
print('Error during the parsing of XML, invalid XML input or file cannot be opened.', file=sys.stderr)
exit(3)
self.variables = {}
self.labels = {}
self.pc = 0
self.data_stack = []
self.call_stack = []
def run(self):
self.read_labels()
self.check_args()
while self.pc < len(self.program):
op = self.program[self.pc].attrib['opcode']
if op == 'MOV':
self.mov(self.program[self.pc])
elif op == 'ADD':
self.add(self.program[self.pc])
elif op == 'SUB':
self.sub(self.program[self.pc])
elif op == 'MUL':
self.mul(self.program[self.pc])
elif op == 'DIV':
self.div(self.program[self.pc])
elif op == 'READINT':
self.read_int(self.program[self.pc])
elif op == 'PRINT':
self.print_(self.program[self.pc])
elif op == 'LABEL':
self.label(self.program[self.pc])
elif op == 'JUMP':
self.jump(self.program[self.pc])
elif op == 'JUMPIFEQ':
self.jumpifeq(self.program[self.pc])
elif op == 'JUMPIFGR':
self.jumpifgr(self.program[self.pc])
elif op == 'CALL':
self.call(self.program[self.pc])
elif op == 'RETURN':
self.return_(self.program[self.pc])
elif op == 'PUSH':
self.push(self.program[self.pc])
elif op == 'POP':
self.pop(self.program[self.pc])
elif op == 'READSTR':
self.readstr(self.program[self.pc])
elif op == 'CONCAT':
self.concat(self.program[self.pc])
elif op == 'GETAT':
self.getat(self.program[self.pc])
elif op == 'LEN':
self.len(self.program[self.pc])
elif op == 'STRINT':
self.strint(self.program[self.pc])
elif op == 'INTSTR':
self.intstr(self.program[self.pc])
else:
print('Semantic Error during the semantic checks: invalid operation.', file=sys.stderr)
exit(5)
self.pc += 1
#print(op + ' | pc='+str(self.pc-1))
#print(self.data_stack)
#print(self.variables)
def read_labels(self):
for i in range(len(self.program)):
if self.program[i].attrib['opcode'] == 'LABEL':
if self.program[i].find('dst').text in self.labels:
print("Semantic Error during the semantic checks: label duplicated", file=sys.stderr)
exit(5)
elif self.program[i].find('dst').attrib['kind'] != 'literal' or self.program[i].find('dst').attrib['type'] != 'string':
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
else:
self.labels[self.program[i].find('dst').text] = i
def check_args(self):
for taci in self.program:
if len(taci.attrib) > 3:
print('Semantic Error during the semantic checks: more arguments than needed.', file=sys.stderr)
exit(5)
for arg in taci:
if arg.tag not in ['src1', 'src2', 'dst']:
print("Semantic Error during the semantic checks: bad syntax for arguments.", file=sys.stderr)
exit(5)
elif arg.attrib['kind'] == 'variable' and not (not arg.text[0].isdigit() and all(c.isalnum() or c == '_' for c in arg.text)):
print('Semantic Error during the semantic checks: invalid variable name: ' + arg.text, file=sys.stderr)
exit(5)
def get_src_value(self, src):
if src.attrib['kind'] == 'variable':
try:
return self.variables[src.text]
except KeyError:
print("Run-time Error: Read access to non-defined or non-initialized variable." + src.text, file=sys.stderr)
exit(11)
else:
if src.attrib['type'] == 'integer':
try:
return int(src.text)
except ValueError:
print("Run-time Error: Invalid literal for a integer", file=sys.stderr)
exit(20)
else:
return src.text
def mov(self, command):
if command.find('dst') is None or command.find('src1') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('dst').attrib['kind'] == 'variable':
self.variables[command.find('dst').text] = self.get_src_value(command.find('src1'))
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def add(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if all(arg.attrib['type'] == 'integer' for arg in command) and command.find('dst').attrib['kind'] == 'variable':
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
self.variables[command.find('dst').text] = src1 + src2
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def sub(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if all(arg.attrib['type'] == 'integer' for arg in command) and command.find('dst').attrib['kind'] == 'variable':
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
self.variables[command.find('dst').text] = src1 - src2
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def mul(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if all(arg.attrib['type'] == 'integer' for arg in command) and command.find('dst').attrib['kind'] == 'variable':
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
self.variables[command.find('dst').text] = src1 * src2
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def div(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if all(arg.attrib['type'] == 'integer' for arg in command) and command.find('dst').attrib['kind'] == 'variable':
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
try:
self.variables[command.find('dst').text] = int(src1 / src2)
except ZeroDivisionError:
print("Run-time Error: Division by zero using DIV instruction.", file=sys.stderr)
exit(12)
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def read_int(self, command):
if command.find('dst') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('dst').attrib['kind'] == 'variable' and command.find('dst').attrib['type'] == 'integer':
try:
self.variables[command.find('dst').text] = int(input(command.find('dst').text + ' = '))
except ValueError:
print("Run-time Error: READINT get invalid value.", file=sys.stderr)
exit(13)
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def print_(self, command):
if command.find('src1') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
print(str( self.get_src_value(command.find('src1')) ))
def label(self, command):
return
def jump(self, command):
if command.find('dst') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('dst').attrib['kind'] != 'literal' or command.find('dst').attrib['type'] != 'string':
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
elif command.find('dst').text not in self.labels:
print("Run-time Error: Jump to a non-existing label or call to non-existing function.", file=sys.stderr)
exit(10)
else:
self.pc = self.labels[command.find('dst').text]
def jumpifeq(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] != command.find('src2').attrib['type'] or command.find('dst').attrib['type'] != 'string' or command.find('dst').attrib['kind'] != 'literal':
print('Run-time Error: Operands of incompatible type.', file=sys.stderr)
exit(14)
else:
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
if src1 == src2:
try:
self.pc = self.labels[command.find('dst').text]
except KeyError:
print('Run-time Error: Jump to a non-existing label or call to non-existing function.', file=sys.stderr)
exit(10)
def jumpifgr(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] != command.find('src2').attrib['type'] or command.find('dst').attrib['type'] != 'string' or command.find('dst').attrib['kind'] != 'literal':
print('Run-time Error: Operands of incompatible type.', file=sys.stderr)
exit(14)
else:
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
if src1 > src2:
try:
self.pc = self.labels[command.find('dst').text]
except KeyError:
print('Run-time Error: Jump to a non-existing label or call to non-existing function.', file=sys.stderr)
exit(10)
def call(self, command):
if command.find('dst') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('dst').attrib['kind'] != 'literal' or command.find('dst').attrib['type'] != 'string':
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
elif command.find('dst').text not in self.labels:
print("Run-time Error: Jump to a non-existing label or call to non-existing function.", file=sys.stderr)
exit(10)
else:
self.call_stack.append(self.pc)
self.pc = self.labels[command.find('dst').text]
def return_(self, command):
if len(self.call_stack) == 0:
print('Run-time Error: Pop from the empty call stack is forbidden', file=sys.stderr)
exit(15)
else:
self.pc = self.call_stack.pop()
def push(self, command):
if command.find('src1') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
self.data_stack.append(self.get_src_value(command.find('src1')))
def pop(self, command):
if command.find('dst') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('dst').attrib['kind'] != 'variable':
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
elif len(self.data_stack) == 0:
print('Run-time Error: Pop from the empty data stack is forbidden', file=sys.stderr)
exit(15)
else:
self.variables[command.find('dst').text] = self.data_stack.pop()
def readstr(self, command):
if command.find('dst') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('dst').attrib['kind'] == 'variable' and command.find('dst').attrib['type'] == 'string':
self.variables[command.find('dst').text] = input(command.find('dst').text + ' = ')
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def concat(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] == command.find('src2').attrib['type'] and command.find('src1').attrib['type'] == 'string' and command.find('dst').attrib['kind'] == 'variable':
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
self.variables[command.find('dst').text] = src1 + src2
else:
print('Run-time Error: Operands of incompatible type.', file=sys.stderr)
exit(14)
def getat(self, command):
if command.find('dst') is None or command.find('src1') is None or command.find('src2') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] == 'string' and command.find('src2').attrib['type'] == 'integer' and command.find('dst').attrib['kind'] == 'variable':
src1 = self.get_src_value(command.find('src1'))
src2 = self.get_src_value(command.find('src2'))
try:
self.variables[command.find('dst').text] = src1[src2]
except IndexError:
print("Run-time Error: Index out of bounds.", file=sys.stderr)
exit(20)
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def len(self, command):
if command.find('dst') is None or command.find('src1') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] == 'string' and command.find('dst').attrib['type'] == 'integer' and command.find('dst').attrib['kind'] == 'variable':
self.variables[command.find('dst').text] = len( self.get_src_value(command.find('src1')) )
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def strint(self, command):
if command.find('dst') is None or command.find('src1') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] == 'string' and command.find('dst').attrib['type'] == 'integer' and command.find('dst').attrib['kind'] == 'variable':
try:
self.variables[command.find('dst').text] = int( self.get_src_value(command.find('src1')) )
except ValueError:
print("Run-time Error: Invalid literal for a integer", file=sys.stderr)
exit(20)
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)
def intstr(self, command):
if command.find('dst') is None or command.find('src1') is None:
print('Semantic Error during the semantic checks: bad syntax for arguments.', file=sys.stderr)
exit(5)
if command.find('src1').attrib['type'] == 'integer' and command.find('dst').attrib['type'] == 'string' and command.find('dst').attrib['kind'] == 'variable':
self.variables[command.find('dst').text] = str( self.get_src_value(command.find('src1')) )
else:
print("Run-time Error: Operands of incompatible type.", file=sys.stderr)
exit(14)