-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
225 lines (208 loc) · 8.5 KB
/
parser.py
File metadata and controls
225 lines (208 loc) · 8.5 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import re
import sys
import traceback
import random
from compiler import Compiler
def parse(source_code, debug=True):
compiler = Compiler()
lines = [line.split("//")[0].strip() for line in source_code.split("\n")]
lines = [line for line in lines if line]
try:
parse_lines(lines, compiler)
except Exception as e:
if debug:
traceback.print_exc()
error_type = type(e).__name__
print(f"{error_type}: {e}", file=sys.stderr)
sys.exit(1)
return compiler.compile()
def parse_lines(lines, compiler):
i = 0
while i < len(lines):
line = lines[i].strip()
# Assignment
if re.match(r"^\w+\s*=\s*.+$", line):
var, expr = [part.strip() for part in line.split("=")]
if expr.startswith("'") and expr.endswith("'"):
expr = str(ord(expr[1:-1]))
parse_assignment(var, expr, compiler)
# While loop
elif line.startswith("while"):
m = re.match(r"while\s*\(?(.+?)\)?\s*(\{)?$", line)
if not m:
raise SyntaxError(f"Unsupported while syntax: {line}")
condition_str = m.group(1).strip()
if not m.group(2): # No '{' on same line
j = i + 1
while j < len(lines) and not lines[j].strip():
j += 1
if j >= len(lines) or lines[j].strip() != "{":
raise SyntaxError(f"Expected '{{' after while condition on line {i + 1}")
i = j
# Normalize special cases
if condition_str == "True":
condition_str = "0 == 0"
elif condition_str == "False":
condition_str = "0 != 0"
elif condition_str == "Maybe":
condition_str = f"0 != {random.randint(0, 1)}"
body_lines, new_i = extract_block(lines, i, block_start="{", block_end="}")
i = new_i
def body():
parse_lines(body_lines, compiler)
compiler.compile_while(condition_str, body)
# If conditional (with optional else)
elif line.startswith("if"):
m = re.match(r"if\s*\(?(.+?)\)?\s*(\{)?$", line)
if not m:
raise SyntaxError(f"Unsupported if syntax: {line}")
condition_str = m.group(1).strip()
if condition_str.startswith("(") and condition_str.endswith(")"):
condition_str = condition_str[1:-1].strip()
if not m.group(2): # No '{' on same line
j = i + 1
while j < len(lines) and not lines[j].strip():
j += 1
if j >= len(lines) or lines[j].strip() != "{":
raise SyntaxError(f"Expected '{{' after if condition on line {i + 1}")
i = j
if condition_str == "True":
condition_str = "0 == 0"
elif condition_str == "False":
condition_str = "0 != 0"
elif condition_str == "Maybe":
condition_str = f"0 != {random.randint(0, 1)}"
body_lines, new_i = extract_block(lines, i, block_start="{", block_end="}")
i = new_i
# Check for optional else block
has_else = False
else_body_lines = []
if i < len(lines):
next_line = lines[i].strip()
if next_line.startswith("else"):
has_else = True
m_else = re.match(r"else\s*(\{)?$", next_line)
if not m_else:
raise SyntaxError(f"Unsupported else syntax: {next_line}")
if not m_else.group(1): # No '{' after else
j = i + 1
while j < len(lines) and not lines[j].strip():
j += 1
if j >= len(lines) or lines[j].strip() != "{":
raise SyntaxError(f"Expected '{{' after else on line {i + 1}")
i = j
else:
i = i # '{' already present on same line
else_body_lines, new_i = extract_block(lines, i, block_start="{", block_end="}")
i = new_i
def body():
parse_lines(body_lines, compiler)
if has_else:
def else_body():
parse_lines(else_body_lines, compiler)
compiler.compile_if_else(condition_str, body, else_body)
else:
compiler.compile_if(condition_str, body)
# For loop
elif line.startswith("for"):
m = re.match(r"for\s*\(([^;]+);([^;]+);([^)]+)\)\s*(\{)?$", line)
if not m:
raise SyntaxError(f"Unsupported for-loop syntax: {line}")
init_str = m.group(1).strip()
condition_str = m.group(2).strip()
increment_str = m.group(3).strip()
if not m.group(4): # No '{' on same line
j = i + 1
while j < len(lines) and not lines[j].strip():
j += 1
if j >= len(lines) or lines[j].strip() != "{":
raise SyntaxError(f"Expected '{{' after for loop header on line {i + 1}")
i = j
body_lines, new_i = extract_block(lines, i, block_start="{", block_end="}")
i = new_i
def body():
parse_lines(body_lines, compiler)
compiler.compile_for(init_str, condition_str, increment_str, body)
elif line.startswith("print"):
_, var = line.split("(",maxsplit = 1)
var = var[:-1].strip()
args = var.split(",")
var = args[0].strip()
special = args[1].strip() if len(args) > 1 else None
if line.startswith("printc"):
if var.startswith("'") and var.endswith("'"):
var= str(ord(var[1:-1]))
compiler.compile_print(var, mode="PRINT_CHAR")
elif line.startswith("println"):
if var.startswith('"') and var.endswith('"'):
for chr in var[1:-1]:
compiler.compile_print(ord(chr), mode="PRINT_CHAR")
else:
compiler.compile_print(var, mode="PRINT")
if special == "\\n":
compiler.compile_print(10, mode="PRINT_CHAR")
i += 1
def parse_assignment(var, expr, compiler):
expr = expr.strip()
# Tokenize the expression
tokens = re.findall(r"\d+|\w+|[\+\-\*/\(\)]", expr)
# Shunting Yard algorithm for precedence
precedence = {'+': 1, '-': 1, '%': 1, '*': 2, '/': 2, '**':3}
output = []
ops = []
for token in tokens:
if re.match(r"^\d+$", token) or re.match(r"^\w+$", token):
output.append(token)
elif token in precedence:
while (ops and ops[-1] in precedence and
precedence[ops[-1]] >= precedence[token]):
output.append(ops.pop())
ops.append(token)
elif token == '(':
ops.append(token)
elif token == ')':
while ops and ops[-1] != '(':
output.append(ops.pop())
ops.pop() # Remove '('
while ops:
output.append(ops.pop())
# Evaluate RPN and generate code
stack = []
temp_id = [0]
def get_temp():
temp_id[0] += 1
return f"__tmp{temp_id[0]}"
for token in output:
if token in precedence:
right = stack.pop()
left = stack.pop()
tmp = get_temp() if len(stack) > 0 or token != output[-1] else var
lval = int(left) if left.isdigit() else left
rval = int(right) if right.isdigit() else right
compiler.compile_math(tmp, lval, token, rval)
stack.append(tmp)
else:
stack.append(token)
# Final assignment if needed
if stack and stack[-1] != var:
final = stack.pop()
if final.isdigit():
compiler.compile_assign(var, int(final))
else:
compiler.compile_assign(var, final)
def extract_block(lines, start_index, block_start="{", block_end="}"):
body_lines = []
depth = 1
i = start_index + 1
while i < len(lines) and depth > 0:
line = lines[i]
if line.strip() == block_start:
depth += 1
elif line.strip() == block_end:
depth -= 1
if depth == 0:
break
else:
body_lines.append(line)
i += 1
return body_lines, i