-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
50 lines (40 loc) · 892 Bytes
/
run.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
import sys
import lexer
import parser
import state
prompt = "> "
exit = "exit"
grammar = parser.parseGrammar("spec.txt")
def lexandparse(string):
statement_stream = lexer.lex(string)
for statement in statement_stream:
statement = lexer.assignNameSpecs(statement, state.names)
if statement != None:
ptree = parser.parse(statement, grammar)
if ptree != None:
ptree = parser.massage(ptree, grammar, lexer.punctuation)
output = state.evaluate(ptree)
if output != None:
if type(output) == type([]):
for i in output:
print(i)
else:
print(output)
def repl():
while True:
inp = input(prompt)
if inp == exit:
break
else:
lexandparse(inp)
def main():
if len(sys.argv) > 1:
f = open(sys.argv[0],'r')
lexandparse(f.read())
f.close()
if len(sys.argv) != 1:
repl()
else:
repl()
if __name__ == "__main__":
main()