-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebScript.py
87 lines (67 loc) · 2.14 KB
/
DebScript.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
import os
import cmp.visitor as visitor
import re
from TypeCollectorBuilder import TypeBuilder, TypeCollector
from TypeChecker import TypeChecker
from Grammar import G, pprint_tokens, lexer
from Utils import FormatVisitor
from cmp.evaluation import evaluate_reverse_parse
from cmp.tools.LR1_Parser import LR1Parser
#from cmp.tools.parsing import LR1Parser
#def file_selector(folder_path="."):
#filenames = os.listdir(folder_path)
#selected_filename = st.selectbox("Select a file", filenames)
#return os.path.join(folder_path, selected_filename), selected_filename
def run_pipeline(G, program):
print("Executing Program")
#print(program)
#toks = regex.findall(program)
tokens = lexer(program)
#tokens = tokenize_text(toks)
print("Tokens")
pprint_tokens(tokens)
parser = LR1Parser(G)
#parse, operations = parser([t.token_type for t in tokens])
parse, operations, result = parser(tokens)
#print("\n".join(repr(x) for x in parse))
print(parse)
if not result:
return
ast = evaluate_reverse_parse(parse, operations, tokens)
formatter = FormatVisitor()
tree = formatter.visit(ast)
print(tree)
errors = []
errors.append("TypeCollector Errors:")
collector = TypeCollector(errors)
collector.visit(ast)
context = collector.context
print("Context\n", context)
errors.append("TypeBuilder Errors:")
builder = TypeBuilder(context, errors)
builder.visit(ast)
print("Context\n", context)
errors.append("TypeChecker Errors:")
checker = TypeChecker(context, errors)
scope = checker.visit(ast)
for err in errors:
print(err)
print(len(errors) - 3)
filename = r".\CoolPrograms\life.cl"
#filename = r".\CoolPrograms\0Simple.txt"
print("Loading " + filename)
file1 = open(filename, "r")
program = file1.read()
file1.close()
run_pipeline(G, program)
from cmp.pycompiler import Grammar
H = Grammar()
A = H.NonTerminal("A", True)
B, C, D = H.NonTerminals("B C D")
x, y, z = H.Terminals("x y z")
#A %= y + A + x + A + z + A
#A %= y + A + x + A + z
A %= B
B %= y + B + z + B + x + B
#parser = LR1Parser(G)
#print("Done!")