-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm.py
202 lines (181 loc) · 5.59 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# coding: utf-8
import sys
from constants import *
from util import print_msg
from error import (
StackEmptyError,
HeapIndexError,
StackIndexError,
NoExistLabelError,
)
class Stack(object):
def __init__(self):
self.l = []
def push(self, val):
self.l.append(val)
def count(self):
return len(self.l)
def is_empty(self):
if self.count() == 0:
return True
return False
def pop(self):
if self.is_empty():
raise StackEmptyError
return self.l.pop()
def copy(self, index):
try:
i = self.count()-index-1
if i < 0:
raise StackIndexError
self.l.append(self.l[i])
except:
import traceback
traceback.print_exc()
raise StackIndexError
def remove(self, index):
try:
i = self.count()-index-1
if i < 0:
raise StackIndexError
del self.l[i]
except:
import traceback
traceback.print_exc()
raise StackIndexError
def swap(self):
try:
self.l[-2], self.l[-1] = self.l[-1], self.l[-2]
except:
import traceback
traceback.print_exc()
raise StackIndexError
def get_two(self):
try:
return self.l.pop(), self.l.pop()
except:
import traceback
traceback.print_exc()
raise StackIndexError
class Heap(object):
def __init__(self):
self.l = []
def insert(self, index, val):
if index < 0:
print_msg("wocao", abort=True)
if index >= self.count():
for i in range(self.count(), index+1):
self.l.append(0)
self.l[index] = val
def count(self):
return len(self.l)
def get(self, index):
try:
value = self.l[index]
except:
import traceback
traceback.print_exc()
raise HeapIndexError
return value
class VM(object):
def __init__(self, instruction):
self.program = instruction
self.val_stack = Stack()
self.call_stack = Stack()
self.heap = Heap()
# program index
self.col = 0
def jump(self, val):
try:
self.col = self.program.index((LABEL, val))
except:
raise NoExistLabelError(val)
def exe(self, i):
if isinstance(i, tuple):
(instruction, val) = i
if instruction == PUSH:
self.val_stack.push(val)
elif instruction == REF:
self.val_stack.copy(val)
elif instruction == SLIDE:
self.val_stack.remove(val)
elif instruction == LABEL:
pass
elif instruction == CALL:
self.call_stack.push(self.col)
self.jump(val)
elif instruction == JUMP:
self.jump(val)
elif instruction == IF_ZERO:
if self.val_stack.pop() == 0:
self.jump(val)
elif instruction == IF_NEGATIVE:
if self.val_stack.pop() < 0:
self.jump(val)
else:
if i == END:
print_msg("\nThe End!\n")
return 'end'
elif i == DUP:
self.val_stack.copy(0)
elif i == SWAP:
self.val_stack.swap()
elif i == DISCARD:
self.val_stack.remove(0)
elif i == PLUS:
a, b = self.val_stack.get_two()
self.val_stack.push(b+a)
elif i == MINUS:
a, b = self.val_stack.get_two()
self.val_stack.push(b-a)
elif i == TIMES:
a, b = self.val_stack.get_two()
self.val_stack.push(b*a)
elif i == DIVIDE:
a, b = self.val_stack.get_two()
self.val_stack.push(b/a)
elif i == MODULO:
a, b = self.val_stack.get_two()
self.val_stack.push(b % a)
elif i == STORE:
val, index = self.val_stack.get_two()
self.heap.insert(index, val)
elif i == RETRIEVE:
index = self.val_stack.pop()
val = self.heap.get(index)
self.val_stack.push(val)
elif i == RETURN:
self.col = self.call_stack.pop()
elif i == OUTPUT_CHAR:
val = self.val_stack.pop()
print_msg('%c' % val)
elif i == OUTPUT_NUM:
val = self.val_stack.pop()
print_msg(val)
elif i == READ_CHAR:
index = self.val_stack.pop()
# val = raw_input()
val = sys.stdin.read(1)
try:
val = ord(val[0])
except:
print_msg("value error\n", abort=True)
self.heap.insert(index, val)
elif i == READ_NUM:
index = self.val_stack.pop()
val = raw_input()
try:
val = int(val)
except:
print_msg("value error\n", abort=True)
self.heap.insert(index, val)
def run(self):
while 1:
i = self.program[self.col]
self.col += 1
try:
r = self.exe(i)
if r == 'end':
break
except Exception, e:
print_msg(e, abort=True)