-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.py
executable file
·220 lines (179 loc) · 7.58 KB
/
lex.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
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
import sys
from ops import *
def lex_program(program_filename: str, args) -> list:
verbose = args['verbose']
program = []
ip_stack = []
with open(program_filename, "r") as program_file:
lines = program_file.readlines()
instruction_pointer = 0
for line_number, line in enumerate(lines):
# ignore whitespace
line = ' '.join(line.split())
# skip empty lines
if len(line) == 0:
continue
instructions = line.replace('\n', '').split(' ')
string = ''
advance = True
if instructions[0] == 'include':
if verbose:
print(instructions)
module = instructions[1].replace('"', '')
module_instructions = lex_program(module, args)
program.extend(module_instructions)
continue
for instruction in instructions:
# if verbose:
# print(instruction_pointer, instruction)
assert OP_COUNT == 26, "Must handle all instructions in lex_program"
if '"' in instruction:
advance = False
s = instruction
s = instruction.replace('"', '')
s = s.replace('\\n', '\n')
s = s.replace('\\t', '\t')
s = s.replace('\\r', '\r')
s = s.replace('\\40', '\40')
if instruction.endswith('"'):
advance = True
string += ' ' + s
op = (OP_PRINTS, string)
program.append(op)
string = ''
else:
string += s
elif string:
advance = False
s = instruction
string += ' ' + s
elif "#" in instruction:
break
elif 'pop' in instruction:
split = instruction.split(':')
if len(split) == 1:
op = (OP_POP, )
program.append(op)
if len(split) == 2:
var_name = split[1]
op = (OP_POP, var_name)
program.append(op)
elif instruction == "+":
op = (OP_ADD, )
program.append(op)
elif instruction == "-":
op = (OP_SUB, )
program.append(op)
elif instruction == "*":
op = (OP_MULTIPLY, )
program.append(op)
elif instruction == "/":
op = (OP_DIVIDE, )
program.append(op)
elif instruction == "dump":
op = (OP_DUMP, )
program.append(op)
elif instruction == "print":
op = (OP_PRINT, )
program.append(op)
elif "dup" in instruction:
op = None
if instruction == "dup":
op = (OP_DUP, )
else:
op = (OP_DUP, int(instruction.replace("dup", "")))
program.append(op)
elif instruction == "if":
op = (OP_IF, )
ip_stack.append({"type": OP_IF, "ip": instruction_pointer})
program.append(op)
elif instruction == "swap":
op = (OP_SWAP, )
program.append(op)
elif instruction == "else":
op = (OP_ELSE, )
# make previous if instruction jump
# to just after this else if condition
# is false
start_of_block = ip_stack.pop()["ip"]
program[start_of_block] = (program[start_of_block][0],instruction_pointer + 1)
program.append(op)
ip_stack.append({"type": OP_ELSE, "ip": instruction_pointer})
elif instruction == "end":
op = None
# make else jump directly to end
reference = ip_stack.pop()
ip = reference["ip"]
if reference["type"] == OP_WHILE:
# if end closes a while loop, connect the while to the
# end and the end to the while
program[ip] = (program[ip][0], instruction_pointer + 1)
op = (OP_END, ip)
program.append(op)
else:
# if end closes a if or else, simply connect the if or
# else to the end
program[ip] = (program[ip][0], instruction_pointer + 1)
op = (OP_END, )
program.append(op)
elif instruction == "while":
op = (OP_WHILE, )
ip_stack.append({"type": OP_WHILE, "ip": instruction_pointer})
program.append(op)
elif 'macro:' in instruction:
split = instruction.split(':')
assert len(split) == 2, "OP_MACRO must have name of macro"
macro_name = split[1]
op = (OP_MACRO, macro_name)
program.append(op)
elif instruction == 'ret':
op = (OP_RET, )
program.append(op)
elif 'call:' in instruction:
split = instruction.split(':')
assert len(split) == 2, "OP_CALL must have name of macro to call"
macro_name = split[1]
op = (OP_CALL, macro_name)
program.append(op)
elif instruction == "exit":
op = (OP_EXIT, )
program.append(op)
elif instruction == "=":
op = (OP_EQUAL, )
program.append(op)
elif instruction == ">":
op = (OP_GREATER_THAN, )
program.append(op)
elif instruction == "<":
op = (OP_LESS_THAN, )
program.append(op)
elif instruction == ">=":
op = (OP_GREATER_THAN_OR_EQUAL, )
program.append(op)
elif instruction == "<=":
op = (OP_LESS_THAN_OR_EQUAL, )
program.append(op)
elif instruction == "not":
op = (OP_NOT, )
program.append(op)
elif instruction.startswith("$"):
var_name = instruction[1:]
op = (OP_PUSH_VAR, var_name)
program.append(op)
else:
try:
number = int(instruction)
op = (OP_PUSH, number)
program.append(op)
except ValueError as error:
print(f"Error on line {line_number+1}: Invalid operator {instruction}. Error: {error}. Exiting with code 1")
exit(1)
if advance:
instruction_pointer += 1
return program
if __name__ == '__main__':
filename = sys.argv[1]
args = []
program = lex_program(filename, args)
print(program)