Reworked mutually exclusive options in the compiler
Now the compiler uses the mutex option from argparse. No need
to manually check mutually exclusive options. Removed function
pallenec_abort, since argparse now throws an error and shows a message.
@@ -9,24 +9,22 @@ local util = require "pallene.util"
local p = argparse("pallenec", "Pallene compiler")
p:argument("source_file", "File to compile")
-p:flag("--emit-c", "Generate a .c file instead of an .so file")
-p:flag("--emit-asm", "Generate a .s file instead of an .so file")
-p:flag("--emit-lua", "Generate a .lua file instead of an .so file")
-p:flag("--compile-c", "Compile a .c file generated by --emit-c")
-p:flag("--show-locations", "Shows locations when dumping the AST")
+p:mutex(
+ p:flag("--emit-c", "Generate a .c file instead of an .so file"),
+ p:flag("--emit-asm", "Generate a .s file instead of an .so file"),
+ p:flag("--emit-lua", "Generate a .lua file instead of an .so file"),
+ p:flag("--compile-c", "Compile a .c file generated by --emit-c"),
+ p:flag("--print-ir", "Show a human-readable version of the program's IR"),
p:option("--dump", "Print the result of an internal compiler pass")
:choices({"ast", "checker", "ir", "uninitialized", "constant_propagation"})
-p:flag("--print-ir", "Show a human-readable version of the program's IR")
+)
+p:flag("--show-locations", "Shows locations when dumping the AST")
local args = p:parse()
-- For compilation errors that don't happen inside a source file.
-- Inspired by gcc, eg. "gcc: fatal error: no input files".
local compiler_name = arg[0]
-local function pallenec_abort(fmt, ...)
- return util.abort(compiler_name .. ": " .. string.format(fmt, ...))
-end
-
local function compile(in_ext, out_ext)
local ok, errs = driver.compile(compiler_name, in_ext, out_ext, args.source_file)
if not ok then util.abort(table.concat(errs, "\n")) end
@@ -70,20 +68,15 @@ local function pretty_print_ir(filename)
io.stdout:write(print_ir(module))
end
-local flags = {}
-if args.emit_c then table.insert(flags, "--emit-c") end
-if args.emit_asm then table.insert(flags, "--emit-asm") end
-if args.compile_c then table.insert(flags, "--compile-c") end
-if args.dump then table.insert(flags, "--dump") end
-if args.print_ir then table.insert(flags, "--print-ir") end
-if args.emit_lua then table.insert(flags, "--emit-lua") end
-
-if #flags >= 2 then
- local conflicting = table.concat(flags, " and ")
- pallenec_abort("flags %s are mutually exclusive", conflicting)
-end
+local flags = 0
+if args.emit_c then flags = flags + 1 end
+if args.emit_asm then flags = flags + 1 end
+if args.compile_c then flags = flags + 1 end
+if args.dump then flags = flags + 1 end
+if args.print_ir then flags = flags + 1 end
+if args.emit_lua then flags = flags + 1 end
-if #flags == 0 then compile("pln", "so")
+if flags == 0 then compile("pln", "so")
elseif args.emit_c then compile("pln", "c")
elseif args.emit_asm then compile("c", "s")
elseif args.compile_c then compile("c" , "so")
