-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_complete.py
More file actions
133 lines (104 loc) · 3.19 KB
/
Copy pathtest_complete.py
File metadata and controls
133 lines (104 loc) · 3.19 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from parser import parse
from interpreter import interpret, Interpreter
def test_complete_pipeline():
test_cases = [
# Basic Arithmetic
("2 + 3", 5),
("10 - 4", 6),
("3 * 4", 12),
("2 + 3 * 4", 14),
("(2 + 3) * 4", 20),
("x = 5", 5),
("5 > 3", True),
("2 < 1", False),
("-5", -5),
("-(3 + 2)", -5)
]
print("Pipeline Testing!")
passed = 0
failed = 0
for code, expected in test_cases:
print(f"\nTest: {code}")
try:
result = interpret(code)
print(f"Expected: {expected}")
print(f"Got: {result}")
if result == expected:
print("PASSED")
passed += 1
else:
print("FAILED - Wrong result")
failed += 1
except Exception as e:
print(f"FAILED - Error: {e}")
failed += 1
print("\nResults:")
print(f"Passed: {passed}")
print(f"Failed: {failed}")
print(f"Success rate: {passed}/{passed + failed}")
def test_variable_persistence():
print("\nVariable Test")
passed = 0
failed = 0
interpreter = Interpreter()
test_sequence = [
("x = 10", 10),
("y = 5", 5),
("x + y", 15),
("x * y", 50),
("result = x + y * 2", 20)
]
for code, expected in test_sequence:
print(f"\nExecuting: {code}")
try:
ast = parse(code)
result = interpreter.visit(ast)
print(f"Result: {result}")
print(f"Variables: {interpreter.variables}")
if result == expected:
print("PASSED")
passed +=1
else:
print(f"FAILED - Expected {expected}, got {result}")
failed += 1
except Exception as e:
print(f"ERROR: {e}")
failed += 1
print("Results:")
print(f"Passed: {passed}")
print(f"Failed: {failed}")
print(f"Success Rate: {passed}/{passed+failed}")
def interactive_interpreter():
print("\n Interactive")
print("Enter expressions or statements (or 'quit' to exit):")
print("Examples:")
print(" x = 5")
print(" y = x + 3")
print(" x * y")
print("-" * 50)
interpreter = Interpreter()
while True:
try:
user_input = input(">>> ").strip()
if user_input.lower() in ["quit", "exit", "q"]:
print("BYE!")
break
if not user_input:
continue
ast = parse(user_input)
result = interpreter.visit(ast)
if result is not None:
print(result)
if interpreter.variables:
print(f"Variables: {interpreter.variables}")
except KeyboardInterrupt:
print("\nGoodbye!")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
test_complete_pipeline()
test_variable_persistence()
print("\n" + "=" * 60)
response = input("Want to try the interactive interpreter? (y/n): ").lower()
if response.startswith("y"):
interactive_interpreter()