-
Notifications
You must be signed in to change notification settings - Fork 6
/
compile-fnames.py
104 lines (98 loc) · 3.15 KB
/
compile-fnames.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
# takes the `fnames-lts` file, parses it, and then generates code so the
# compiler can expose all standard library functions, constants, and properties
# to Catspeak programs
from pathlib import Path
from textwrap import dedent
import os
import re
blocklist = set([
"argument",
"gml_",
"phy_",
"physics_",
])
def is_in_blocklist(symbol):
global blocklist
for item in blocklist:
if symbol.startswith(item):
return True
return False
# parse fnames
consts = []
functions = []
prop_get = []
prop_set = []
with open("fnames-lts", "r", encoding="utf-8") as fnames:
for line in fnames:
symbol_data = line.strip()
symbol = None
modifiers = None
is_function = False
if match := re.search("^([A-Za-z0-9_]+)\([^\)]*\)(.*)", line):
symbol = match[1]
modifiers = match[2]
is_function = True
elif match := re.search("^([A-Za-z0-9_]+)(.*)", line):
symbol = match[1]
modifiers = match[2]
else:
continue
if is_in_blocklist(symbol):
continue
if "@" in modifiers or \
"&" in modifiers or \
"?" in modifiers or \
"%" in modifiers or \
"^" in modifiers:
continue
if is_function:
functions.append(symbol)
elif "#" in modifiers:
consts.append(symbol)
else:
prop_get.append(symbol)
if "*" in modifiers:
continue
prop_set.append(symbol)
# write to gml file
codegen_path = Path("src-lts/scripts/__scr_catspeak_gml_interface/__scr_catspeak_gml_interface.gml")
if not codegen_path.parent.exists():
os.makedirs(codegen_path.parent)
with open(codegen_path, "w", encoding="utf-8") as file:
print(f"...writing '{codegen_path}'")
writeln = lambda: file.write("\n")
file.write(dedent("""
//! AUTO GENERATED, DON'T MODIFY THIS FILE
//! DELETE THIS FILE IF YOU DO NOT USE
//!
//! ```gml
//! Catspeak.interface.exposeEverythingIDontCareIfModdersCanEditUsersSaveFilesJustLetMeDoThis = true;
//! ```
//# feather use syntax-errors
/// @ignore
function __catspeak_get_gml_interface() {
static db = undefined;
if (db == undefined) {
db = { };
""").strip())
for symbol in functions:
writeln()
if symbol == "method":
file.write(f" db[$ \"method\"] = method(undefined, catspeak_method);")
else:
file.write(f" db[$ \"{symbol}\"] = method(undefined, {symbol});")
for symbol in consts:
writeln()
file.write(f" db[$ \"{symbol}\"] = {symbol};")
for symbol in prop_get:
writeln()
file.write(f" db[$ \"{symbol}_get\"] = method(undefined, function() {{ return {symbol} }});")
for symbol in prop_set:
writeln()
file.write(f" db[$ \"{symbol}_set\"] = method(undefined, function(val) {{ {symbol} = val }});")
file.write(dedent("""
}
return db;
}
""").rstrip())
writeln()