-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemu.py
executable file
·253 lines (209 loc) · 6.94 KB
/
emu.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
#!/usr/bin/python3
import sys
class Machine(object):
def __init__(self, stack = [], functions = {}):
self.stack = stack
self.control = []
self.cts = []
self.ip = 0
self.variables = {}
self.functions = functions
self.commands = ['DMP', 'RET', 'OUT', 'DIG', 'NUM', 'STR', 'DUP', 'SWP', 'ADD', 'SUB', 'MUL', 'DIV', 'MOD', 'LST', 'GRT', 'EQU', 'BCO', 'ECO', 'BIT', 'EIT', 'CT1', 'CT2', 'STO', 'RCL', 'DEF', 'FUN', 'INP', 'IND', 'LEN', 'BRK', 'NXT']
def feed(self, program):
self.ip = 0
self.program = program
def run(self):
while self.ip < len(self.program):
opcode = self.program[self.ip]
if opcode not in self.commands:
self.funccall(opcode)
else:
if getattr(self, 'cmd_' + opcode)():
return True
self.ip += 1
#print(self.stack)
def funccall(self, func):
if func not in self.functions:
raise SyntaxError('Unknown command: ' + func)
call = Machine(stack = self.stack, functions = self.functions)
call.feed(self.functions[func])
call.run()
def push(self, v):
self.stack.append(v)
def pop(self):
if len(self.stack) == 0:
raise BufferError
return self.stack.pop()
def pop2(self):
if len(self.stack) < 2:
raise BufferError
r = self.stack[-2]
del self.stack[-2]
return r
def cmd_DMP(self):
print(self.stack)
def cmd_SWP(self):
self.push(self.pop2())
def cmd_NUM(self):
n = self.program[self.ip + 1]
if '.' in n:
self.push(float(n))
else:
self.push(int(n))
self.ip += 1
def cmd_STR(self):
self.push(self.program[self.ip + 1].replace(';N', '\n').replace(';_', ' ').replace(';;', ';'))
self.ip += 1
def cmd_DIG(self):
if len(self.stack) < 2:
raise BufferError
self.push(self.stack[-2])
def cmd_OUT(self):
print(self.pop())
def cmd_INP(self):
self.push(input())
def cmd_IND(self):
if len(self.stack) < 2:
raise BufferError
self.push(self.stack[-2][self.pop() - 1])
def cmd_LEN(self):
if len(self.stack) < 1:
raise BufferError
self.push(len(self.stack[-1]))
def cmd_DUP(self):
if len(self.stack) < 1:
raise BufferError
self.push(self.stack[-1])
def cmd_BCO(self):
if self.pop():
self.control.append((-1,0,0))
else:
n=1
while self.program[self.ip] != 'ECO' or n:
self.ip += 1
if self.program[self.ip] == 'BCO':
n += 1
elif self.program[self.ip] == 'ECO':
n -= 1
def cmd_ECO(self):
if len(self.control) == 0 or self.control.pop()[0] != -1:
raise SyntaxError('ECO without matching BCO')
def cmd_BIT(self):
step = self.pop()
start = self.pop()
end = self.pop()
if start > end:
n = 1
while self.program[self.ip] != 'EIT' or n:
self.ip += 1
if self.program[self.ip] == 'BIT':
n += 1
elif self.program[self.ip] == 'EIT':
n -= 1
else:
self.control.append((self.ip, step, end))
self.cts.append(start)
def cmd_EIT(self):
if len(self.control) == 0 or self.control[-1][0] == -1:
raise SyntaxError('EIT without matching BIT')
self.cts[-1] += self.control[-1][1]
if self.cts[-1] <= self.control[-1][2]:
self.ip = self.control[-1][0]
else:
self.cts.pop()
self.control.pop()
def cmd_BRK(self):
ctrl = None
for i in range(-1, -len(self.control) - 1, -1):
if self.control[i][0] != -1:
ctrl = i
break
if ctrl is None:
raise SyntaxError('BRK outside iteration')
n = 1
while self.program[self.ip] != 'EIT' or n:
self.ip += 1
if self.program[self.ip] == 'BIT':
n += 1
elif self.program[self.ip] == 'EIT':
n -= 1
self.cts.pop()
self.control = self.control[:ctrl]
def cmd_CT1(self):
if len(self.cts) < 1:
raise SyntaxError('CT1 outside BIT')
self.push(self.cts[-1])
def cmd_CT2(self):
if len(self.cts) < 2:
raise SyntaxError('CT2 outside nested BIT')
self.push(self.cts[-2])
def cmd_RET(self):
return True
def cmd_ADD(self):
self.push(self.pop2() + self.pop())
def cmd_SUB(self):
self.push(self.pop2() - self.pop())
def cmd_MUL(self):
self.push(self.pop2() * self.pop())
def cmd_DIV(self):
self.push(self.pop2() / self.pop())
def cmd_MOD(self):
self.push(self.pop2() % self.pop())
def cmd_LST(self):
self.push(int(self.pop2() < self.pop()))
def cmd_GRT(self):
self.push(int(self.pop2() > self.pop()))
def cmd_EQU(self):
self.push(int(self.pop() == self.pop()))
def cmd_STO(self):
self.variables[self.program[self.ip + 1]] = self.pop()
self.ip += 1
def cmd_RCL(self):
if self.program[self.ip + 1] not in self.variables:
raise UnboundLocalError
self.push(self.variables[self.program[self.ip + 1]])
self.ip += 1
def cmd_DEF(self):
name = self.program[self.ip + 1]
self.ip += 2
function = []
while self.program[self.ip] != 'DEF':
function.append(self.program[self.ip])
self.ip += 1
self.functions[name] = function
def do(machine, program):
machine.feed(program.upper().split())
try:
if machine.run():
exit()
except SyntaxError as e:
print(e)
except BufferError:
print('Stack underflow')
except IndexError:
print('Execution past end of program')
except UnboundLocalError:
print('Invalid variable access')
except ValueError:
print('Invalid number')
except TypeError:
print('Type mismatch')
else:
return True
if __name__ == '__main__':
machine = Machine()
if len(sys.argv) >= 2:
for sourcefile in sys.argv[1:]:
if not sourcefile.endswith('.emu'):
sourcefile += '.emu'
f = open(sourcefile, 'r')
program = ' '.join([x.strip(' \n') for x in f.readlines() if len(x.strip()) and x.strip()[0] != '#'])
if do(machine, program) is not True:
exit()
while True:
try:
strin = input("> ")
except EOFError:
print()
exit()
do(machine, strin)