This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
212 lines (149 loc) · 3.58 KB
/
interpreter.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
210
211
212
import struct
import io
import os
import pathlib as path
import sys
from enum import Enum
class instr(Enum):
nop = 0x00
push_int = 0x01
push_str = 0x02
push_float = 0x03
push_nil = 0x04
pop = 0x20
add = 0x21
sub = 0x22
mul = 0x23
div = 0x24
store = 0x25
load = 0x26
jump = 0x27
print = 0xA0
halt = 0xFF
class Interpreter:
def __init__(self, data):
self.call_stack = [{"__frame__": 0, "__return__": 0}]
self.value_stack = []
self.stream = io.BytesIO(data)
self.symbols = {}
self.runInitial()
def readUInt8(self):
d = self.stream.read(1)
if (d == None): return None
[x] = struct.unpack("B", d)
return x
def readUInt16(self):
d = self.stream.read(2)
if (d == None): return None
[x] = struct.unpack("H", d)
return x
def readUInt32(self):
d = self.stream.read(4)
if (d == None): return None
[x] = struct.unpack("I", d)
return x
def readUInt64(self):
d = self.stream.read(8)
if (d == None): return None
[x] = struct.unpack("Q", d)
return x
def readString(self):
s = bytearray()
while (True):
c = self.stream.read(1)
if (c == b"\x00"):
break
s += c
return s.decode("utf-8")
def jump(self, pos):
self.stream.seek(self.base_pos + pos, os.SEEK_SET)
def tell(self):
return self.stream.tell()
def push(self, val):
self.value_stack.append(val)
def pop(self):
val = self.value_stack[-1]
self.value_stack = self.value_stack[:-1]
return val
def runInitial(self):
# Read initial instruction pointer
initial_ip = self.readUInt32()
# Read symbols
sym_count = self.readUInt32()
for i in range(sym_count):
k = self.readString()
v = self.readUInt32()
# Note: we need to patch this later
self.symbols[k] = v
# Read base code pos
self.base_pos = self.tell()
# Jump to end
self.jump(initial_ip)
def run(self, ip = None):
"""
Run the script from the given position
"""
if (ip != None):
self.jump(ip)
while (True):
opcode = self.readUInt8()
print(hex(self.tell())[2:], "|", hex(opcode)[2:])
match (opcode):
case 0x00: # nop
pass
case 0x01: # push <int>
self.push(self.readUInt32())
case 0x02: # push <str>
self.push(self.readString())
case 0x03: # push <float>
self.push(self.readFloat32()) # !!!
case 0x04: # push <nil>
self.push(None)
case 0x20: # pop
self.pop()
case 0x21: # add
b = self.pop()
a = self.pop()
self.push(a + b)
case 0x22: # sub
b = self.pop()
a = self.pop()
self.push(a - b)
case 0x23: # mul
b = self.pop()
a = self.pop()
self.push(a * b)
case 0x24: # div
b = self.pop()
a = self.pop()
self.push(a / b)
case 0x25: # str
value = self.pop()
key = self.pop()
self.call_stack[-1][key] = value
case 0x26: # ldr
self.push(self.call_stack[-1][self.pop()])
case 0x27: # jmp
self.jump(self.pop())
case 0x28: # jz
addr = self.pop()
val = self.pop()
if (val == 0):
self.jump(addr)
case 0xA0: # print
print(self.pop())
case 0xFF: # halt
break
case _:
raise BaseException(f"Bad opcode at {self.ip}")
def call(self, symbol = "main"):
ip = self.symbols.get(symbol, None)
if (ip == None):
return
print(f"\t{symbol} {hex(ip)[2:]}")
self.run(ip)
def main():
i = Interpreter(path.Path(sys.argv[1]).read_bytes())
i.call(sys.argv[2] if len(sys.argv) > 2 else "main")
if (__name__ == "__main__"):
main()