-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.py
More file actions
37 lines (26 loc) · 1 KB
/
compiler.py
File metadata and controls
37 lines (26 loc) · 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
from ast_nodes import *
class Compiler:
def compile(self, ast):
condition, action = ast
program = []
program.append("LOAD R0 word")
self.compile_condition(condition, program)
# default decision register
program.append("OR R4 R1 R3")
program.append("FETCH R5 R0")
return program
def compile_condition(self, node, program):
if isinstance(node, Condition):
if node.op == "in":
program.append("IN_INDEX R1 R0")
elif isinstance(node, Not):
program.append("BLACKLIST R3 R0")
program.append("NOT R3 R3")
elif isinstance(node, And):
self.compile_condition(node.left, program)
self.compile_condition(node.right, program)
program.append("AND R4 R1 R3")
elif isinstance(node, Or):
self.compile_condition(node.left, program)
self.compile_condition(node.right, program)
program.append("OR R4 R1 R3")