-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.py
77 lines (62 loc) · 1.54 KB
/
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
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
'''
TODO:
* syntax checking (paren count, etc)
* in parse or in get_expr?
* argparse, argv, cli for flags?
'''
from parse import parse
from reg import fetch, assign, EXPR, VAL
from reg import clear_registers
from stack import clear_stack
from mem import clear_memory
from env import initialize_env
from run import run
from stats import display_stats
INTERPRETER_PROMPT = '<<< '
INTERPRETER_EXIT = '.quit', '.exit'
EXIT_MESSAGE = 'Byeeeeeeee!'
def repl():
info_flag = 0
stats_flag = 1
initialize()
while True:
try:
get_expr()
run(info_flag=info_flag)
display_result(stats_flag=stats_flag)
except KeyboardInterrupt:
print()
break
# except Exception as e: # better way to do this?
# print(e)
def ecio_eval(expr):
'''Evaluates an expression without invoking the repl'''
initialize()
parse_and_set_expr(expr)
run()
return get_result()
def initialize():
# optional
clear_registers()
clear_stack()
clear_memory()
# required
initialize_env()
def get_expr():
expr = input(INTERPRETER_PROMPT)
if expr in INTERPRETER_EXIT:
raise Exception(EXIT_MESSAGE)
else:
parse_and_set_expr(expr)
def display_result(stats_flag=1):
print(get_result())
print()
display_stats(stats_flag)
print()
def parse_and_set_expr(lisp_expr):
parsed = parse(lisp_expr)
assign(EXPR, parsed)
def get_result():
return fetch(VAL)
if __name__ == '__main__':
repl()