-
Notifications
You must be signed in to change notification settings - Fork 0
/
grass.py
44 lines (29 loc) · 883 Bytes
/
grass.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
from lexerMod import *
from parserMod import *
from interpreterMod import *
global_symbol_table = Symbols()
global_symbol_table.set_var(K_NULL,Number(0))
global_symbol_table.set_var(K_TRUE,Number(1))
global_symbol_table.set_var(K_FALSE,Number(0))
def run(text,filename):
#tokens
lexer = Lexer(text,filename)
tokens,error = lexer.tokeniser()
if error:
return error.stringfy()
print(tokens)
#parser
parser = Parser(tokens)
ast = parser.parse()
if ast.error:
return ast.error.stringfy()
print(ast.node,"ast nodes")
interpreter = Interpreter()
context = Context('<program>')
context.symbol_table = global_symbol_table
result = interpreter.visit(ast.node,context)
if result.error:
return result.error.stringfy()
if result :
return result.value
#interpreter