2 require"lib/tokenizer/default"
3 require"lib/parser/default"
5 require"lib/backend/default"
7 local input,finaloutput,mode,cppopts
8 mode = {"preprocess","compile","assemble"}
9 cppopts = "-P -nostdinc -isystem include -traditional-cpp "
12 if arg[argn]:sub(1,1) == "-" then
14 local rem = arg[argn]:sub(2)
16 if rem:sub(1,1) == "E" then -- preprocess only
18 elseif rem:sub(1,1) == "S" then -- compile only
19 mode = {"preprocess","compile"}
20 elseif rem:sub(1,1) == "c" then -- compile and assemble
21 mode = {"preprocess","compile","assemble"}
22 elseif rem:sub(1,1) == "o" then -- output
24 print("jwcc: -o can only be specified once")
28 finaloutput = rem:sub(2)
31 if not arg[argn+1] then
32 print("jwcc: -o requires an option")
35 finaloutput = arg[argn+1]
38 elseif rem:sub(1,1) == "D" then
40 cppopts = cppopts .. "-D "..rem:sub(2).." "
43 if not arg[argn+1] then
44 print("jwcc: -D requires an option")
47 cppopts = cppopts .. "-D "..arg[argn+1].." "
50 elseif rem:sub(1,1) == "I" then
52 cppopts = cppopts .. "-I "..rem:sub(2).." "
55 if not arg[argn+1] then
56 print("jwcc: -I requires an option")
59 cppopts = cppopts .. "-I "..arg[argn+1].." "
62 elseif rem:len() == 0 then
65 print("jwcc: unknown option \""..rem:sub(1,1).."\"")
72 print("jwcc: input can only be specified once")
81 print("jwcc: no input specified")
86 for k,v in pairs(mode) do
87 if v == "preprocess" then
88 if input:match("%.c$") then
89 if not mode[k+1] and finaloutput then
92 output = input:gsub("%.c$", ".i")
95 if os.execute("cpp "..cppopts.." "..input.." "..output) ~= 0 then
100 elseif v == "compile" then
101 if input:match("%.i$") then
102 if not mode[k+1] and finaloutput then
105 output = input:gsub("%.i$", ".s")
107 local f = assert(io.open(input, "r"))
108 local insrc = f:read("*all")
110 tokenlist = tokenize(insrc)
111 parsetree = parse(tokenlist)
113 if backend(parsetree, output) then
118 elseif v == "assemble" then
119 if input:match("%.s$") then
120 if not mode[k+1] and finaloutput then
123 output = input:gsub("%.o$", ".s")
125 if os.execute("blarg-elf-as --fatal-warnings -o "..output.." "..input) ~= 0 then