LuaComp/src/init.lua

73 lines
1.9 KiB
Lua
Raw Normal View History

2019-11-06 06:47:42 +11:00
--[[
init.lua - Main file of LuaComp
Copyright 2019 Adorable-Catgirl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]
2019-11-06 07:14:33 +11:00
--#include "luacomp_vars.lua"
2019-11-06 06:47:42 +11:00
--#include "ast.lua"
--#include "generator.lua"
--#include "directive_provider.lua"
--#include "cfg/minifier_providers.lua"
local argparse = require("argparse")
2019-11-06 06:47:42 +11:00
2019-11-06 07:14:33 +11:00
local parser = argparse(arg[0], "LuaComp v"..LUACOMP_VERSION.."\nA Lua preprocessor+postprocessor.")
2019-11-06 06:47:42 +11:00
parser:argument("input", "Input file (- for STDIN)")
parser:option("-O --output", "Output file. (- for STDOUT)", "-")
parser:option("-m --minifier", "Sets the minifier", "none")
2019-11-25 05:25:40 +11:00
parser:option("-x --executable", "Makes the script an executable (default: current lua version)"):args "?"
2019-11-06 06:47:42 +11:00
local args = parser:parse()
local file = args.input
local f
if (file ~= "-") then
2019-11-25 05:25:40 +11:00
f = io.open(file, "r")
if not f then
2019-11-06 06:47:42 +11:00
io.stderr:write("ERROR: File `"..file.."' does not exist!\n")
os.exit(1)
end
else
2019-11-25 05:25:40 +11:00
f = io.stdin
2019-11-06 06:47:42 +11:00
end
local ast = mkast(f, file)
local ocode = generate(ast)
local minifier = providers[args.minifier]
2019-11-25 05:25:40 +11:00
local rcode, err = minifier(ocode)
if (not rcode) then
io.stderr:write("Error for minifier `"..args.minifier.."': \n")
io.stderr:write(err)
os.exit(1)
end
2019-11-06 06:47:42 +11:00
local of
if (args.output == "-") then
of = io.stdout
else
of = io.open(args.output, "w")
end
2019-11-25 05:25:40 +11:00
local ver = _VERSION:lower():gsub(" ", "")
if jit then
ver = "luajit"
end
2019-11-06 06:47:42 +11:00
if (args.executable) then
2019-11-25 05:25:40 +11:00
of:write("#!/usr/bin/env ", args.executable[1] or ver, "\n")
2019-11-06 06:47:42 +11:00
end
of:write(rcode)
of:close()
2019-11-06 07:14:33 +11:00
f:close()