Repository navigation
Expand file tree
/
Copy pathinscript.py
More file actions
114 lines (95 loc) · 2.97 KB
/
Copy pathinscript.py
File metadata and controls
114 lines (95 loc) · 2.97 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
import sys, io
if sys.platform == "win32":
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
#!/usr/bin/env python3
"""
Inscript - A clean, Python-like interpreted programming language.
Focus: Language feel, clarity, and developer happiness.
"""
import sys
import os
from inscript.lexer import Lexer
from inscript.parser import Parser
from inscript.interpreter import Interpreter
def run_file(filename: str):
"""Run an Inscript file."""
try:
with open(filename, 'r') as f:
source = f.read()
run_source(source)
except FileNotFoundError:
print(f"Error: File '{filename}' not found")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def run_source(source: str):
"""Run Inscript source code."""
try:
# Lexical analysis
lexer = Lexer(source)
tokens = lexer.tokenize()
# Parsing
parser = Parser(tokens)
ast = parser.parse()
# Interpretation
interpreter = Interpreter()
interpreter.interpret(ast)
except SyntaxError as e:
print(f"Syntax Error: {e}")
sys.exit(1)
except NameError as e:
print(f"Name Error: {e}")
sys.exit(1)
except RuntimeError as e:
print(f"Runtime Error: {e}")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
def repl():
"""Start an interactive REPL."""
print("Inscript 0.1.0 - Interactive Mode")
print("Type 'exit' or 'quit' to exit")
print("-" * 40)
interpreter = Interpreter()
while True:
try:
source = input(">>> ")
if source.lower() in ('exit', 'quit'):
print("Goodbye!")
break
if not source.strip():
continue
# Lexical analysis
lexer = Lexer(source)
tokens = lexer.tokenize()
# Parsing
parser = Parser(tokens)
ast = parser.parse()
# Interpretation
result = interpreter.interpret(ast)
if result is not None:
print(result)
except SyntaxError as e:
print(f"Syntax Error: {e}")
except NameError as e:
print(f"Name Error: {e}")
except RuntimeError as e:
print(f"Runtime Error: {e}")
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
except Exception as e:
print(f"Error: {e}")
def main():
if len(sys.argv) < 2:
print("Inscript 0.1.0")
print("Usage: inscript <script.is> or inscript --repl")
sys.exit(0)
if sys.argv[1] in ('--repl', '-i', 'repl'):
repl()
else:
run_file(sys.argv[1])
if __name__ == '__main__':
main()