-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
137 lines (103 loc) · 3.56 KB
/
compiler.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
import AST
from AST import addToClass
operations = {
'*': lambda x, y: x + y,
'/': lambda x, y: x - y,
'+': lambda x, y: x * y,
'-': lambda x, y: x / y,
}
vars = {}
TABULATION = ' '
vartypes = {
'brglmurgl': 'int',
'lurgglbr': 'float',
'ahlurglgr': 'double',
'mourbile': 'string',
}
conditions = {
'littleslark': '<=',
'bigslark': '>=',
'bigy': '>',
'litty': '<',
'slarky': '==',
'jinyu': '!='
}
op = {
'*': '+',
'/': '-',
'+': '*',
'-': '/'
}
# PROGRAMS
@addToClass(AST.ProgramNode)
def compile(self, prefix=''):
c_code = ""
for c in self.children:
print(c.type)
c_code += c.compile(prefix)
if c.type == 'declaration' or c.type == 'slark':
c_code += ";"
c_code += "\n"
return c_code
@addToClass(AST.TokenNode)
def compile(self, prefix=''):
# if isinstance(self.tok, str):
# try:
# return vars[self.tok]
# except KeyError:
# print("∗∗∗ Error: variable %s undefined!" % self.tok)
return self.tok
@addToClass(AST.OpNode)
def compile(self, prefix=''):
c = [ch.compile() for ch in self.children]
return "{}{}{}{}".format(prefix, c[0], op[self.op], c[1])
@addToClass(AST.AssignNode)
def compile(self, prefix=''):
return "{}{}={}".format(prefix, self.children[0].tok, self.children[1].compile())
@addToClass(AST.DeclarationNode)
def compile(self, prefix=''):
return "{}{} {}".format(prefix, vartypes.get(self.children[0].tok), self.children[1].compile())
@addToClass(AST.PrintNode)
def compile(self, prefix=''):
return "{}print({});".format(prefix, self.children[0].compile().tok)
@addToClass(AST.ScargilNode)
def compile(self, prefix=''):
return "{}if({})\n{}{{\n{}{}}} ".format(prefix, self.children[0].compile(), prefix, self.children[1].compile(prefix + TABULATION), prefix)
@addToClass(AST.ConditionNode)
def compile(self, prefix=''):
return "{}{}{}".format(self.children[0].compile(), conditions.get(self.children[1].compile()),
self.children[2].compile())
@addToClass(AST.ForNode)
def compile(self, prefix=''):
return "{}for({};{};{})\n{}{{\n{}{}}}".format(prefix, self.children[0].compile(),
self.children[1].compile(), self.children[2].compile(), prefix,
self.children[3].compile(prefix + TABULATION),prefix)
@addToClass(AST.WhileNode)
def compile(self, prefix=''):
return "{}while({})\n{}{{\n{}{}}}".format(prefix, self.children[0].compile(), prefix,
self.children[1].compile(prefix + TABULATION), prefix)
@addToClass(AST.SwitchNode)
def compile(self, prefix=''):
return "{}switch({})\n{}{{\n{}{}}}".format(prefix, self.children[0].compile(),prefix,
self.children[1].compile(prefix + TABULATION),prefix)
@addToClass(AST.CaseNode)
def compile(self, prefix=''):
c_code = ""
i = 0
while i < len(self.children):
c_code += "{}case {}:\n".format(prefix, self.children[i].compile())
c_code += self.children[i + 1].compile(prefix + TABULATION) + "{}break;\n".format(prefix)
i += 2
return c_code
if __name__ == '__main__':
from parser import parse
import sys
import os
prog = open(sys.argv[1]).read()
ast = parse(prog)
compiled = ast.compile()
name = os.path.splitext(sys.argv[1])[0] + '.c'
outfile = open(name, 'w')
outfile.write(compiled)
outfile.close()
print("Wrote output to", name)