-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzApp.py
125 lines (104 loc) · 3.71 KB
/
zApp.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
import streamlit as st
import os
import cmp.visitor as visitor
from TypeCollectorBuilder import TypeBuilder, TypeCollector
from TypeChecker import TypeChecker
from Grammar import G, lexer, ocur, ccur, semi
from Utils import FormatVisitor
from cmp.evaluation import evaluate_reverse_parse
from cmp.tools.LR1_Parser 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 pprint_tokens(tokens):
indent = 0
pending = []
s = ""
for token in tokens:
pending.append(token)
if token.token_type in { ocur, ccur, semi }:
if token.token_type == ccur:
indent -= 1
s += (' '*indent + ' '.join(str(t.token_type) for t in pending))
s += "\n"
pending.clear()
if token.token_type == ocur:
indent += 1
s += (' '.join([str(t.token_type) for t in pending]))
return s
st.title("Cool Language")
sb = st.selectbox("Choose where the file is going to be loaded or written:", ["Import File", "Raw Input"],)
import_file = True if sb == "Import File" else False
st.sidebar.title("Options")
showProgram = st.sidebar.checkbox("Show Program", False)
showTokens = st.sidebar.checkbox("Show Tokenization", False)
showParsing = st.sidebar.checkbox("Show Parsing")
showAST = st.sidebar.checkbox("Show AST")
showTypesCollected = st.sidebar.checkbox("Show Types Collected")
showTypesBuilded = st.sidebar.checkbox("Show Types Builded")
local_name = ""
if import_file:
st.write("Introduce File's FOLDER Location")
ti = st.text_input(r"Introduce Folder's Adress (Default: .\CoolPrograms)")
ti = r".\CoolPrograms" if ti == "" else ti
filename, local_name = file_selector(ti)
st.text("You selected: " + filename)
file1 = open(filename, "r")
program = file1.read()
file1.close()
else:
program = st.text_area("Write Program:")
###---------------RUN PIPELINE-----------------###
if st.button("Submit"):
st.write("Executing Program", local_name)
def run_pipeline(program):
if showProgram:
st.text(program)
tokens = lexer(program)
if showTokens:
st.write("Tokenizing")
st.text(pprint_tokens(tokens))
parser = LR1Parser(G)
parse, operations, success = parser(tokens)
if showParsing:
st.write("Parsing")
st.text("\n".join(repr(x) for x in parse))
if not success:
st.text(parse)
return
ast = evaluate_reverse_parse(parse, operations, tokens)
formatter = FormatVisitor()
tree = formatter.visit(ast)
if showAST:
st.write("Building AST")
st.text(tree)
errors = []
collector = TypeCollector(errors)
collector.visit(ast)
context = collector.context
if errors == []:
st.success("Collecting Types")
else:
st.error("Collecting Types")
if showTypesCollected:
st.text("Context:")
st.text(context)
builder = TypeBuilder(context, errors)
builder.visit(ast)
if errors == []:
st.success("Building Types")
else:
st.error("Building Types")
if showTypesBuilded:
st.text("Context")
st.text(context)
checker = TypeChecker(context, errors)
scope = checker.visit(ast)
if errors == []:
st.success("Checking Types")
else:
st.error("Checking Types")
st.text("Errors")
st.text(errors)
run_pipeline(program)