-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragonc.lua
128 lines (99 loc) · 2.45 KB
/
dragonc.lua
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
-- this program may seem redundant but it uses real dragonc and then the assembler
-- which are separate smaller programs instead of one big one
-- cuz KISS
local function getdirectory(p)
for i = #p, 1, -1 do
if p:sub(i,i) == "/" then
return p:sub(1,i)
end
end
return "./"
end
local sd = getdirectory(arg[0])
local flat = false
local incdir = ""
local libdir = ""
local target = "target=xr17032"
local format = "format=xloff"
local preprocargs = " "
local asmout = false
local narg = {}
for k,v in ipairs(arg) do
if v == "-flat" then
flat = true
elseif v == "-S" then
asmout = true
elseif v:sub(1,7) == "incdir=" then
incdir = v
elseif v:sub(1,7) == "libdir=" then
libdir = v
elseif v:sub(1,7) == "target=" then
target = v
elseif v:sub(1,7) == "format=" then
format = v
elseif v:find("=") then
preprocargs = preprocargs..v.." "
else
narg[#narg + 1] = v
end
end
local function printhelp()
print("== dragonc.lua ==")
print("compiler for dragonfruit to xloff/xr17032 object files")
print("(or flat binaries with the -flat argument)")
print("usage: dragonc.lua [source1 source2 ...] [dest1 dest2 ...]")
end
local sourcef = {}
local destf = {}
if (#narg < 2) or (math.floor(#narg/2) ~= #narg/2) then
print("argument mismatch")
printhelp()
return
end
for i = 1, #narg/2 do
sourcef[#sourcef + 1] = narg[i]
destf[#destf + 1] = narg[#narg/2 + i]
end
local lua = sd.."lua.sh "
local preproc = sd.."preproc/preproc.lua "..incdir.." "..libdir.." "..preprocargs
local dragonc = sd.."dragonfruit/dragonc.lua "..target.." "
local asm = sd.."asmfx/asmfx.lua "..format.." "..target.." "
if flat then
asm = asm .. "format=flat "
end
local dx = 0
local function getfilename(p)
local qp = 1
for i = 1, #p do
if p:sub(i,i) == "/" then
qp = i + 1
end
end
return p:sub(qp)
end
for k,v in ipairs(sourcef) do
local ed = getdirectory(v)
local pout = ed..".__out"..getfilename(v)..".pp "
local eout
if not asmout then
eout = ed..".__out"..getfilename(v)..".s "
else
eout = destf[k]
end
local err
-- is there a better way to do this? probably.
err = os.execute(lua..preproc..v.." "..pout)
if not err or (err > 0) then
os.execute("rm -f "..pout)
os.exit(1)
end
err = os.execute(lua..dragonc..pout.." "..eout)
os.execute("rm -f "..pout)
if not err or (err > 0) then os.exit(1) end
if asmout then
return
end
err = os.execute(lua..asm..eout..destf[k])
if not err or (err > 0) then os.exit(1) end
os.execute("rm -f "..eout)
end