-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.py
41 lines (29 loc) · 914 Bytes
/
repl.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 parser.parser import parse
from interpreter.interpreter import interpret
from cmd import Cmd
class Prompt(Cmd):
prompt = '>>> '
# intro = "Welcome to the haellang repl!"
def init(self):
self.assignment_store = dict()
def do_exit(self, _):
exit(0)
def default(self, statement):
try:
self.assignment_store = execute_statement(statement, self.assignment_store)
except Exception as e:
print(e)
do_EOF = do_exit
def execute_statement(statement:str, assignment_store:dict):
ast = parse(statement)
value, new_assignment_store = interpret(ast, assignment_store)
print(f'- {value}')
return new_assignment_store
def repl():
prompt = Prompt()
prompt.init()
while True:
try:
prompt.cmdloop()
except KeyboardInterrupt as k:
print('\nKeyboardInterrupt')