-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands.py
209 lines (141 loc) · 4.37 KB
/
commands.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
class StopProgram(Exception):
pass
class InputError(Exception):
pass
class Jump(Exception):
def __init__(self, line):
self.line = line
def parse_digit(sign, digit):
digit = digit or '0'
digit = digit.replace(' ', '0').replace('\t', '1')
sign = sign.replace(' ', '1').replace('\t', '-1')
digit = int(sign) * int(digit, base=2)
return digit
def push_stack(line, inp, output, stack, heap, sign=None, digit=None):
assert digit is not None
assert sign is not None
stack.append(parse_digit(sign, digit))
return inp, output
def duplicate_nth(line, inp, output, stack, heap, sign=None, digit=None):
assert digit is not None
assert sign is not None
n = parse_digit(sign, digit)
if n < 0:
raise IndexError
stack.append(stack[-n-1])
return inp, output
def discard_top_n(line, inp, output, stack, heap, sign=None, digit=None):
assert digit is not None
assert sign is not None
n = parse_digit(sign, digit)
if n < 0 or n >= len(stack):
top = stack.pop()
del stack[:]
stack.append(top)
else:
stack_copy = stack[:]
del stack[:]
for i in range(0, len(stack_copy)-n-1):
stack.append(stack_copy[i])
stack.append(stack_copy[-1])
return inp, output
def duplicate_top(line, inp, output, stack, heap):
stack.append(stack[-1])
return inp, output
def swap_first_two(line, inp, output, stack, heap):
a = stack.pop()
b = stack.pop()
stack.append(a)
stack.append(b)
return inp, output
def discard_top(line, inp, output, stack, heap):
stack.pop()
return inp, output
def addition(line, inp, output, stack, heap):
stack.append(stack.pop() + stack.pop())
return inp, output
def subtraction(line, inp, output, stack, heap):
a = stack.pop()
b = stack.pop()
stack.append(b - a)
return inp, output
def multiplication(line, inp, output, stack, heap):
stack.append(stack.pop() * stack.pop())
return inp, output
def division(line, inp, output, stack, heap):
a = stack.pop()
b = stack.pop()
stack.append(b // a)
return inp, output
def modulo(line, inp, output, stack, heap):
a = stack.pop()
b = stack.pop()
stack.append(b % a)
return inp, output
def heap_retrieve(line, inp, output, stack, heap):
a = stack.pop()
stack.append(heap[a])
return inp, output
def heap_store(line, inp, output, stack, heap):
a = stack.pop()
b = stack.pop()
heap[b] = a
return inp, output
def read_char(line, inp, output, stack, heap):
if not inp:
raise InputError
a, inp = inp[0], inp[1:]
b = stack.pop()
heap[b] = ord(a)
return inp, output
def read_number(line, inp, output, stack, heap):
i = inp.find('\n')
if i == -1:
raise InputError
try:
a = int(inp[:i])
except ValueError:
try:
a = int(inp[:i], base=16)
except:
raise InputError
inp = inp[i+1:]
b = stack.pop()
heap[b] = a
return inp, output
def pop_print(line, inp, output, stack, heap):
output += str(stack.pop())
return inp, output
def pop_print_chr(line, inp, output, stack, heap):
output += chr(int(stack.pop()))
return inp, output
def mark_location(line, inp, output, stack, heap, **kwargs):
return inp, output
def call_subroutine(line, inp, output, stack, heap,
call_stack=None,
label=None,
locations=None):
assert label is not None
assert call_stack is not None
call_stack.append(line)
raise Jump(locations[label])
def jump(line, inp, output, stack, heap, label=None, locations=None):
assert label is not None
raise Jump(locations[label])
def jump_if_zero(line, inp, output, stack, heap, label=None, locations=None):
assert label is not None
if stack.pop() == 0:
raise Jump(locations[label])
return inp, output
def jump_if_lt_zero(line, inp, output, stack, heap,
label=None,
locations=None):
assert label is not None
if stack.pop() < 0:
raise Jump(locations[label])
return inp, output
def exit_subroutine(line, inp, output, stack, heap, call_stack=None):
assert call_stack is not None
raise Jump(call_stack.pop() + 1)
def exit_program(line, inp, output, stack, heap):
raise StopProgram