-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhen.py
More file actions
146 lines (120 loc) · 4.26 KB
/
when.py
File metadata and controls
146 lines (120 loc) · 4.26 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
#!/usr/bin/env python3
"""
WHEN Language Interpreter
A command-line tool for running WHEN programs
Usage:
when <filename.when> - Run a WHEN program
when -i - Interactive REPL mode
when --hot-reload <filename.when> - Run with hot reload enabled
when --version - Show version
when --help - Show this help
"""
import sys
import os
from pathlib import Path
# Add the current directory to the path so we can import our modules
current_dir = Path(__file__).parent
sys.path.insert(0, str(current_dir))
from lexer import Lexer
from parser import Parser
from interpreter import Interpreter
__version__ = "1.0.0"
def show_help():
print(__doc__)
def show_version():
print(f"WHEN Language Interpreter v{__version__}")
print("Built on Python", sys.version)
def run_file(filename: str, hot_reload: bool = False):
"""Run a WHEN program from a file"""
try:
if not os.path.exists(filename):
print(f"Error: File '{filename}' not found")
sys.exit(1)
if not filename.endswith('.when'):
print("Warning: WHEN files should have .when extension")
with open(filename, 'r', encoding='utf-8') as f:
source = f.read()
# Tokenize
lexer = Lexer(source)
tokens = lexer.tokenize()
# Parse
parser = Parser(tokens)
ast = parser.parse()
# Interpret with hot reload if enabled
interpreter = Interpreter(enable_hot_reload=hot_reload, source_file=filename)
interpreter.interpret(ast)
except FileNotFoundError:
print(f"Error: File '{filename}' not found")
sys.exit(1)
except SyntaxError as e:
print(f"Syntax Error: {e}")
sys.exit(1)
except KeyboardInterrupt:
print("\nProgram interrupted by user")
sys.exit(0)
except Exception as e:
print(f"Runtime Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
def interactive_mode():
"""Interactive REPL for WHEN (simplified for now)"""
print(f"WHEN Language Interactive Shell v{__version__}")
print("Type 'exit()' to quit")
print("Note: Interactive mode is limited - use files for full WHEN programs")
interpreter = Interpreter()
while True:
try:
line = input(">>> ")
if line.strip() in ['exit()', 'quit()', 'exit', 'quit']:
break
if line.strip() == '':
continue
# Simple expression evaluation for now
try:
lexer = Lexer(line)
tokens = lexer.tokenize()
parser = Parser(tokens)
# Try to parse as expression for simple evaluation
if len(tokens) > 1 and tokens[0].type.name != 'MAIN':
# Add a simple main wrapper for evaluation
wrapped = f"x = {line}\nmain:\n print(x)\n exit()"
lexer = Lexer(wrapped)
tokens = lexer.tokenize()
parser = Parser(tokens)
ast = parser.parse()
interpreter.interpret(ast)
except Exception as e:
print(f"Error: {e}")
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
except EOFError:
break
print("Goodbye!")
def main():
"""Main entry point"""
if len(sys.argv) == 1:
show_help()
return
arg = sys.argv[1]
if arg in ['-h', '--help']:
show_help()
elif arg in ['-v', '--version']:
show_version()
elif arg in ['-i', '--interactive']:
interactive_mode()
elif arg == '--hot-reload':
if len(sys.argv) < 3:
print("Error: --hot-reload requires a filename")
print("Usage: when --hot-reload <filename.when>")
sys.exit(1)
run_file(sys.argv[2], hot_reload=True)
elif arg.startswith('-'):
print(f"Unknown option: {arg}")
print("Use 'when --help' for usage information")
sys.exit(1)
else:
# Assume it's a filename
run_file(arg)
if __name__ == "__main__":
main()