-
Notifications
You must be signed in to change notification settings - Fork 14
/
gdsl.py
401 lines (378 loc) · 12.4 KB
/
gdsl.py
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import json
from dataclasses import dataclass
from typing import Literal
from rich import print
@dataclass
class UnOp:
opcode: str
input: str
fields: dict[str, str]
@dataclass
class BinOp:
opcode: str
lhs: str
rhs: str
@dataclass
class Menu:
input: str
opcode: str
default: str
@dataclass
class Block:
name: str
opcode: str
args: list[str]
fields: dict[str, str]
menu: Menu | None
def snake_to_pascal(s: str):
return "".join(x.title() for x in s.split("_"))
def table_split(table: str, n: int):
xs = table.split()
xs.extend([""] * (n - len(xs)))
return xs
def parse():
un_ops: dict[str, UnOp | None] = {}
bin_ops: dict[str, BinOp | None] = {}
blocks: dict[str, Block | list[Block]] = {}
reporters: dict[str, Block | list[Block]] = {}
old_opcode = ""
old_input = ""
old_lhs = ""
old_rhs = ""
old_fields: list[str] = []
old_menu = ""
opcode_prefix = ""
old_args = ""
section: Literal["UNARY", "BINARY", "BLOCKS", "REPORTERS"] | None = None
lines = iter(open("gdsl.txt"))
for line in lines:
line = line[:-1].strip()
if line.startswith("#") or not line:
continue
print(line)
if line in ["UNARY OPERATORS", "BINARY OPERATORS", "BLOCKS", "REPORTERS"]:
section = line.split()[0] # type: ignore
next(lines, None)
next(lines, None)
next(lines, None)
continue
if section == "UNARY":
if line.endswith("~"):
un_ops[line.removesuffix("~")] = None
continue
table, fields = line.split("|")
fields = fields.strip()
fields = (
dict(
(old_fields[i] if key == "..." else key, value)
for i, (key, value) in enumerate(
x.split("=") for x in fields.strip().split(",")
)
)
if fields
else {}
)
old_fields = list(fields.keys())
variant, opcode, input = table.split()
if opcode == "...":
opcode = old_opcode
else:
opcode = "operator_" + opcode
old_opcode = opcode
if input == "...":
input = old_input
else:
old_input = input
un_ops[variant] = UnOp(opcode, input, fields)
elif section == "BINARY":
if line.endswith("~"):
bin_ops[line.removesuffix("~")] = None
continue
variant, opcode, lhs, rhs = line.split()
if opcode == "...":
opcode = old_opcode
else:
opcode = "operator_" + opcode
old_opcode = opcode
if lhs == "...":
lhs = old_lhs
else:
old_lhs = lhs
if rhs == "...":
rhs = old_rhs
else:
old_rhs = rhs
bin_ops[variant] = BinOp(opcode, lhs, rhs)
else:
if line.startswith("["):
opcode_prefix = line.split("]")[0].removeprefix("[")
continue
table, fields, menu = line.split("|")
menu = menu.strip()
if menu:
input_opcode, default = menu.split("=")
if input_opcode == "...":
input_opcode = old_menu
old_menu = input_opcode
input, opcode = input_opcode.split(":")
menu = Menu(
input=input,
opcode=opcode,
default=default,
)
else:
menu = None
fields = fields.strip()
fields = (
dict(
(old_fields[i] if key == "..." else key, value)
for i, (key, value) in enumerate(
x.split("=") for x in fields.strip().split(",")
)
)
if fields
else {}
)
old_fields = list(fields.keys())
name, opcode, args = table_split(table, 3)
variant = snake_to_pascal(name)
if opcode == "...":
opcode = old_opcode
else:
old_opcode = opcode
opcode = f"{opcode_prefix}_{opcode}"
if args == "...":
args = old_args
else:
old_args = args
args = args.split(",") if args else []
if section == "BLOCKS":
container = blocks
else:
container = reporters
if variant in container:
block = container[variant]
if not isinstance(block, list):
block = [block]
block.append(Block(name, opcode, args, fields, menu))
container[variant] = block
else:
container[variant] = Block(name, opcode, args, fields, menu)
return un_ops, bin_ops, blocks, reporters
un_ops, bin_ops, blocks, reporters = parse()
f = open("src/blocks.rs", "w")
f.write("""
pub struct Menu {
pub input: &'static str,
pub opcode: &'static str,
pub default: &'static str,
}
""")
f.write("#[derive(Debug, Copy, Clone)]\npub enum UnOp {")
for un_op in un_ops:
f.write(f"{un_op},")
f.write("}\n\n")
f.write("impl UnOp {")
f.write("pub fn opcode(&self) -> &'static str {")
f.write("match self {")
for variant, op in un_ops.items():
if not op:
continue
f.write(f'Self::{variant} => "{op.opcode}",')
f.write("_ => unreachable!()")
f.write("}")
f.write("}\n\n")
f.write("pub fn input(&self) -> &'static str {")
f.write("match self {")
for variant, op in un_ops.items():
if not op:
continue
f.write(f'Self::{variant} => "{op.input}",')
f.write("_ => unreachable!()")
f.write("}")
f.write("}\n\n")
f.write("pub fn fields(&self) -> Option<&'static str> {")
f.write("match self {")
for variant, op in un_ops.items():
if not op:
continue
if len(op.fields) == 0:
f.write(f"Self::{variant} => None,")
else:
f.write(
f"Self::{variant} => Some({json.dumps(json.dumps({k:[v,None] for k,v in op.fields.items()}))}),"
)
f.write("_ => unreachable!()")
f.write("}")
f.write("}")
f.write("}\n\n")
f.write("#[derive(Debug, Copy, Clone)]\npub enum BinOp {")
for bin_op in bin_ops:
f.write(f"{bin_op},")
f.write("}\n\n")
f.write("impl BinOp {")
f.write("pub fn opcode(&self) -> &'static str {")
f.write("match self {")
for variant, op in bin_ops.items():
if not op:
continue
f.write(f'Self::{variant} => "{op.opcode}",')
f.write("_ => unreachable!()")
f.write("}")
f.write("}\n\n")
f.write("pub fn lhs(&self) -> &'static str {")
f.write("match self {")
for variant, op in bin_ops.items():
if not op:
continue
f.write(f'Self::{variant} => "{op.lhs}",')
f.write("_ => unreachable!()")
f.write("}")
f.write("}\n\n")
f.write("pub fn rhs(&self) -> &'static str {")
f.write("match self {")
for variant, op in bin_ops.items():
if not op:
continue
f.write(f'Self::{variant} => "{op.rhs}",')
f.write("_ => unreachable!()")
f.write("}")
f.write("}")
f.write("}")
def write_blocks(typename: str, blocks: dict[str, Block | list[Block]]):
f.write(f"#[derive(Debug, Copy, Clone)]\npub enum {typename} {{")
for variant, block in blocks.items():
if isinstance(block, list):
for block in block:
f.write(f"{variant}{len(block.args)},")
else:
f.write(f"{variant},")
f.write("}\n\n")
f.write(f"impl {typename} {{")
f.write("pub fn menu(&self) -> Option<Menu> {")
f.write("match self {")
for variant, block in blocks.items():
if isinstance(block, list):
for block in block:
avariant = f"{variant}{len(block.args)}"
if block.menu:
f.write(
f"Self::{avariant} => Some(Menu {{ opcode: {json.dumps(block.menu.opcode)}, input: {json.dumps(block.menu.input)}, default: {json.dumps(block.menu.default)} }}),"
)
elif block.menu:
f.write(
f"Self::{variant} => Some(Menu {{ opcode: {json.dumps(block.menu.opcode)}, input: {json.dumps(block.menu.input)}, default: {json.dumps(block.menu.default)} }}),"
)
f.write("_ => None }")
f.write("}\n\n")
f.write("pub fn overloads(name: &str) -> &'static [Self] {")
f.write("match name {")
for variant, block in blocks.items():
if isinstance(block, list):
variants = ",".join(f"Self::{variant}{len(b.args)}" for b in block)
f.write(f'"{block[0].name}" => &[{variants}],')
f.write("_ => &[] }")
f.write("}\n\n")
f.write("pub fn from_shape(name: &str, args: usize) -> Option<Self> {")
f.write("match (name, args) {")
for variant, block in blocks.items():
if isinstance(block, list):
b = block
for block in block:
f.write(
f'("{block.name}", {len(block.args)}) => Some(Self::{variant}{len(block.args)}),'
)
f.write(f'("{b[0].name}", _) => Some(Self::{variant}{len(b[0].args)}),')
else:
f.write(f'("{block.name}", _) => Some(Self::{variant}),')
f.write("_ => None")
f.write("}")
f.write("}\n\n")
f.write("pub fn name(&self) -> &'static str {")
f.write("match self {")
for variant, block in blocks.items():
if not block:
continue
if isinstance(block, list):
for block in block:
f.write(
f"Self::{variant}{len(block.args)} => {json.dumps(block.name)},"
)
else:
f.write(f"Self::{variant} => {json.dumps(block.name)},")
f.write("}")
f.write("}\n\n")
f.write("pub fn all_names() -> &'static [&'static str] {")
f.write("&[")
for variant, block in blocks.items():
if not block:
continue
if isinstance(block, list):
block = block[0]
f.write(f"{json.dumps(block.name)},")
f.write("]")
f.write("}\n\n")
f.write("pub fn opcode(&self) -> &'static str {")
f.write("match self {")
for variant, block in blocks.items():
if not block:
continue
if isinstance(block, list):
for block in block:
f.write(
f"Self::{variant}{len(block.args)} => {json.dumps(block.opcode)},"
)
else:
f.write(f"Self::{variant} => {json.dumps(block.opcode)},")
f.write("}")
f.write("}\n\n")
f.write("pub fn args(&self) -> &'static [&'static str] {")
f.write("match self {")
for variant, block in blocks.items():
if not block:
continue
if isinstance(block, list):
for block in block:
f.write(
f"Self::{variant}{len(block.args)} => &{json.dumps(block.args)},"
)
else:
f.write(f"Self::{variant} => &{json.dumps(block.args)},")
f.write("}")
f.write("}\n\n")
f.write("pub fn fields(&self) -> Option<&'static str> {")
f.write("match self {")
for variant, block in blocks.items():
if not block:
continue
if isinstance(block, list):
for block in block:
if len(block.fields) == 0:
f.write(f"Self::{variant}{len(block.args)} => None,")
continue
f.write(
f"Self::{variant}{len(block.args)} => Some({json.dumps(json.dumps({k:[v,None] for k,v in block.fields.items()}))}),"
)
else:
if len(block.fields) == 0:
f.write(f"Self::{variant} => None,")
continue
f.write(
f"Self::{variant} => Some({json.dumps(json.dumps({k:[v,None] for k,v in block.fields.items()}))}),"
)
f.write("}")
f.write("}\n\n")
f.write("}")
write_blocks("Block", blocks)
write_blocks("Repr", reporters)
print(
json.dumps(
f'\\b({"|".join(block.name if isinstance(block, Block) else block[0].name for block in blocks.values() )})\\b'
)
)
print()
print(
json.dumps(
f'\\b({"|".join(block.name if isinstance(block, Block) else block[0].name for block in reporters.values() )})\\b'
)
)