-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.py
180 lines (148 loc) Β· 4.45 KB
/
vm.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
import sys
# Implements a simple stack-based VM
class VM:
def __init__(self, rom):
self.rom = rom
self.accumulator1 = 0
self.accumulator2 = 0
self.instruction_pointer = 1
self.stack = []
def step(self):
cur_ins = self.rom[self.instruction_pointer]
self.instruction_pointer += 1
fn = VM.OPERATIONS.get(cur_ins, None)
if cur_ins[0] == 'π':
return
if fn is None:
raise RuntimeError("Unknown instruction '{}' at {}".format(
repr(cur_ins), self.instruction_pointer - 1))
else:
fn(self)
def add(self):
self.stack.append(self.stack.pop() + self.stack.pop())
def sub(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b - a)
def if_zero(self):
if self.stack[-1] == 0:
while self.rom[self.instruction_pointer] != 'π':
if self.rom[self.instruction_pointer] in ['π', 'β°']:
break
self.step()
else:
self.find_first_endif()
self.instruction_pointer += 1
def if_not_zero(self):
if self.stack[-1] != 0:
while self.rom[self.instruction_pointer] != 'π':
if self.rom[self.instruction_pointer] in ['π', 'β°']:
break
self.step()
else:
self.find_first_endif()
self.instruction_pointer += 1
def find_first_endif(self):
while self.rom[self.instruction_pointer] != 'π':
self.instruction_pointer += 1
def jump_to(self):
marker = self.rom[self.instruction_pointer]
if marker[0] != 'π°':
print('Incorrect symbol : ' + marker[0])
raise SystemExit()
marker = 'π' + marker[1:]
self.instruction_pointer = self.rom.index(marker) + 1
def jump_top(self):
self.instruction_pointer = self.stack.pop()
def exit(self):
print('\nDone.')
raise SystemExit()
def print_top(self):
sys.stdout.write(chr(self.stack.pop()))
sys.stdout.flush()
def push(self):
if self.rom[self.instruction_pointer] == 'π₯':
self.stack.append(self.accumulator1)
elif self.rom[self.instruction_pointer] == 'π₯':
self.stack.append(self.accumulator2)
else:
raise RuntimeError('Unknown instruction {} at position {}'.format(
self.rom[self.instruction_pointer], str(self.instruction_pointer)))
self.instruction_pointer += 1
def pop(self):
if self.rom[self.instruction_pointer] == 'π₯':
self.accumulator1 = self.stack.pop()
elif self.rom[self.instruction_pointer] == 'π₯':
self.accumulator2 = self.stack.pop()
else:
raise RuntimeError('Unknown instruction {} at position {}'.format(
self.rom[self.instruction_pointer], str(self.instruction_pointer)))
self.instruction_pointer += 1
def pop_out(self):
self.stack.pop()
def load(self):
num = 0
if self.rom[self.instruction_pointer] == 'π₯':
acc = 1
elif self.rom[self.instruction_pointer] == 'π₯':
acc = 2
else:
raise RuntimeError('Unknown instruction {} at position {}'.format(
self.rom[self.instruction_pointer], str(self.instruction_pointer)))
self.instruction_pointer += 1
while self.rom[self.instruction_pointer] != 'β':
num = num * 10 + (ord(self.rom[self.instruction_pointer][0]) - ord('0'))
self.instruction_pointer += 1
if acc == 1:
self.accumulator1 = num
else:
self.accumulator2 = num
self.instruction_pointer += 1
def clone(self):
self.stack.append(self.stack[-1])
def multiply(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b * a)
def divide(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b // a)
def modulo(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b % a)
def xor(self):
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(b ^ a)
OPERATIONS = {
'π‘': add,
'π€‘': clone,
'π': divide,
'π²': if_zero,
'π': if_not_zero,
'π': jump_to,
'π': load,
'π¬': modulo,
'β': multiply,
'πΏ': pop,
'π€': pop_out,
'π€': print_top,
'π₯': push,
'πͺ': sub,
'π': xor,
'β°': jump_top,
'β': exit
}
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Missing program')
raise SystemExit()
with open(sys.argv[1], 'r') as f:
print('Running ....')
all_ins = ['']
all_ins.extend(f.read().split())
vm = VM(all_ins)
while 1:
vm.step()