-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
36 lines (29 loc) · 859 Bytes
/
cli.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
import readline
import atexit
from Interpreter import Interpreter
from os import environ
def setup():
readline.parse_and_bind('tab: complete')
history = ".calcli_history"
if environ.get('HOME') is not None:
history = f"{environ.get('HOME')}/{history}"
elif environ.get('USERPROFILE') is not None:
history = f"{environ.get('USERPROFILE')}\\{history}"
try:
readline.read_history_file(history)
except FileNotFoundError:
pass
atexit.register(readline.write_history_file, history)
def read(prompt=""):
return input(prompt)
setup()
try:
interpreter: Interpreter = Interpreter()
while True:
expr = read("/>> ")
r = interpreter.run(expr)
print(repr(r))if r is not None else None
except KeyboardInterrupt:
print('Ctrl-C')
except EOFError:
print('Tchau ;)')