-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.py
More file actions
148 lines (110 loc) · 3.93 KB
/
vm.py
File metadata and controls
148 lines (110 loc) · 3.93 KB
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
import ast, sys
class Vm:
def __init__(self):
self.processor = []
self.instructions = []
self.index = 0
self.instructions_count = 0
def push(self, x):
self.processor.append(x)
# math
def plus(self, a, b):
return a + b
def minus(self, a, b):
return a - b
def mod(self, a, b):
return a % b
def mul(self, a, b):
return a * b
def div(self, a, b):
return a / b
# builtins
def _int(self, x):
return int(x)
def _str(self, x):
return str(x)
def _print(self):
self.index += 1
sys.stdout.write(str(self.processor[13](self.processor[self.index])) + "\n")
sys.stdout.flush()
def _load(self, x):
return self.processor[x]
# logic
def _assign(self):
self.index += 1
if self.processor[self.index] == 9:
self.index += 1
x = self.processor[self.index]
self.index += 1
self.processor[self.index] = self.processor[6](x)
#print("ASSIGN: " + str(self.index), self.processor[6](x))
elif self.processor[self.index] in [1,2,3,4,5]:
opcode = self.processor[self.index]
self.index += 1
var_type = self.processor[self.index]
self.index += 1
x = self.processor[self.index]
self.index += 1
y = self.processor[self.index]
self.index += 1
res = self.processor[opcode](self.processor[13](x),self.processor[13](y))
if var_type == 9:
self.processor[self.index] = self.processor[6](res)
#print("ASSIGN: " + str(self.index), self.processor[6](res))
def function_def(self):
self.index += 1
while self.processor[self.index] != 15:
self.index += 1
self.index += 1
def function_run(self):
self.index += 1
function_index = self.processor[self.index]
start_index = self.index
self._jmp(function_index)
while self.processor[self.index] != 15:
self.instructions[self.processor[self.index]]()
self.index += 1
self._jmp(start_index)
def _label(self):
pass
def _jmp(self, i):
self.index = i
def load_functions(self):
self.instructions = [
0, # free
self.plus, # 1
self.minus, # 2
self.mod, # 3
self.mul, # 4
self.div, # 5
self._int, # 6
self._str, # 7
self._print, # 8
9, # int
10, # str
11, # float
self._assign, # 12
self._load, # 13
self.function_def, # 14
self._label, # 15
self.function_run, # 16
]
for i in self.instructions:
self.push(i)
self.instructions_count = len(self.processor)
def run(self):
self.load_functions()
for i in [12, 9, 1, 0, 14, 12, 9, 2, 0, 8, 25, 15, 0, 14, 12, 9, 1, 0, 12, 9, 10, 0, 12, 2, 9, 34, 38, 0, 8, 44, 15, 0, 16, 22, 16, 31, 16, 22, 16, 31]:
self.processor.append(i)
self.index = self.instructions_count
print("VM INSTRUCTIONS: " + str(self.index))
print("TOTAL: " + str(len(self.processor)))
while True:
#print("VM RUN: " + str(self.instructions[self.processor[self.index]]), self.index)
self.instructions[self.processor[self.index]]()
self.index += 1
if self.index >= len(self.processor):
break
#print(self.processor)
if __name__ == "__main__":
Vm().run()