-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc.py
More file actions
167 lines (159 loc) · 6.6 KB
/
pc.py
File metadata and controls
167 lines (159 loc) · 6.6 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
#!/usr/local/bin python3.11
# **** PlayCode interpreter ****
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 = """"""
def visitor(tree):
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 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)
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:
tmp = SYMBOL_TABLE[left.children[0]][visitor(left.children[1])]
SYMBOL_TABLE[left.children[0]][visitor(left.children[1])] = SYMBOL_TABLE[right.children[0]][visitor(right.children[1])]
SYMBOL_TABLE[right.children[0]][visitor(right.children[1])] = tmp
elif len(left.children) > 1:
tmp = SYMBOL_TABLE[left.children[0]][visitor(left.children[1])]
SYMBOL_TABLE[left.children[0]][visitor(left.children[1])] = SYMBOL_TABLE[right.children[0]]
SYMBOL_TABLE[right.children[0]] = tmp
elif len(right.children) > 1:
tmp = SYMBOL_TABLE[left.children[0]]
SYMBOL_TABLE[left.children[0]] = SYMBOL_TABLE[right.children[0]][visitor(right.children[1])]
SYMBOL_TABLE[right.children[0]][visitor(right.children[1])] = tmp
else:
tmp = SYMBOL_TABLE[left.children[0]]
SYMBOL_TABLE[left.children[0]] = SYMBOL_TABLE[right.children[0]]
SYMBOL_TABLE[right.children[0]] = tmp
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]))
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":
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
return int(visitor(left)) + int(visitor(right))
case "sub":
left, right = tree.children
return int(visitor(left)) - int(visitor(right))
case "mul":
left, right = tree.children
return int(visitor(left)) * int(visitor(right))
case "div":
left, right = tree.children
return int(visitor(left)) / int(visitor(right))
case "eq":
left, right = tree.children
return int(visitor(left)) == int(visitor(right))
case "neq":
left, right = tree.children
return int(visitor(left)) != int(visitor(right))
case "lt":
left, right = tree.children
return int(visitor(left)) < int(visitor(right))
case "gt":
left, right = tree.children
return int(visitor(left)) > int(visitor(right))
case "vector":
data = []
for branch in tree.children:
data.append(visitor(branch))
return list(data)
case "number":
return int(tree.children[0])
case "identifier":
if len(tree.children) > 1:
return SYMBOL_TABLE[str(tree.children[0])][int(visitor(tree.children[1]))]
else:
return SYMBOL_TABLE[str(tree.children[0])]
case "true":
return True
case "false":
return False
# **** main ****
if (__name__ == "__main__"):
# python3 pc.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 pc.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 = []
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']}")
else:
print(f"{COLORS['fail']}No source file provided{COLORS['end']}")