forked from steam-bell-92/python-mini-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (79 loc) · 2.83 KB
/
Copy pathmain.py
File metadata and controls
98 lines (79 loc) · 2.83 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Interactive CLI REPL and runner for Mathematical Expression Parser & Evaluator.
"""
import sys
import os
# Ensure project root is in sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from typing import Dict, Union
from expression_parser import evaluate_expression, TokenizerError, ParseError, EvaluationError
def run_repl():
print("=" * 60)
print(" Mathematical Expression Parser & Evaluator (CLI REPL)")
print("=" * 60)
print("Commands:")
print(" exit / quit : Exit REPL")
print(" vars : Show current active variables")
print(" let x = val : Set variable value (e.g. let x = 10)")
print(" clear : Reset custom variables")
print("-" * 60)
user_vars: Dict[str, Union[int, float]] = {}
while True:
try:
inp = input("math> ").strip()
except (KeyboardInterrupt, EOFError):
print("\nExiting REPL. Goodbye!")
break
if not inp:
continue
if inp.lower() in ("exit", "quit"):
print("Goodbye!")
break
if inp.lower() == "vars":
if not user_vars:
print("No custom variables set. Builtins available: pi, e, tau.")
else:
print("Active variables:", user_vars)
continue
if inp.lower() == "clear":
user_vars.clear()
print("Custom variables cleared.")
continue
if inp.lower().startswith("let "):
parts = inp[4:].split("=", 1)
if len(parts) == 2:
var_name = parts[0].strip()
val_expr = parts[1].strip()
if not var_name.isidentifier():
print(f"Error: Invalid variable name '{var_name}'")
continue
try:
val = evaluate_expression(val_expr, user_vars)
user_vars[var_name] = val
print(f"Set {var_name} = {val}")
except Exception as e:
print(f"Error evaluating variable value: {e}")
continue
else:
print("Usage for assignment: let <var_name> = <expression>")
continue
try:
result = evaluate_expression(inp, user_vars)
print(f"Result: {result}")
except (TokenizerError, ParseError, EvaluationError) as e:
print(f"Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
def main():
if len(sys.argv) > 1:
expr = " ".join(sys.argv[1:])
try:
res = evaluate_expression(expr)
print(res)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
else:
run_repl()
if __name__ == "__main__":
main()