mirror of
https://github.com/Reuh/candran.git
synced 2025-10-27 09:59:29 +00:00
Allow can and canc to read from stdin, check for errors, cleaner output
This commit is contained in:
parent
ab7472152f
commit
09ac497aed
7 changed files with 92 additions and 62 deletions
19
bin/can
19
bin/can
|
|
@ -5,12 +5,25 @@ local cmdline = require("lib.cmdline")
|
|||
local args = cmdline(arg)
|
||||
|
||||
if args.help or args.h then
|
||||
print("Candran interpreter version "..candran.VERSION.." by Reuh")
|
||||
print("Usage: "..arg[0].." [target=<target>] [options] filename")
|
||||
print("Candran "..candran.VERSION.." interpreter by Reuh")
|
||||
print("Usage: "..arg[0].." [options] filename")
|
||||
print("Use - instead of a filename to read from the standard input.")
|
||||
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, val))
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
if #args >= 1 then
|
||||
if arg[#arg] == "-" then
|
||||
local f, err = candran.load(io.read("*a"), "stdin", nil, args)
|
||||
if not f then
|
||||
io.stderr:write("can: "..err.."\n")
|
||||
os.exit(1)
|
||||
end
|
||||
f()
|
||||
elseif #args >= 1 then
|
||||
candran.dofile(args[1], args)
|
||||
else -- REPL
|
||||
print("Candran " .. candran.VERSION)
|
||||
|
|
|
|||
78
bin/canc
78
bin/canc
|
|
@ -5,33 +5,74 @@ local parse = require("lib.lua-parser.parser").parse
|
|||
local pp = require("lib.lua-parser.pp")
|
||||
|
||||
if #arg < 1 then
|
||||
print("Candran compiler version "..candran.VERSION.." by Reuh")
|
||||
print("Usage: "..arg[0].." [target=<target>] [dest=<destination directory>] [-print] [-preprocess] [-compile] [-ast] [options] filename...")
|
||||
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("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, val))
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
local args = cmdline(arg)
|
||||
|
||||
if arg[#arg] == "-" then
|
||||
table.insert(args, io.stdin)
|
||||
end
|
||||
|
||||
for _, file in ipairs(args) do
|
||||
local dest = file:gsub("%.can$", "")..".lua"
|
||||
-- Read
|
||||
local dest, input
|
||||
if file == io.stdin then
|
||||
dest = args.out or "stdin.lua"
|
||||
|
||||
input = io.read("*a")
|
||||
|
||||
args.chunkname = "stdin"
|
||||
else
|
||||
dest = args.out or (file:gsub("%.can$", "")..".lua")
|
||||
|
||||
local inputFile, err = io.open(file, "r")
|
||||
if not inputFile then
|
||||
io.stderr:write("canc: cannot open "..file..": "..err)
|
||||
os.exit(1)
|
||||
end
|
||||
input = inputFile:read("*a")
|
||||
inputFile:close()
|
||||
|
||||
args.chunkname = file
|
||||
end
|
||||
|
||||
-- Parse-only situations
|
||||
if args.parse or args.ast then
|
||||
local ast, err = parse(input, args.chunkname)
|
||||
if not ast then
|
||||
io.stderr:write("canc: "..err.."\n")
|
||||
os.exit(1)
|
||||
end
|
||||
if args.ast then
|
||||
pp.dump(ast)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
-- Compile and output
|
||||
if args.dest then
|
||||
dest = args.dest .. "/" .. dest
|
||||
end
|
||||
|
||||
if not args.print then
|
||||
print("Compiling "..file.." in "..dest)
|
||||
end
|
||||
|
||||
local inputFile, err = io.open(file, "r")
|
||||
if not inputFile then error("Error while opening input file: "..err) end
|
||||
local input = inputFile:read("*a")
|
||||
inputFile:close()
|
||||
|
||||
args.chunkname = file
|
||||
|
||||
if args.ast then
|
||||
pp.dump(assert(parse(input, args.chunkname)))
|
||||
return
|
||||
print("Compiling "..args.chunkname.." in "..dest)
|
||||
end
|
||||
|
||||
local out = input
|
||||
|
|
@ -48,12 +89,13 @@ for _, file in ipairs(args) do
|
|||
if args.print then
|
||||
print(out)
|
||||
else
|
||||
local outFile = io.open(dest, "w")
|
||||
local outFile, err = io.open(dest, "w")
|
||||
if not outFile then
|
||||
os.execute("mkdir -p "..dest:gsub("[^/]+%.lua$", ""))
|
||||
outFile, err = io.open(dest, "w")
|
||||
if not outFile then
|
||||
error("Error while writing output file: "..err)
|
||||
io.stderr:write("canc: cannot open "..dest..": "..err)
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
outFile:write(out)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue