diff --git a/src/ast.lua b/src/ast.lua index dc19d48..23cc348 100644 --- a/src/ast.lua +++ b/src/ast.lua @@ -206,6 +206,22 @@ local function mkast(f, n) code = code .. lc ilpos = ilpos+1 end + elseif (lc == "/" and ilpos == 1) then + if (peek(f, 2) == "/#") then --Directive + add_code() + skip(f, 2) + local d, r = parse_directive(f) + if not d then + parse_error(r) + end + d.line = lpos + d.file = n + lpos = lpos+1 + tree[#tree+1] = d + else + code = code .. lc + ilpos = ilpos+1 + end elseif (lc == "$" and peek(f) == "(") then add_code() skip(f) diff --git a/src/init.lua b/src/init.lua index 992396f..54a1b69 100644 --- a/src/init.lua +++ b/src/init.lua @@ -28,25 +28,31 @@ local parser = argparse(arg[0], "LuaComp v"..LUACOMP_VERSION.."\nA Lua preproces parser:argument("input", "Input file (- for STDIN)") parser:option("-O --output", "Output file. (- for STDOUT)", "-") parser:option("-m --minifier", "Sets the minifier", "none") -parser:flag("-x --executable", "Makes the script an executable") +parser:option("-x --executable", "Makes the script an executable (default: current lua version)"):args "?" local args = parser:parse() local file = args.input local f if (file ~= "-") then - if (not os.execute("stat "..file..">/dev/null")) then + f = io.open(file, "r") + if not f then io.stderr:write("ERROR: File `"..file.."' does not exist!\n") os.exit(1) end - f = io.open(file, "r") else - file = io.stdin + f = io.stdin end local ast = mkast(f, file) local ocode = generate(ast) local minifier = providers[args.minifier] -local rcode = minifier(ocode) +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 local of if (args.output == "-") then @@ -54,8 +60,12 @@ if (args.output == "-") then else of = io.open(args.output, "w") end +local ver = _VERSION:lower():gsub(" ", "") +if jit then + ver = "luajit" +end if (args.executable) then - of:write("#!/usr/bin/env lua5.3\n") + of:write("#!/usr/bin/env ", args.executable[1] or ver, "\n") end of:write(rcode) of:close() diff --git a/src/luacomp_vars.lua b/src/luacomp_vars.lua index 9e44fb7..afd67ae 100644 --- a/src/luacomp_vars.lua +++ b/src/luacomp_vars.lua @@ -4,7 +4,7 @@ local function _sv(k, v) end _sv("LUACOMP_V_MAJ", 1) -_sv("LUACOMP_V_MIN", 0) +_sv("LUACOMP_V_MIN", 1) _sv("LUACOMP_V_PAT", 0) _sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT) _sv("LUACOMP_NAME", "LuaComp") \ No newline at end of file diff --git a/src/staticinit.lua b/src/staticinit.lua index e69de29..34cd26e 100644 --- a/src/staticinit.lua +++ b/src/staticinit.lua @@ -0,0 +1,68 @@ +--[[ + staticinit.lua - Main file of LuaComp, directly includes argparse. + + 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. +]] + +--#include "luacomp_vars.lua" +--#include "ast.lua" +--#include "generator.lua" +--#include "directive_provider.lua" +--#include "cfg/minifier_providers.lua" + +local argparse = (function() +--#include "argparse.lua" +end)() + +local parser = argparse(arg[0], "LuaComp v"..LUACOMP_VERSION.."\nA Lua preprocessor+postprocessor.") +parser:argument("input", "Input file (- for STDIN)") +parser:option("-O --output", "Output file. (- for STDOUT)", "-") +parser:option("-m --minifier", "Sets the minifier", "none") +parser:option("-x --executable", "Makes the script an executable (default: current lua version)"):args "?" +local args = parser:parse() +local file = args.input +local f +if (file ~= "-") then + f = io.open(file, "r") + if not f then + io.stderr:write("ERROR: File `"..file.."' does not exist!\n") + os.exit(1) + end +else + f = io.stdin +end +local ast = mkast(f, file) +local ocode = generate(ast) + +local minifier = providers[args.minifier] + +local rcode = minifier(ocode) + +local of +if (args.output == "-") then + of = io.stdout +else + of = io.open(args.output, "w") +end +local ver = _VERSION:lower():gsub(" ", "") +if jit then + ver = "luajit" +end +if (args.executable) then + of:write("#!/usr/bin/env ", args.executable[1] or ver, "\n") +end +of:write(rcode) +of:close() +f:close()