-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpangea_cli.py
More file actions
executable file
·187 lines (142 loc) · 4.38 KB
/
Copy pathpangea_cli.py
File metadata and controls
executable file
·187 lines (142 loc) · 4.38 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""
Pangea REPL - Interactive command line interface for the Pangea interpreter
"""
import sys
import argparse
from pangea_python_interpreter import PangeaInterpreter
def run_file(filename):
"""Run a Pangea file"""
try:
with open(filename, 'r') as f:
code = f.read()
interpreter = PangeaInterpreter()
interpreter.exec(code)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
sys.exit(1)
except Exception as e:
print(f"Error executing file: {e}")
sys.exit(1)
def run_repl():
"""Run interactive REPL"""
print("Pangea Python Interpreter REPL")
print("Type 'help' for help, 'exit' to quit")
print("=" * 40)
interpreter = PangeaInterpreter()
while True:
try:
code = input("pangea> ").strip()
if not code:
continue
if code.lower() in ['exit', 'quit']:
print("Goodbye!")
break
if code.lower() == 'help':
print_help()
continue
if code.lower() == 'reset':
interpreter = PangeaInterpreter()
print("Interpreter reset.")
continue
if code.lower() == 'examples':
show_examples()
continue
# Execute the code
interpreter.exec(code)
except KeyboardInterrupt:
print("\nGoodbye!")
break
except EOFError:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
def print_help():
"""Print help information"""
help_text = """
Pangea Language Help
===================
Basic Commands:
help - Show this help
exit/quit - Exit the interpreter
reset - Reset interpreter state
examples - Show example code
Basic Syntax:
print "hello" - Print a string
print 2 + 3 - Print arithmetic result
5 times print "hi" - Repeat action
def func#1 print arg 1 - Define function
func "test" - Call function
Data Types:
Numbers: 42, 3.14, -5
Strings: "hello", "hello+world"
Arrays: [ 1 2 3 ]
Objects: { "key" "value" }
Control Flow:
if condition then else
times count block
when condition value
unless condition block
Functions:
def name#arity body - Define function
arg n - Get nth argument (1-indexed)
Built-in Functions:
print, times, if, when, unless, def, arg
each, each_item, each_key, each_break
Mathematical: +, -, *, **, %, ==, <, >, <=, squared
"""
print(help_text)
def show_examples():
"""Show example code"""
examples = """
Example Code
============
1. Hello World:
print "Hello+World!"
2. Arithmetic:
print 2 + 3 * 4
print 5 squared
3. Function Definition:
def greet#1 print "Hello," print arg 1
greet "Alice"
4. Loops:
5 times print "Hello"
3 times ( print "Count:" print times_count 1 )
5. Conditionals:
print "positive" when 5 > 0
if true print "yes" print "no"
6. Factorial (Recursive):
def factorial#1 if ( arg 1 ) == 0 1 ( arg 1 ) * factorial ( arg 1 ) - 1
print factorial 5
7. Arrays and Objects:
print [ 1 2 3 4 5 ]
print { "name" "John" "age" 30 }
8. Iteration:
[ 1 2 3 ] each print each_item
Try copying and pasting these examples!
"""
print(examples)
def run_code(code):
"""Run a single line of code"""
interpreter = PangeaInterpreter()
interpreter.exec(code)
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description='Pangea Python Interpreter')
parser.add_argument('file', nargs='?', help='Pangea file to execute')
parser.add_argument('-c', '--code', help='Execute code directly')
parser.add_argument('-i', '--interactive', action='store_true',
help='Start interactive REPL after running file/code')
args = parser.parse_args()
# Execute file if provided
if args.file:
run_file(args.file)
# Execute code if provided
elif args.code:
run_code(args.code)
# Start REPL if requested or no other action
if args.interactive or (not args.file and not args.code):
run_repl()
if __name__ == "__main__":
main()