-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.py
48 lines (43 loc) · 1.7 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
"""Parts of this code was adopted from https://composingprograms.com/.
The code has been changed according to Postscript syntax.
https://creativecommons.org/licenses/by-sa/3.0/
"""
try:
import readline # history and arrow keys for CLI
except ImportError:
pass # but not everyone has it
import sys
from psparser import read
from psoperators import PSOperators
# program start
if __name__ == '__main__':
"""Run a read-eval-print loop.
`python3 repl.py` to start an interactive REPL using dynamic scoping.
`python3 repl.py --static` to start an interactive REPL using dynamic scoping.
`python3 repl.py --read` to interactively read expressions and print their Python representations.
"""
static = len(sys.argv) >= 2 and ('--static' in sys.argv)
read_only = len(sys.argv) >= 2 and ('--read' in sys.argv)
#create the PostScript stacks
if static:
ps_env = PSOperators("static")
else:
ps_env = PSOperators("dynamic")
while True:
try:
# `input` prints the prompt, waits, and returns the user's input.
user_input = input('<ssps> ')
expr_list = read(user_input)
for expr in expr_list:
if read_only:
print(repr(expr))
else:
expr.eval(ps_env)
ps_env.cleanTop()
#print('opstack ', ps_env.opstack)
#print('dictstack ' , ps_env.dictstack)
except (SyntaxError, NameError, TypeError, Exception) as err:
print(type(err).__name__ + ':', err)
except (KeyboardInterrupt, EOFError): # Ctrl-C, Ctrl-D
print() # blank line
break # exit while loop (and end program)