-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_bos_internal_inc.py
74 lines (65 loc) · 1.75 KB
/
build_bos_internal_inc.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
#!/usr/bin/python3
import os, sys
def error(e):
print("Something went wrong!")
print("Error:",e)
quit()
def fwalk(d, ext=None):
for root, dirs, files in os.walk(d):
for file in files:
if ext is not None:
if "." not in file:
continue
x = file.rsplit(".", maxsplit=1)[1]
if x.lower() != ext.lower():
continue
yield root.replace("\\","/")+"/"+file
def build_internal_inc(args=[]):
print("Building bos_internal.inc")
o = []
sourcelist = list(fwalk("src", "asm"))
# print("\n".join(sourcelist))
for fname in sourcelist:
with open(fname) as f:
curglob = ""
data = f.read().split("\n")
for line in data:
if ":" in line:
if ";" in line:
if line.find(":") > line.find(";"):
continue
if line.startswith("."):
lbl, _ = line.split(":", maxsplit=1)
lbl = curglob + lbl
else:
lbl, _ = line.split(":", maxsplit=1)
curglob = lbl
else:
continue
# print(lbl)
if not all([c.isalnum() or c in '_.' for c in lbl]):
continue
o.append("if defined "+lbl)
o.append("_n_"+lbl+" strcalc "+lbl)
o.append("db '_"+lbl+" := ', _n_"+lbl+",$A")
o.append("end if")
with open("obj/gen_internal.inc", "w") as f:
f.write("""
macro org? a
virtual at a
end macro
include '../src/main.asm'
end virtual
calminstruction (var) strcalc? val
compute val, val ; compute expression
arrange val, val ; convert result to a decimal token
stringify val ; convert decimal token to string
publish var, val
end calminstruction
"""+"\n".join(o))
os.system(f"fasmg obj/gen_internal.inc bin/bos_internal.inc {' '.join(args)}")
if __name__=='__main__':
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} build_args")
else:
build_internal_inc(sys.argv[1:])