-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheme.py
44 lines (38 loc) · 1.03 KB
/
scheme.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
## a subset of scheme
from parse import parse, Tokenizer
from env import makeglobalenv
from primitives import islist, isbool
from eval import eval
try:
input = raw_input
except NameError:
pass
def tostring(a):
if islist(a):
return '(' + ' '.join(map(str, a)) + ')'
elif isbool(a):
return '#t' if a else '#f'
else:
return str(a)
def REPL():
globalenv = makeglobalenv()
while True:
try:
inp = input('* ')
while True:
try:
sexp = parse(inp)
break
except SyntaxError as e:
if e.msg == 'Unexpected end of token stream':
inp += ' ' + input(' ')
else:
raise e
print(tostring(eval(sexp, globalenv)))
except (KeyboardInterrupt, EOFError):
print("Exiting... Bye!")
return
except Exception as e:
print(str(e))
if __name__ == '__main__':
REPL()