-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.py
79 lines (71 loc) · 2.29 KB
/
interpreter.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
from dataclasses import dataclass
from fractions import Fraction
from typing import Union, Mapping
import os
from core import RuntimeEnvironment
from utils.datatypes import (
AST,
NumLiteral,
BinOp,
Variable,
Value,
Let,
If,
BoolLiteral,
UnOp,
ASTSequence,
Declare,
)
from utils.visualizer import ASTViz
from stream import Stream, Lexer, Parser
from utils.colors import GREEN, BOLD, RESET, RED, BLACK, YELLOW, BLUE, INVERSE, BRIGHT_INVERSE
from utils.errors import InvalidTokenError, TokenError
def interpret(feedback=False, visualize=False):
runtime = RuntimeEnvironment()
persist = False
while True:
try:
if not persist:
line = input(f"{GREEN}{BOLD}gossip{RESET} >>> ")
else:
line += input(f"{GREEN}{BOLD}gossip{RESET} ... ... ")
if line == "exit":
break
if line == "clear":
os.system("clear")
continue
if line == "":
continue
persist = True if line[-1] == "{" else False if line[-2:] == "};" else persist
L = Lexer.from_stream(Stream.from_string(line))
L_ = Lexer.from_stream(Stream.from_string(line))
S = Parser.from_lexer(L)
S_ = Parser.from_lexer(L_)
assert len([s for s in S_]) != 0 or persist
except (AssertionError, TokenError, ) as e:
print(f"{RED}InvalidTokenError{RESET}: Invalid token encountered.")
pass
try:
for s in S:
try:
if visualize:
vis = ASTViz(depth=0, code=line)
vis.treebuilder(s)
if feedback:
if not persist:
print(f"{RED}{runtime.eval(s)}{RESET}")
else:
if not persist:
runtime.eval(s)
except:
continue
except Exception as e:
print(e)
def compile_gossip(lines):
runtime = RuntimeEnvironment()
for line in lines:
L = Lexer.from_stream(Stream.from_string(line))
S = Parser.from_lexer(L)
for s in S:
x = runtime.eval(s)
print(x)