-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·71 lines (53 loc) · 1.29 KB
/
main.py
File metadata and controls
executable file
·71 lines (53 loc) · 1.29 KB
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
#!/usr/bin/env python3
import sys
from typing import Dict
from lark.exceptions import UnexpectedInput
from cas.model import Node, Assignment
from cas.parser import parse
from evaluate import evaluate, UnboundVariableError
from simplify import simplify
# use equation syntax for variable binding
# todo https://docs.python.org/3/library/readline.html
# or maybe: https://github.com/Textualize/textual
def main():
ctx: Dict[str, Node] = dict()
while 1:
command = None
text = input('> ')
if text == '':
continue
if text.startswith('simp '):
command = 'simp'
text = text.replace('simp ', '')
elif text.startswith('raw '):
command = 'raw'
text = text.replace('raw ', '')
elif text == 'vars':
print(ctx)
continue
try:
expr = parse(text)
except UnexpectedInput as e:
print('Error: bad expression')
print(e)
continue
if type(expr) == Assignment:
ctx[expr.lhs.name] = expr.rhs
continue
try:
ret = None
if command is None:
ret = evaluate(expr, ctx)
elif command == 'simp':
ret = simplify(expr)
elif command == 'raw':
ret = expr
except UnboundVariableError:
print('Error: unbound variable exception')
continue
print(ret)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, EOFError):
sys.exit()