-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
202 lines (142 loc) · 3.36 KB
/
handlers.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
from errors import *
# machine state
class VM:
def __init__(self):
self.pc = 0
self.stack = list()
self.heap = dict()
self.ras = list()
vm = VM()
def push(arg):
global vm
vm.stack.append(arg)
def pop():
global vm
if not vm.stack:
raiseOperandStackEmptyException()
vm.stack.pop()
def dup():
global vm
if not vm.stack:
raiseOperandStackEmptyException()
vm.stack.append(vm.stack[-1])
def swap():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
vm.stack[-1], vm.stack[-2] = vm.stack[-2], vm.stack[-1]
def add():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
arg = vm.stack[-2] + vm.stack[-1]
vm.stack = vm.stack[:-2]
vm.stack.append(arg)
def sub():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
arg = vm.stack[-2] - vm.stack[-1]
vm.stack = vm.stack[:-2]
vm.stack.append(arg)
def mul():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
arg = vm.stack[-2] * vm.stack[-1]
vm.stack = vm.stack[:-2]
vm.stack.append(arg)
def div():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
arg = vm.stack[-2] // vm.stack[-1]
vm.stack = vm.stack[:-2]
vm.stack.append(arg)
def mod():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
arg = vm.stack[-2] % vm.stack[-1]
vm.stack = vm.stack[:-2]
vm.stack.append(arg)
def load():
global vm
if not vm.stack:
raiseOperandStackEmptyException()
vm.stack.append(vm.heap[vm.stack.pop()])
def store():
global vm
if len(vm.stack) < 2:
raiseOperandStackEmptyException()
vm.heap[vm.stack[-2]] = vm.stack[-1]
vm.stack = vm.stack[:-2]
def label(arg):
pass
def call(arg):
global vm
vm.ras.append(vm.pc)
vm.pc = arg - 1
def jump(arg):
global vm
vm.pc = arg - 1
def jnil(arg):
global vm
if not vm.stack:
raiseOperandStackEmptyException()
if vm.stack.pop() == 0:
vm.pc = arg - 1
def jneg(arg):
global vm
if not vm.stack:
raiseOperandStackEmptyException()
if vm.stack.pop() < 0:
vm.pc = arg - 1
def ret():
global vm
if not vm.ras:
raise CallStackEmptyException()
vm.pc = vm.ras.pop()
def halt():
print()
exit(0)
def getchar(arg = None):
global vm
vm.stack.append(ord(input()[1]))
def getint(arg = None):
global vm
vm.stack.append(int(input()))
def putchar(arg = None):
global vm
if not vm.stack:
raiseOperandStackEmptyException()
print(chr(vm.stack.pop()), end = '')
def putint(arg = None):
global vm
if not vm.stack:
raiseOperandStackEmptyException()
print(vm.stack.pop(), end = '')
handlers = {
'push': push,
'pop': pop,
'dup': dup,
'swap': swap,
'add': add,
'sub': sub,
'mul': mul,
'div': div,
'mod': mod,
'load': load,
'store': store,
'label': label,
'call': call,
'jump': jump, # unconditional jump
'jnil': jnil, # jump if TOS == 0
'jneg': jneg, # jump if TOS < 0
'ret': ret,
'halt': halt,
'getchar': getchar,
'getint': getint,
'putchar': putchar,
'putint': putint
}