-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_cfg.py
More file actions
33 lines (24 loc) · 789 Bytes
/
Copy pathcompiler_cfg.py
File metadata and controls
33 lines (24 loc) · 789 Bytes
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
class CFGCompiler:
def __init__(self):
self.label_id = 0
def new_label(self):
self.label_id += 1
return f"L{self.label_id}"
def compile(self, ast):
program = []
if isinstance(ast, If):
end_label = self.new_label()
# condition
program.append("LOAD R0 word")
program.append("IN_INDEX R1 R0")
program.append("BLACKLIST R3 R0")
program.append("NOT R3 R3")
# decision gate
program.append("AND R4 R1 R3")
# branch
program.append(f"JMPZ R4 {end_label}")
# body
program.append(f"EXEC {ast.body.name}")
# end
program.append(f"LABEL {end_label}")
return program