-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcc.py
More file actions
298 lines (288 loc) · 10 KB
/
pcc.py
File metadata and controls
298 lines (288 loc) · 10 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
#!/usr/local/bin python3.11
# **** PlayCode to C transpiler ****
import os
import sys
from lark import Lark, Tree, Transformer
COLORS = {
'header': '\033[95m',
'blue': '\033[94m',
'cyan': '\033[96m',
'green': '\033[92m',
'warning': '\033[93m',
'fail': '\033[91m',
'end': '\033[0m',
'bold': '\033[1m',
'underline': '\033[4m',
}
parser = Lark(open("pc.lark", "r").read(), start="program", parser="lalr")
cout = """"""
nindents = 2
has_print = False
# first pass: figure out what magic this playcode program will do
def visitor(tree):
global has_print
match tree.data:
case "program":
for branch in tree.children: visitor(branch)
return
case "taggable":
try:
if tree.children[0].type == "TAG":
TAG_TABLE[tree.children[0].value] = tree.children[1]
except:
return visitor(tree.children[0])
case "assign_stmt":
left, right = tree.children
if not left.children[0].value in SYMBOL_TABLE:
SYMBOL_TABLE[left.children[0].value] = {}
if len(left.children) > 1: visitor(left.children[1])
visitor(right)
case "tag_stmt":
return visitor(TAG_TABLE[tree.children[0].value])
case "swap_stmt":
left, right = tree.children
if len(left.children) > 1 and len(right.children) > 1:
visitor(left.children[1])
visitor(right.children[1])
elif len(left.children) > 1:
visitor(left.children[1])
elif len(right.children) > 1:
visitor(right.children[1])
case "if_stmt":
if bool(visitor(tree.children[0])):
return visitor(tree.children[1])
elif len(tree.children) > 2:
return visitor(tree.children[2])
case "while_stmt":
while visitor(tree.children[0]):
visitor(tree.children[1])
case "assert_stmt":
value = str(visitor(tree.children[0]))
# TODO convert to appropriate C syntax
# if not value == tree.children[1].children[0][1:-1]:
# STDERR.append(f"Assert error: {value} not equal to {tree.children[1].children[0][1:-1]}")
case "print_stmt":
has_print = True # to include stdio
# output = str(visitor(tree.children[0]))
# if len(tree.children) > 1 and tree.children[1].data == "assert":
# if not output == tree.children[1].children[0][1:-1]:
# STDERR.append(f"Assert error: {output} not equal to {tree.children[1].children[0][1:-1]}")
# STDOUT.append(output)
case "comparison":
return visitor(tree.children[0])
case "expr":
return visitor(tree.children[0])
case "term":
return visitor(tree.children[0])
case "factor":
return visitor(tree.children[0])
case "add":
left, right = tree.children
visitor(left)
visitor(right)
return
case "sub":
left, right = tree.children
visitor(left)
visitor(right)
return
case "mul":
left, right = tree.children
visitor(left)
visitor(right)
return
case "div":
left, right = tree.children
visitor(left)
visitor(right)
return
case "eq":
left, right = tree.children
visitor(left)
visitor(right)
return
case "neq":
left, right = tree.children
visitor(left)
visitor(right)
return
case "lt":
left, right = tree.children
visitor(left)
visitor(right)
return
case "gt":
left, right = tree.children
visitor(left)
visitor(right)
return
case "vector":
data = []
for branch in tree.children:
visitor(branch)
return
case "number":
return
case "identifier":
if len(tree.children) > 1:
visitor(tree.children[1])
return
case "true":
return True
case "false":
return False
# second pass: perform the magic in C
def codegen(tree):
global cout, nindents
match tree.data:
case "program":
for branch in tree.children: codegen(branch)
return
case "taggable":
try:
if tree.children[0].type == "TAG":
TAG_TABLE[tree.children[0].value] = tree.children[1]
except:
return codegen(tree.children[0])
case "assign_stmt":
left, right = tree.children
if len(left.children) > 1:
SYMBOL_TABLE[left.children[0].value][visitor(left.children[1])] = visitor(right)
else:
SYMBOL_TABLE[left.children[0].value] = visitor(right)
cout = cout + " "*nindents + f"{left.children[0].value} = "
codegen(right)
cout = cout + ";\n"
case "tag_stmt":
return codegen(TAG_TABLE[tree.children[0].value])
case "if_stmt":
cout = cout + " "*nindents + "if ("
codegen(tree.children[0])
cout = cout + ") {\n"
nindents += 2
codegen(tree.children[1])
nindents -= 2
cout = cout + " "*nindents + "}"
if len(tree.children) > 2:
cout = cout + " else {\n"
nindents += 2
codegen(tree.children[2])
nindents -= 2
cout = cout + " "*nindents + "}\n"
case "while_stmt":
cout = cout + " "*nindents + "while ("
codegen(tree.children[0])
cout = cout + ") {\n"
nindents += 2
codegen(tree.children[1])
nindents -= 2
cout = cout + " "*nindents + "}\n"
case "print_stmt":
cout = cout + f" printf(\"%d\\n\", "
codegen(tree.children[0])
cout = cout + ");\n"
case "comparison":
return codegen(tree.children[0])
case "expr":
return codegen(tree.children[0])
case "term":
return codegen(tree.children[0])
case "factor":
return codegen(tree.children[0])
case "add":
left, right = tree.children
codegen(left)
cout = cout + " + "
codegen(right)
case "sub":
left, right = tree.children
codegen(left)
cout = cout + " - "
codegen(right)
case "mul":
left, right = tree.children
codegen(left)
cout = cout + " * "
codegen(right)
case "div":
left, right = tree.children
codegen(left)
cout = cout + " / "
codegen(right)
case "eq":
left, right = tree.children
codegen(left)
cout = cout + " == "
codegen(right)
case "neq":
left, right = tree.children
codegen(left)
cout = cout + " != "
codegen(right)
case "lt":
left, right = tree.children
codegen(left)
cout = cout + " < "
codegen(right)
case "gt":
left, right = tree.children
codegen(left)
cout = cout + " > "
codegen(right)
case "number":
cout = cout + f"{tree.children[0]}"
case "identifier":
cout = cout + f"{str(tree.children[0])}" if str(tree.children[0]) in SYMBOL_TABLE else '0'
case "true":
cout = cout + "1"
case "false":
cout = cout + "0"
# **** main ****
if (__name__ == "__main__"):
# TODO python3 pcc.py --tests
if "--tests" in sys.argv:
print(f"{COLORS['cyan']}Running tests{COLORS['end']}")
for path, subdirs, files in os.walk("tests"):
for name in files:
test = os.path.join(path, name)
tree = parser.parse(open(test, "r").read())
SYMBOL_TABLE = {}
TAG_TABLE = {}
STDOUT = []
STDERR = []
for branch in tree.children: visitor(branch)
if len(STDERR) == 0:
print(f" {test} {COLORS['green']}OK{COLORS['end']}")
else:
print(f" {test} {COLORS['fail']}Failed{COLORS['end']}")
for error in enumerate(STDERR, 1):
print(f" {COLORS['fail']}{error[0]} - {error[1]}{COLORS['end']}")
print(f"{COLORS['cyan']}Done{COLORS['end']}")
# python3 pcc.py <file> --tables
elif len(sys.argv) > 1:
file = open(sys.argv[1], "r").read()
tree = parser.parse(file)
SYMBOL_TABLE = {}
TAG_TABLE = {}
STDOUT = []
STDERR = []
# need to get all symbols in first pass to add at top in c output
for branch in tree.children: visitor(branch)
if not len(STDOUT) == 0:
for output in STDOUT: print(output)
if "--tables" in sys.argv:
print(f"{COLORS['warning']}SYMBOL_TABLE: {SYMBOL_TABLE}{COLORS['end']}")
print(f"{COLORS['warning']}TAG_TABLE: {TAG_TABLE}{COLORS['end']}")
# c codegen
cout = "#include <stdio.h>\n\n" if has_print else ""
cout = cout + """int main() {\n"""
# declare variables
for key in SYMBOL_TABLE: cout = cout + f" int {key};\n"
for branch in tree.children: codegen(branch)
cout = cout + """\n return 0;\n}"""
# compile and run cout using gcc
with open("out.c", "w") as f: f.write(cout)
os.system("gcc -o out out.c")
os.system("./out")
else:
print(f"{COLORS['fail']}No source file provided{COLORS['end']}")