mirror of
https://github.com/Reuh/candran.git
synced 2025-10-27 09:59:29 +00:00
Use argparse to parse CLI args, separate preprocessor constants from options
This commit is contained in:
parent
dd22f2de3d
commit
e9ae8e21a3
8 changed files with 253 additions and 346 deletions
44
bin/can
44
bin/can
|
|
@ -1,28 +1,30 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
local candran = require("candran").setup()
|
||||
local cmdline = require("candran.cmdline")
|
||||
local util = require("candran.util")
|
||||
local argparse = require("argparse")
|
||||
|
||||
local args = cmdline(arg)
|
||||
-- Parse args --
|
||||
|
||||
if args.help or args.h then
|
||||
print("Candran "..candran.VERSION.." interpreter by Reuh")
|
||||
print("Usage: "..arg[0].." [options] filename")
|
||||
print("Specify no options to start the REPL.")
|
||||
print("Use - instead of a filename to read from the standard input.")
|
||||
print("Interpreter options:")
|
||||
print(" -help or -h print this text")
|
||||
print("Default options:")
|
||||
for opt, val in pairs(candran.default) do
|
||||
if type(val) == "string" then val = val:gsub("\n", "\\n") end
|
||||
print((" %s=%q"):format(opt, tostring(val)))
|
||||
end
|
||||
return
|
||||
end
|
||||
local parser = argparse()
|
||||
:name "can"
|
||||
:description("Candran "..candran.VERSION.." interpreter by Reuh.")
|
||||
:epilog "For more info, see https://github.com/Reuh/candran"
|
||||
|
||||
parser:argument("filename", "Candran file to run. Use - to read from standard input. Start the REPL if no filename given.")
|
||||
:args "?"
|
||||
|
||||
util.cli.addCandranOptions(parser)
|
||||
|
||||
local args = parser:parse()
|
||||
|
||||
local options = util.cli.makeCandranOptions(args)
|
||||
|
||||
-- Run --
|
||||
|
||||
-- stdin
|
||||
if arg[#arg] == "-" then
|
||||
local f, err = candran.load(io.read("*a"), "stdin", nil, args)
|
||||
if args.filename == "-" then
|
||||
local f, err = candran.load(io.read("*a"), "stdin", nil, options)
|
||||
if not f then
|
||||
io.stderr:write("can: "..err.."\n")
|
||||
os.exit(1)
|
||||
|
|
@ -33,8 +35,8 @@ if arg[#arg] == "-" then
|
|||
os.exit(1)
|
||||
end
|
||||
-- file
|
||||
elseif #args >= 1 then
|
||||
local f, err = candran.loadfile(args[1], nil, args)
|
||||
elseif args.filename then
|
||||
local f, err = candran.loadfile(args.filename, nil, options)
|
||||
if not f then
|
||||
io.stderr:write("can: "..err.."\n")
|
||||
os.exit(1)
|
||||
|
|
@ -47,6 +49,8 @@ elseif #args >= 1 then
|
|||
end
|
||||
-- REPL
|
||||
else
|
||||
candran.default = util.merge(candran.default, options)
|
||||
|
||||
-- Setup linenoise
|
||||
local s, l = pcall(require, "linenoise")
|
||||
if not s then -- pure Lua compatibility thingy
|
||||
|
|
|
|||
85
bin/canc
85
bin/canc
|
|
@ -1,48 +1,63 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
local candran = require("candran")
|
||||
local cmdline = require("candran.cmdline")
|
||||
local parse = require("candran.can-parser.parser").parse
|
||||
local pp = require("candran.can-parser.pp")
|
||||
local util = require("candran.util")
|
||||
local argparse = require("argparse")
|
||||
|
||||
local args = cmdline(arg)
|
||||
-- Parse args --
|
||||
|
||||
if #arg < 1 or args.help or args.h then
|
||||
print("Candran "..candran.VERSION.." compiler by Reuh")
|
||||
print("Usage: "..arg[0].." [options] filenames...")
|
||||
print("Use - instead of filenames to read from the standard input. The output file will be named stdin.lua by default.")
|
||||
print("Compiler options:")
|
||||
print(" dest=\"directory\" where compiled files should be written")
|
||||
print(" out=\"name.lua\" output filename. By default, will use the same name as the input file with a .lua extension.")
|
||||
print(" -print write to the standard output instead of creating files")
|
||||
print(" -preprocess only run the preprocessor")
|
||||
print(" -compile only run the compiler")
|
||||
print(" -parse only parse the file and prints errors to stdout")
|
||||
print(" -ast (for debugging purposes) only parse the files and dump the AST to stdout")
|
||||
print(" -help or -h print this text")
|
||||
print("Default options:")
|
||||
for opt, val in pairs(candran.default) do
|
||||
if type(val) == "string" then val = val:gsub("\n", "\\n") end
|
||||
print((" %s=%q"):format(opt, tostring(val)))
|
||||
end
|
||||
return
|
||||
end
|
||||
local parser = argparse()
|
||||
:name "canc"
|
||||
:description("Candran "..candran.VERSION.." compiler by Reuh.")
|
||||
:epilog "For more info, see https://github.com/Reuh/candran"
|
||||
|
||||
if arg[#arg] == "-" then
|
||||
table.insert(args, io.stdin)
|
||||
end
|
||||
parser:argument("filename", "Candran files to compile. Use - to read from standard input; the output file will then be named stdin.lua by default.")
|
||||
:args "+"
|
||||
|
||||
for _, file in ipairs(args) do
|
||||
parser:group("Output options",
|
||||
parser:option("-d --destination")
|
||||
:description "Where compiled files should be written"
|
||||
:argname "directory",
|
||||
|
||||
parser:option("-o --output")
|
||||
:description "Output filename. (default: same name as the input file with a .lua extension)"
|
||||
:argname "filename",
|
||||
|
||||
parser:flag("-p --print")
|
||||
:description "Write to the standard output instead of creating files",
|
||||
|
||||
parser:flag("--preprocess")
|
||||
:description "Only run the preprocessor",
|
||||
|
||||
parser:flag("--compile")
|
||||
:description "Only run the compiler",
|
||||
|
||||
parser:flag("--parse")
|
||||
:description "Only parse the file and prints syntax errors to stdout",
|
||||
|
||||
parser:flag("--ast")
|
||||
:description"(for debugging purposes) Only parse the files and dump the AST to stdout"
|
||||
)
|
||||
|
||||
util.cli.addCandranOptions(parser)
|
||||
|
||||
local args = parser:parse()
|
||||
|
||||
-- Compile --
|
||||
|
||||
for _, file in ipairs(args.filename) do
|
||||
-- Read
|
||||
local dest, input
|
||||
if file == io.stdin then
|
||||
dest = args.out or "stdin.lua"
|
||||
if file == "-" then
|
||||
dest = args.output or "stdin.lua"
|
||||
|
||||
input = io.read("*a")
|
||||
|
||||
args.chunkname = "stdin"
|
||||
else
|
||||
dest = args.out or (file:gsub("%.can$", "")..".lua")
|
||||
dest = args.output or (file:gsub("%.can$", "")..".lua")
|
||||
|
||||
local inputFile, err = io.open(file, "r")
|
||||
if not inputFile then
|
||||
|
|
@ -69,8 +84,10 @@ for _, file in ipairs(args) do
|
|||
end
|
||||
|
||||
-- Compile and output
|
||||
if args.dest then
|
||||
dest = args.dest .. "/" .. dest
|
||||
local options = util.cli.makeCandranOptions(args)
|
||||
|
||||
if args.destination then
|
||||
dest = args.destination .. "/" .. dest
|
||||
end
|
||||
|
||||
if not args.print then
|
||||
|
|
@ -79,7 +96,7 @@ for _, file in ipairs(args) do
|
|||
|
||||
local out = input
|
||||
if args.preprocess then
|
||||
local r, err = candran.preprocess(out, args)
|
||||
local r, err = candran.preprocess(out, options)
|
||||
if not r then
|
||||
io.stderr:write("canc: "..err.."\n")
|
||||
os.exit(1)
|
||||
|
|
@ -87,7 +104,7 @@ for _, file in ipairs(args) do
|
|||
out = r
|
||||
end
|
||||
if args.compile then
|
||||
local r, err = candran.compile(out, args)
|
||||
local r, err = candran.compile(out, options)
|
||||
if not r then
|
||||
io.stderr:write("canc: "..err.."\n")
|
||||
os.exit(1)
|
||||
|
|
@ -95,7 +112,7 @@ for _, file in ipairs(args) do
|
|||
out = r
|
||||
end
|
||||
if args.compile == nil and args.preprocess == nil then
|
||||
local r, err = candran.make(input, args)
|
||||
local r, err = candran.make(input, options)
|
||||
if not r then
|
||||
io.stderr:write("canc: "..err.."\n")
|
||||
os.exit(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue