-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkiran.py
55 lines (48 loc) · 1.93 KB
/
kiran.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
#######################################
# kiran > kiran.py
# Created by Uygur Kiran on 2021/4/11.
#######################################
from src.Lexer import *
from src.Interpreter import *
from src.Context import *
#######################################
# BUILT-IN VALUES AND FUNCS
#######################################
global_symbol_table = SymbolTable()
global_symbol_table.set("NULL", Number.null)
global_symbol_table.set("TRUE", Number.true)
global_symbol_table.set("FALSE", Number.false)
global_symbol_table.set("MATH_PI", Number.math_PI)
global_symbol_table.set("PRINT", BuiltInFunction.print)
global_symbol_table.set("PRINT_RET", BuiltInFunction.print_ret)
global_symbol_table.set("INPUT", BuiltInFunction.input)
global_symbol_table.set("INPUT_INT", BuiltInFunction.input_int)
global_symbol_table.set("CLEAR", BuiltInFunction.clear)
global_symbol_table.set("CLS", BuiltInFunction.clear)
global_symbol_table.set("IS_NUM", BuiltInFunction.is_number)
global_symbol_table.set("IS_STR", BuiltInFunction.is_string)
global_symbol_table.set("IS_LIST", BuiltInFunction.is_list)
global_symbol_table.set("IS_FUNC", BuiltInFunction.is_function)
global_symbol_table.set("APPEND", BuiltInFunction.append)
global_symbol_table.set("POP", BuiltInFunction.pop)
global_symbol_table.set("EXTEND", BuiltInFunction.extend)
global_symbol_table.set("LEN", BuiltInFunction.len)
global_symbol_table.set("RUN", BuiltInFunction.run)
#######################################
# RUN
#######################################
def run(fn, text):
# GET TOKENS
lexer = Lexer(fn, text)
tokens, error = lexer.make_tokens()
if error: return None, error
# GET AST
parser = Parser(tokens)
ast = parser.parse()
if ast.error: return None, ast.error
# RUN PROGRAM
interpreter = Interpreter()
context = Context("<program>")
context.symbol_table = global_symbol_table
result = interpreter.visit(ast.node, context)
return result.value, result.error