-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.py
More file actions
366 lines (290 loc) · 10.9 KB
/
compiler.py
File metadata and controls
366 lines (290 loc) · 10.9 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import numpy as np
import importlib
import os
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def compile(filename):
file = CodeFile(filename)
def import_file(filename):
dest = "out/" + filename.removesuffix(".pyf") + ".py"
dest_folder = "/".join(dest.split("/")[:-1])
create_dir(dest_folder)
file = CodeFile(filename, dest)
lib = importlib.import_module("out." + filename.removesuffix(".pyf").replace("/", "."))
return lib
class CodeFile:
def __init__(self, filename, dest=None):
self.filename = filename
with open(self.filename) as f:
self.text = f.readlines()
self.text = list(map(lambda x: x.removesuffix("\n"), self.text))
self.code_blocks = []
self.find_code_blocks()
self.arrows = []
self.find_arrows()
self.compile()
self.write(dest)
def find(self, string):
pass
def get_char(self, x, y):
if 0 <= y < len(self.text):
if 0 <= x < len(self.text[y]):
return self.text[y][x]
return " "
def find_code_blocks(self):
for y, line in enumerate(self.text):
chars_left = 0
for x in range(len(line)):
if chars_left > 0:
chars_left -= 1
continue
char = self.get_char(x, y)
if char == "{":
l = 1
depth = 1
string = None
while not depth == 0:
if self.get_char(x+l-1, y) == "\\":
l += 1
continue
if self.get_char(x+l, y) == "{" and string is None:
depth += 1
elif self.get_char(x+l, y) == "}" and string is None:
depth -= 1
elif self.get_char(x+l, y) == '"':
if string is None:
string = '"'
elif string == '"':
string = None
elif self.get_char(x+l, y) == "'":
if string is None:
string = "'"
elif string == "'":
string = None
if x + l >= len(line):
raise Exception("Code block not closed")
l += 1
chars_left = l - 1
self.code_blocks.append(CodeBlock(line[x:x+l], x, y))
def find_arrows(self):
for y, line in enumerate(self.text):
for x in range(len(line)):
if not self.in_code_block(x, y):
char = self.get_char(x, y)
if char in "><^v":
self.arrows.append(Arrow((x, y), self))
def in_code_block(self, x, y):
for block in self.code_blocks:
if x in block.range and y == block.y:
return True
return False
def find_start(self):
for block in self.code_blocks:
if block.code == "start":
return block
def arrows_from(self, x, y):
arrows = []
for arrow in self.arrows:
if arrow.start[0] == x and arrow.start[1] == y:
arrows.append(arrow)
return arrows
def arrows_from_block(self, block):
arrows = []
if block.simple:
for arrow in self.arrows:
if arrow.start[0] in block.range and arrow.start[1] == block.y:
arrows.append(arrow)
else:
for arrow in self.arrows:
if arrow.start[0] in block.sections[-1] and arrow.start[1] == block.y:
arrows.append(arrow)
return arrows
def arrows_from_section(self, block, index):
arrows = []
for arrow in self.arrows:
if arrow.start[0] in block.sections[index] and arrow.start[1] == block.y:
arrows.append(arrow)
return arrows
def arrows_to_range(self, x, y):
arrows = []
for arrow in self.arrows:
if arrow.to[0] in x and arrow.to[1] == y:
arrows.append(arrow)
return arrows
def block_at(self, x, y):
for block in self.code_blocks:
if x in block.range and y == block.y:
return block
def resolve_tree(self, block):
if block is None:
raise Exception("Arrow leads to nothing")
if block.simple:
code = self.resolve_inserts(block) + "\n"
arrows = self.arrows_from_block(block)
if len(arrows) > 0:
code += self.resolve_tree(self.block_at(*arrows[0].to))
return code
else:
code = ""
for i, code_block in enumerate(self.resolve_inserts(block)[:-1]):
code += code_block + ":\n"
code += indent(self.resolve_tree(self.block_at(*self.arrows_from_section(block, i)[0].to))) + "\n"
arrows = self.arrows_from_section(block, -1)
if len(arrows) > 0:
code += self.resolve_tree(self.block_at(*arrows[0].to)) + "\n"
return code
def resolve_inserts(self, block):
code = block.code
if not block.simple:
code = "\n".join(block.code)
index = 0
for r in block.insert_ranges:
while code[index] != "_":
index += 1
arrows = self.arrows_to_range(r, block.y)
if len(arrows) > 0:
insert_block = self.block_at(*arrows[0].start)
insert_block.parse(force_simple=True)
insert = self.resolve_inserts(insert_block)
code = code[:index] + insert + code[index+len(r):]
index += len(r)
if not block.simple:
code = code.split("\n")
return code
def compile(self):
block = self.find_start()
block = self.block_at(*self.arrows_from_block(block)[0].to)
self.code = self.resolve_tree(block)
def write(self, dest=None):
if dest is None:
dest = self.filename.removesuffix(".pyf")
dest = dest + ".py"
dest = "out/" + dest
text = self.code
with open(dest, "w") as f:
f.write(text)
class CodeBlock:
def __init__(self, line, x, y):
self.line = line
self.len = len(line)
#self.code = line[1:-1]
#self.code = self.code.strip()
self.x = x
self.y = y
self.end = self.len + self.x
self.range = range(self.x, self.end)
self.insert_ranges = find_insert_ranges(self.line, self.x)
self.parse()
def parse(self, force_simple=False):
code = self.line
no_strings = remove_strings(code)
semi_colons = [i for i, char in enumerate(no_strings) if char == ";"]
if len(semi_colons) == 0 or force_simple:
self.simple = True
code = code.removeprefix("{").removesuffix("}")
code = code.strip()
self.code = code
else:
self.simple = False
self.code = []
start = 0
for semi_colon in semi_colons:
self.code.append(code[start:semi_colon])
start = semi_colon + 1
self.code.append(code[start:])
self.code[0] = self.code[0].removeprefix("{")
self.code[-1] = self.code[-1].removesuffix("}")
self.code = list(map(lambda x: x.strip(), self.code))
semi_colons.insert(0, -1)
semi_colons.append(len(code))
self.sections = [range(self.x+i+1, self.x+j) for i, j in zip(semi_colons[:-1], semi_colons[1:])]
class Arrow:
def __init__(self, end, file):
self.file = file
self.end = np.array(end)
char = self.get_char(end[0], end[1])
if char == ">":
self.direction = np.array((1, 0))
elif char == "<":
self.direction = np.array((-1, 0))
elif char == "^":
self.direction = np.array((0, -1))
elif char == "v":
self.direction = np.array((0, 1))
self.to = self.end + self.direction
self.find_start()
def get_char(self, x, y):
return self.file.get_char(x, y)
def find_start(self):
found = False
current_direction = self.direction
start = np.copy(self.end)
while not found:
start -= current_direction
if self.file.in_code_block(start[0], start[1]):
found = True
else:
char = self.get_char(start[0], start[1])
if char == "|":
assert current_direction[0] == 0
elif char == "-":
assert current_direction[1] == 0
elif char == "+":
pass
elif char == "*":
possible_directions = []
if current_direction[0] == 0:
possible_directions.append(np.array((1, 0)))
possible_directions.append(np.array((-1, 0)))
else:
possible_directions.append(np.array((0, 1)))
possible_directions.append(np.array((0, -1)))
for direction in possible_directions:
p = start - direction
if self.get_char(p[0], p[1]) != " ":
current_direction = direction
break
else:
if char != " ":
raise Exception(f"Invalid character {char} at {start + 1}")
else:
raise Exception(f"Invalid character Space at {start + 1}")
self.start = start
def remove_strings(code):
no_strings = ""
string = None
for i, char in enumerate(code):
if i > 0 and code[i-1] == "\\":
no_strings += char
continue
if string is None:
if char == '"':
string = '"'
elif char == "'":
string = "'"
elif char == string:
string = None
if string is None:
no_strings += char
else:
no_strings += " "
return no_strings
def indent(code):
lines = code.splitlines()
lines = map(lambda x: " " + x, lines)
return "\n".join(lines)
def find_insert_ranges(line, offset=0):
ranges = []
start = None
for i, char in enumerate(line):
if start is None:
if char == "_":
start = i
else:
if char != "_":
ranges.append(range(start+offset, i+offset))
start = None
return ranges
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)