forked from luz-lang/luzlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (84 loc) · 2.92 KB
/
Copy pathmain.py
File metadata and controls
95 lines (84 loc) · 2.92 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
import sys
from luz.lexer import Lexer
from luz.parser import Parser
from luz.interpreter import Interpreter
def run(text, interpreter):
try:
lexer = Lexer(text)
tokens = lexer.get_tokens()
parser = Parser(tokens)
ast = parser.parse()
result = interpreter.visit(ast)
return result
except Exception as e:
error_name = type(e).__name__
line = getattr(e, 'line', None)
col = getattr(e, 'col', None)
if line is not None and col is not None:
prefix = f"[Line {line}, Col {col}] "
elif line is not None:
prefix = f"[Line {line}] "
else:
prefix = ""
msg = getattr(e, 'message', str(e))
print(f"{prefix}{error_name}: {msg}")
return None
def check(filename):
"""Parse-only mode for the VS Code extension. Outputs errors as JSON."""
import json
try:
with open(filename, 'r', encoding='utf-8') as f:
code = f.read()
lexer = Lexer(code)
tokens = lexer.get_tokens()
parser = Parser(tokens)
parser.parse()
print(json.dumps([]))
except Exception as e:
line = getattr(e, 'line', None)
msg = getattr(e, 'message', str(e))
print(json.dumps([{"line": line, "message": msg}]))
def main():
interpreter = Interpreter()
if len(sys.argv) > 2 and sys.argv[1] == '--check':
check(sys.argv[2])
return
if len(sys.argv) > 1:
filename = sys.argv[1]
try:
with open(filename, 'r', encoding='utf-8') as f:
code = f.read()
run(code, interpreter)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
except Exception as e:
print(f"Error reading file: {e}")
else:
print("Luz Interpreter v1.8.0 - Type 'exit' to terminate")
while True:
try:
text = input("Luz > ")
if text.strip().lower() == "exit":
break
if not text.strip():
continue
result = run(text, interpreter)
if result is not None:
print(result)
except KeyboardInterrupt:
print("\nExiting...")
break
except Exception as e:
error_name = type(e).__name__
line = getattr(e, 'line', None)
col = getattr(e, 'col', None)
if line is not None and col is not None:
prefix = f"[Line {line}, Col {col}] "
elif line is not None:
prefix = f"[Line {line}]"
else:
prefix = ""
msg = getattr(e, 'message', str(e))
print(f"{prefix}{error_name}: {msg}")
if __name__ == "__main__":
main()