1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 17:59:30 +00:00
candran/bin/canc
Reuh 70d3aba121 v0.6.0
* Fixed HORRIBLE parsing bugs with short functions and right assignemnt
operators
* Allowed to omit then, do and end for some statements
* Fixed hexa numbers parsing
* Run the Lua 5.3 test suite through Candran, everything that should
work worked! Yay!

Lacks tests and README
2017-08-31 19:17:34 +02:00

62 lines
1.5 KiB
Lua

#!/bin/lua
local candran = require("candran")
local cmdline = require("lib.cmdline")
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...")
return
end
local args = cmdline(arg)
for _, file in ipairs(args) do
local dest = file:gsub("%.can$", "")..".lua"
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
end
local out = input
if args.preprocess then
out = candran.preprocess(out, args)
end
if args.compile then
out = candran.compile(out, args)
end
if args.compile == nil and args.preprocess == nil then
out = candran.make(input, args)
end
if args.print then
print(out)
else
local outFile = 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)
end
end
outFile:write(out)
outFile:close()
end
end