-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeTranslate.py
More file actions
309 lines (257 loc) · 10.1 KB
/
Copy pathcodeTranslate.py
File metadata and controls
309 lines (257 loc) · 10.1 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
central_index = 0
def get_index():
global central_index
central_index += 1
return central_index - 1
class NamespaceLayer(object):
def __init__(self, name: str):
self.name = name
self.variables = []
class Namespace(object):
def __init__(self):
self.layers = [
NamespaceLayer('root')
]
def copy(self):
new = Namespace()
new.layers = self.layers.copy()
return new
def add_layer(self, name: str):
self.layers.append(NamespaceLayer(name))
return self
def pop_layer(self):
self.layers.pop()
return self
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
def __str__(self):
return self.cmd
def translate(self, namespace: Namespace):
return []
Program = list[Command]
class Variable(object):
def __init__(self, name: str):
self.name = name
def __str__(self):
return self.name
class Value(object):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
class Comparator(object):
def __init__(self, comparator: str):
if comparator not in ['<', '>', '==', '!=', '<=', '>=']:
raise Exception(f'comparator "{comparator}" is not a valid comparator')
self.comparator = comparator
def __str__(self):
return self.comparator
Instance = Variable | Value
Condition = tuple[Instance, Comparator, Instance]
class Nop(Command):
def __init__(self):
super().__init__('nop')
def translate(self, namespace: Namespace):
return []
class Operator(object):
def __init__(self, operator: str):
if operator not in ['+', '-', '*', '/', '%', '^']:
raise Exception(f'operator "{operator}" is not a valid operator')
self.operator = operator
def __str__(self):
return self.operator
class DynamiclyAssignedVariable(Variable):
def __init__(self, name: str, pointer_addr: int):
super().__init__(name)
self.pointer_addr = pointer_addr
self.pointer_name = f'var_dyn_{name}_{get_index()}'
def __str__(self):
return print(f'<DynamiclyAssignedVariable name={self.name} pointer_addr={self.pointer_addr} pointer_name={self.pointer_name}>')
class StaticlyAssignedVariable(Variable):
def __init__(self, name: str, addr: int):
super().__init__(name)
self.addr = addr
self.pointer_name = f'var_dyn_{name}_{get_index()}'
def __str__(self):
return print(f'<StaticlyAssignedVariable name={self.name} addr={self.addr} pointer_name={self.pointer_name}>')
class Function(object):
def __init__(self, name, inputs, code):
self.name: str = name
self.inputs: list[Variable] = inputs
self.code: Program = code
def __str__(self):
return f'<Function name={self.name} inputs={self.inputs} code=\n{self.code}\n>'
class Branch(Command):
def __init__(self, condition_code_pairs):
super().__init__('branch')
self.condition_code_pairs: dict[Condition, Program] = condition_code_pairs
def __str__(self):
return f'<Branch condition_code_pairs={self.condition_code_pairs}>'
class WhileLoop(Command):
def __init__(self, condition: Condition, code: Program):
super().__init__('while')
self.init = Nop()
self.condition: Condition = condition
self.code: Program = code
def __str__(self):
return f'<WhileLoop condition={self.condition} code=\n{self.code}\n>'
def translate(self, namespace: Namespace):
self.break_jump = f'while_break_{get_index()}'
self.start_jump = f'while_start_{get_index()}'
out = []
out += self.init.translate(namespace)
out.append(f'JMIF {self.condition.translate(namespace)} ~{self.start_jump}')
out.append(f'JUMP ~{self.break_jump}')
out.append(f'{self.start_jump}:')
out += [i.translate(namespace) for i in self.code]
out.append(f'JUMP ~{self.start_jump}')
out.append(f'{self.break_jump}:')
return out
class ForLoop(WhileLoop):
def __init__(self, init: Command, condition: Condition, step: Command, code: Program):
super().__init__(condition, code)
self.init: Command = init
self.code.append(step)
def __str__(self):
return f'<ForLoop init={self.init} condition={self.condition} code=\n{self.code}\n>'
class Print(Command):
def __init__(self, value: Instance):
super().__init__('print')
self.value: Instance = value
def __str__(self):
return f'<Print value={self.value}>'
class Assignment(Command):
def __init__(self, variable: Variable, value: Instance):
super().__init__('assign')
self.variable: Variable = variable
self.value: Instance = value
def __str__(self):
return f'<Assignment variable={self.variable} value={self.value}>'
class Modification(Command):
def __init__(self, variable: Variable, operator: Operator, value: Instance):
super().__init__('modify')
self.variable: Variable = variable
self.operator: Operator = operator
self.value: Instance = value
def __str__(self):
return f'<Modification variable={self.variable} operator={self.operator} value={self.value}>'
class Definition(Command):
def __init__(self, variable: Variable, value: Instance):
super().__init__('define')
self.variable: Variable = variable
self.value: Instance = value
def __str__(self):
return f'<Definition variable={self.variable} value={self.value}>'
class Return(Command):
def __init__(self, value: Instance):
super().__init__('return')
self.value: Instance = value
def __str__(self):
return f'<Return value={self.value}>'
class Continue(Command):
def __init__(self):
super().__init__('continue')
class Break(Command):
def __init__(self):
super().__init__('break')
class FunctionCall(Command):
def __init__(self, name: str, inputs: list[Instance]):
super().__init__('call')
self.name: str = name
self.inputs: list[Instance] = inputs
def __str__(self):
return f'<FunctionCall name={self.name} inputs={self.inputs}>'
class CodePiecer:
@staticmethod
def piece(code: list[object], namespace: Namespace=None):
out = []
if namespace is None:
namespace = Namespace()
for line in code:
out.append(CodePiecer.make_command(line, namespace))
return out
@staticmethod
def make_command(line, namespace: Namespace):
command = {}
if line[0] == 'for':
if len(line) != 3:
raise Exception(f"for statement {line} does not have: 'for', '(init, condition, increment)', 'code'")
if len(line[1]) != 3:
raise Exception(f"for statement {line} does not have: 'init', 'condition', 'increment'")
command = {
'type': 'for',
'init': CodePiecer.make_command(line[1][0], namespace),
'condition': line[1][1],
'increment': CodePiecer.make_command(line[1][2], namespace),
'code': CodePiecer.piece(line[2], namespace)
}
elif line[0] == 'if':
if len(line) != 3:
raise Exception(f"if statement {line} does not have: 'if', 'condition', 'code'")
command = {
'type': 'if',
'condition': line[1],
'code': CodePiecer.piece(line[2]),
'else': None
}
elif line[0] == 'elif':
if len(line) != 3:
raise Exception(f"elif statement {line} does not have: 'elif', 'condition', 'code'")
command = {
'type': 'elif',
'condition': line[1],
'code': CodePiecer.piece(line[2]),
'else': None
}
elif line[0] == 'else':
if len(line) != 2:
raise Exception(f"else statement {line} does not have: 'else', 'code'")
command = {
'type': 'else',
'code': CodePiecer.piece(line[1]),
}
elif line[0] == 'while':
if len(line) != 3:
raise Exception(f"while loop {line} does not have: 'while', 'condition', 'code'")
command = {
'type': 'while',
'condition': line[1],
'code': CodePiecer.piece(line[2])
}
elif line[0] == 'do':
if len(line) != 4:
raise Exception(f"do while loop {line} does not have: 'do', 'condition', 'code', 'while'")
command = {
'type': 'dowhile',
'code': CodePiecer.piece(line[2]),
'condition': line[2]
}
elif line[0] == 'print':
if len(line) != 2:
raise Exception(f"print statement {line} does not have: 'print', 'expression'")
command = {
'type': 'print',
'expression': line[1]
}
elif line[0] == 'func':
if len(line) != 4:
raise Exception(f"function {line} does not have: 'func', 'name', 'args', 'code'")
code = CodePiecer.piece(line[3], namespace.copy().add_layer(line[1]))
command = Function(line[1], line[2], code)
elif line[1] in ['=', '+=', '-=', '*=', '/=', ':=']:
if len(line) != 3:
raise Exception(f"statement {line} does not have: 'var', 'operator', 'expression'")
print(line)
command = {
'type': line[1],
'var': line[0], # idk what to call this. TODO: help me name this
'expression': line[2]
}
elif line[1] in ['++', '--']:
if len(line) != 2:
raise Exception(f"statement {line} does not have: 'var', 'operator'")
command = str(Modification(line[0], line[1][0], Value(1)))
else:
raise Exception(f"could not find command {line}")
return command