-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_utils.py
35 lines (28 loc) · 1.21 KB
/
main_utils.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
from interpreter.evaluator.environment_ import Environment
from interpreter.evaluator.evaluator import Evaluator
from interpreter.parser_.ast_objects import Error, Expression
from interpreter.parser_.parser_ import Parser
from interpreter.tokens.token_queue import TokenQueue
from interpreter.tokens.tokenizer import Tokenizer
from utils.ast_visualizer import ASTVisualizer
from utils.utils import LanguageRuntimeException
def evaluate(source: str, environment: Environment) -> tuple[list[Expression], list[str]]:
"""Execute code in a file.
Unlike REPL, this execution style does not use the results of each individual expression.
"""
try:
t = Tokenizer(source)
tokens = TokenQueue(t)
p = Parser(tokens)
ast = p.parse()
return Evaluator(ast, environment).evaluate()
except LanguageRuntimeException as e:
# This catch is needed for the parser and tokenizer. Evaluator.evaluate handles these errors on its own.
error_object = Error(e.line_num, str(e))
return [str(error_object)], []
def visualize_ast(source: str) -> bytes:
t = Tokenizer(source)
tq = TokenQueue(t)
p = Parser(tq)
ast = p.parse()
return ASTVisualizer(ast).visualize()