-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pangea_interpreter.py
More file actions
144 lines (111 loc) · 3.37 KB
/
test_pangea_interpreter.py
File metadata and controls
144 lines (111 loc) · 3.37 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
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""
Test script for the Pangea Python Interpreter
"""
from pangea_python_interpreter import PangeaInterpreter
def test_basic_operations():
"""Test basic operations"""
print("=== Testing Basic Operations ===")
interpreter = PangeaInterpreter()
# Basic arithmetic
interpreter.exec('print 3 + 4')
interpreter.exec('print 10 - 3')
interpreter.exec('print 5 * 6')
interpreter.exec('print 2 ** 3')
# Postfix operator
interpreter.exec('print 5 squared')
# Boolean operations
interpreter.exec('print 5 > 3')
interpreter.exec('print 2 == 2')
def test_control_flow():
"""Test control flow constructs"""
print("\n=== Testing Control Flow ===")
interpreter = PangeaInterpreter()
# If statement
interpreter.exec('if true print "condition+is+true" print "never+printed"')
interpreter.exec('if false print "never+printed" print "condition+is+false"')
# Times loop
interpreter.exec('3 times print "loop+iteration"')
# When (ternary operator)
interpreter.exec('print "yes" when true')
interpreter.exec('print "no" when false')
def test_data_structures():
"""Test arrays and objects"""
print("\n=== Testing Data Structures ===")
interpreter = PangeaInterpreter()
# Arrays
interpreter.exec('print [ 1 2 3 4 5 ]')
# Objects
interpreter.exec('print { "name" "John" "age" 30 "city" "New+York" }')
def test_functions():
"""Test function definitions and calls"""
print("\n=== Testing Functions ===")
interpreter = PangeaInterpreter()
# Simple function
interpreter.exec('''
def greet#1 (
print "Hello,"
print arg 1
)
greet "World"
''')
# Function with return value
interpreter.exec('''
def add_one#1 ( arg 1 ) + 1
print add_one 5
''')
# Recursive function
interpreter.exec('''
def countdown#1 (
print arg 1
if ( arg 1 ) > 0
countdown ( arg 1 ) - 1
print "Done!"
)
countdown 3
''')
def test_complex_example():
"""Test a more complex example"""
print("\n=== Testing Complex Example ===")
interpreter = PangeaInterpreter()
# Fibonacci sequence
interpreter.exec('''
(
def fibonacci#1
if ( arg 1 ) <= 1
arg 1
( fibonacci ( ( arg 1 ) - 1 ) ) + ( fibonacci ( ( arg 1 ) - 2 ) )
print "Fibonacci+sequence:"
10 times (
print fibonacci ( times_count 1 ) - 1
)
)
''')
def interactive_mode():
"""Interactive REPL mode"""
print("\n=== Interactive Mode ===")
print("Enter Pangea code (type 'exit' to quit):")
interpreter = PangeaInterpreter()
while True:
try:
code = input("pangea> ")
if code.strip().lower() == 'exit':
break
if code.strip():
interpreter.exec(code)
except KeyboardInterrupt:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
def main():
"""Main test function"""
test_basic_operations()
test_control_flow()
test_data_structures()
test_functions()
test_complex_example()
# Uncomment the next line to run interactive mode
# interactive_mode()
if __name__ == "__main__":
main()