Compare commits

...

6 Commits

Author SHA1 Message Date
Sam 81fdcb064b luacomp3 maybe? 2023-08-02 12:55:41 -04:00
sam 798d60dadd experimental relative include 2022-02-14 21:48:54 -05:00
sam 5b8a90da87 nice 2022-02-14 12:14:17 -05:00
Sam Roxanne bc5e11b23f sam doesn't test these 2022-02-08 21:32:19 -05:00
Sam Roxanne 508b565ff1 fixed shell bug 2022-02-08 13:28:37 -05:00
Sam Roxanne 72af06da59 Pragma, libluacomp 2021-06-28 11:00:15 -05:00
23 changed files with 341 additions and 463 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libpreproc"]
path = libpreproc
url = git@github.com:lunaboards-dev/libpreproc.git

View File

@ -0,0 +1 @@
print("bar")

View File

@ -0,0 +1,3 @@
--#pragma "relative_include" "y"
print("foo")
--#include "bar.lua"

View File

@ -0,0 +1,2 @@
print("relative include test!")
--#include "rel_include/foo.lua"

1
libpreproc Submodule

@ -0,0 +1 @@
Subproject commit af74760d6113fee5e509cc983163f140a674ae07

12
luacomp3/init.lua Normal file
View File

@ -0,0 +1,12 @@
--@[[if false then]]
local parser = require("luacomp3.parser")
--@[[else]]
--#include "parser.lua" "parser"
--@[[end]]
local p = setmetatable({}, {__index=parser})
p:init()
local f = io.open(arg[1], "rb")
local d = f:read("a")
f:close()
p:parse(d)

68
luacomp3/parser.lua Normal file
View File

@ -0,0 +1,68 @@
local lpp = require("libpreproc")
local p = function(a)return lpp.pattern(a, true)end
local parser = {}
function parser:single_pass()
end
local span = lpp.block(p"[{", p"}]")
local block = lpp.block(p"[[", p"]]")
local parens = lpp.block(p"(", p")")
local luacode = lpp.pattern("@(%d*)")
local shellcode = p"$"
local directive = lpp.pattern("--#!*")
function parser:init()
local parse = lpp.instance()
--Lua code span
parse:add_token(lpp.prefix(luacode, span), function(stream, instance, result)
if #result.matches == 2 and result.matches[1] and result.matches[1] ~= "" then
result.matches.pass = tonumber(result.matches[1], 10)
if (result.matches.pass > 0) then
instance:emit(string.format("@%d[{%s}]", result.matches.pass, result.matches[2]))
return
end
end
instance:write(string.format("write(%s)", result.matches[#result.matches]))
end)
-- Lua code block
parse:add_token(lpp.prefix(luacode, block), function(stream, instance, result)
--print("<m>", table.unpack(result.matches))
if #result.matches == 2 and result.matches[1] and result.matches[1] ~= "" then
result.matches.pass = tonumber(result.matches[1], 10)
if (result.matches.pass > 0) then
instance:emit(string.format("@%d[[%s]]", result.matches.pass, result.matches[2]))
return
end
end
instance:write(result.matches[#result.matches])
end)
-- Shell variable
parse:add_token(lpp.prefix(shellcode, span), function(stream, instance, result)
instance:write(string.format("write(os.getenv(%q))", result.matches[1]))
end)
-- Shell variable, wrapped in quotes
parse:add_token(lpp.prefix(shellcode, parens), function(stream, instance, result)
instance:write(string.format("write('\"'..os.getenv(%q)..'\"')", result.matches[1]))
end)
-- Shell block
parse:add_token(lpp.prefix(shellcode, block), function(stream, instance, matches)
end)
self.parser = parse
end
function parser:parse(code)
self.parser:compile(code)
print(self.parser.code)
local res = self.parser:generate()
print(res)
end
return parser

View File

@ -10,9 +10,11 @@ os.execute("rm -rf build")
os.execute("mkdir build")
for i=1, #luaexec do
os.execute("luacomp -xlua"..luaexec[i].." -mluamin -O build/luacomp-"..luaexec[i].." src/init.lua")
os.execute("luacomp -xlua"..luaexec[i].." -mnone -O build/luacomp-static-"..luaexec[i].." src/staticinit.lua")
--os.execute("luacomp -xlua"..luaexec[i].." -mnone -O build/luacomp-static-"..luaexec[i].." src/staticinit.lua")
os.execute("chmod +x build/luacomp-"..luaexec[i])
os.execute("chmod +x build/luacomp-static-"..luaexec[i])
--os.execute("chmod +x build/luacomp-static-"..luaexec[i])
end
os.execute("LIBLUACOMP=y luacomp -O build/libluacomp.lua src/libluacomp.lua")
os.execute("cp -v build/luacomp-".._VERSION:sub(5).." luacomp")

View File

@ -2,30 +2,16 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local function dprint(...)
local args = {...}
for i=1, #args do
args[i] = tostring(args[i])
end
if (false) then
io.stderr:write("DEBUG\t"..table.concat(args,"\t"),"\n")
end
end
local stat = require("posix.sys.stat")
local dirent = require("posix.dirent")
--#include "src/shell_var.lua"
--#include "src/luacomp_vars.lua"
--#include "src/libluacomp.lua"
--#include "src/directive_provider.lua"
--#include "src/cfg/minifier_providers.lua"
__DSYM = {}
local parser = argparse(arg[0]:match("[^/]+$"), "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.")
parser:argument("input", "Input file (- for STDIN)")
parser:option("-O --output", "Output file. (- for STDOUT)", "-")
parser:option("-m --minifier", "Sets the postprocessor", "none")
parser:option("-x --executable", "Makes the script an executable (default: current lua version)"):args "?"
parser:flag("-g --debugging", "Adds inline debugging utils to assist in debugging."):action(function() DEBUGGING=true end)
parser:flag("--generator-code", "Outputs only the code from the generator.")
parser:flag("--verbose", "Verbose output. (Debugging)"):action(function() VERBOSE=true end)
parser:flag("--post-processors", "Lists postprocessors"):action(function()
@ -65,6 +51,35 @@ else
f = io.stdin
end
local ocode = luacomp.process_file(f, (file == "-") and "stdin" or file, args.generator_code)
if DEBUGGING then
-- generate debugging symbols
local dsyms = {"LEM:LCDEBUG!!!"}
for i=1, #__DSYM do
local sym = __DSYM[i]
local sym_str = string.format("FILE[%s]:START[%d,%d]:END[%d:%d]:FILE[%d,%d]", sym.file, sym.sx or 0, sym.sy or 0, sym.ex or 0, sym.ey or 0, sym.fx or -1, sym.fy or -1)
if sym.tag then
sym_str = sym_str .. ":"..sym.tag
end
table.insert(dsyms, sym_str)
end
ocode = ocode .. "\n--[["..table.concat(dsyms, "\n").."\n]]"
end
if DEBUGGING then
local dsymt = {}
for i=1, #__DSYM do
local sym = __DSYM[i]
local symstr = string.format("file=%q,sx=%q,sy=%q,ex=%q,ey=%q,fx=%q,fy=%q", sym.file, sym.sx or 0, sym.sy or 0, sym.ex or 0, sym.ey or 0, sym.fx or -1, sym.fy or -1)
if sym.tagv then
for i=1, #sym.tagv.vals do
sym.tagv.vals[i]=string.format("%q", sym.tagv.vals[i])
end
symstr = symstr .. ",tag={" ..string.format("type=%q,vals={%s}", sym.tagv.type, table.concat(sym.tagv.vals, ",")).."}"
end
table.insert(dsymt,"{"..symstr.."}")
end
ocode = "_G['LCDEBUG!!!'] = {\n"..table.concat(dsymt, ",\n").."\n}\n" .. ocode
end
local minifier = providers[args.minifier]
dprint("Minifier: "..args.minifier, minifier)

View File

@ -1,286 +0,0 @@
--[[
ast.lua - Generates a structure for use in preprocessing.
]]
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local function nextc(f, c)
c = c or 1
return f:read(c)
end
local function peek(f, c)
c = c or 1
local z = f:read(c)
f:seek("cur", -c)
return z
end
local function skip(f, c)
c = c or 1
return f:seek("cur", c)
end
local ws = {
["\t"] = true,
[" "] = true
}
local function parse_hex(f)
local lc = " "
local hex = ""
while (48 <= lc:byte() and lc:byte() <= 57) or (97 <= lc:byte() and lc:byte() <= 102) or (65 <= lc:byte() and lc:byte() <= 70) and lc do
lc = nextc(f)
if (48 <= lc:byte() and lc:byte()) <= 57 or (97 <= lc:byte() and lc:byte() <= 102) or (65 <= lc:byte() and lc:byte() <= 70) and lc then
hex = hex .. lc
end
end
return tonumber(hex, 16)
end
local function parse_number(f, c)
local lc = " "
local num = c
while 48 <= lc:byte() and lc:byte() <= 57 and lc do
lc = nextc(f)
if (48 <= lc:byte() and lc:byte() <= 57) and lc then
num = num .. lc
end
end
return tonumber(hex, 10)
end
local esct = {
["t"] = "\t",
["n"] = "\n",
["r"] = "\r",
["\\"] = "\\\\"
}
for i=0, 9 do
esct[tostring(i)] = string.char(i)
end
local function parse_dblquote(f)
local val = ""
while peek(f) ~= "\"" do
local c = nextc(f)
if (peek(f) == "\n" or peek(f) == "\r") then
return nil, "Unexpected end of line"
elseif (not peek(f)) then
return nil, "Unexpected end of file"
end
if (c == "\\") then
if (esct[peek(f)]) then
c = esct[peek(f)]
skip(f)
else
c = nextc(f)
end
end
val = val .. c
end
skip(f)
return val
end
local function parse_snglquote(f)
local val = ""
while peek(f) ~= "\'" do
local c = nextc(f)
if (peek(f) == "\n" or peek(f) == "\r") then
return nil, "Unexpected end of line"
elseif (not peek(f)) then
return nil, "Unexpected end of file"
end
if (c == "\\") then
if (esct[peek(f)]) then
c = esct[peek(f)]
skip(f)
else
c = nextc(f)
end
end
val = val .. c
end
skip(f)
return val
end
local function parse_envarg(f)
local val = ""
while peek(f) ~= ")" do
if (peek(f) == "\n" or peek(f) == "\r") then
return nil, "Unexpected end of line"
elseif (not peek(f)) then
return nil, "Unexpected end of file"
end
val = val .. nextc(f)
end
skip(f)
return val
end
local function parse_directive(f)
local lc = "_"
local name = ""
local args = {}
local carg = ""
while not ws[lc] do
lc = nextc(f)
if (lc == "\n" or lc == "\r") then
if (lc == "\r" and peek(f) == "\n") then skip(f) end
return {type="directive", name=name}
elseif not ws[lc] then
name = name .. lc
end
end
while true do
lc = nextc(f)
if (lc == "\n" or lc == "\r") then
if (lc == "\r" and peek(f) == "\n") then skip(f) end
return {type="directive", name=name, args=args}
elseif lc == "0" and peek(f) == "x" then
skip(f)
local val = parse_hex(f)
args[#args+1] = val
elseif 48 <= lc:byte() and lc:byte() <= 57 then
local val = parse_number(f, lc)
args[#args+1] = val
elseif lc == "\"" then
local val, e = parse_dblquote(f)
if not val then return val, e end
args[#args+1] = val
elseif lc == "\'" then
local val, e = parse_snglquote(f)
if not val then return val, e end
args[#args+1] = val
elseif lc == "$" and peek(f) == "(" then
skip(f)
local val = parse_envarg(f)
if not os.getenv(val) then return nil, "Enviroment variable `"..val.."' does not exist." end
args[#args+1] = os.getenv(val)
elseif lc == "@" and peek(f, 2) == "[{" then
skip(f, 2)
local val = ""
while peek(f, 2) ~= "}]" do
val = val .. nextc(f)
end
args[#args+1] = {type="lua_var", val}
skip(f, 2)
elseif not ws[lc] then
return nil, "Syntax error"
end
end
end
local function mkast(f, n)
io.stderr:write("PROC\t",n,"\n")
local lc = " "
local lpos = 1
local ilpos = 1
local tree = {}
local code = ""
local branches = {}
local function add_code()
tree[#tree+1] = {type="code", data=code, file=n, line=lpos}
code = ""
end
local function parse_error(e)
io.stderr:write("ERROR:"..n..":"..lpos..": "..e.."\n")
os.exit(1)
end
while lc and lc ~= "" do
lc = nextc(f)
if (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 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)
local val, e = parse_envarg(f)
if not val then
parse_error(e)
end
tree[#tree+1] = {type="envvar", var=val, file=n, line=lpos}
elseif (lc == "@" and peek(f, 2) == "[[") then
add_code()
skip(f, 2)
local val = ""
while peek(f, 2) ~= "]]" do
val = val .. nextc(f)
end
tree[#tree+1] = {type="lua", code=val, file=n, line=lpos}
skip(f, 2)
elseif (lc == "@" and peek(f, 2) == "[{") then
add_code()
skip(f, 2)
local val = ""
while peek(f, 2) ~= "}]" do
val = val .. nextc(f)
end
tree[#tree+1] = {type="lua_r", code=val, file=n, line=lpos}
skip(f, 2)
elseif (lc == "$" and peek(f, 2) == "[[") then
add_code()
skip(f, 2)
local val = ""
while peek(f, 2) ~= "]]" do
val = val .. nextc(f)
end
tree[#tree+1] = {type="shell", code=val, file=n, line=lpos}
skip(f, 2)
elseif (lc == "$" and peek(f, 2) == "[{") then
add_code()
skip(f, 2)
local val = ""
while peek(f, 2) ~= "}]" do
val = val .. nextc(f)
end
tree[#tree+1] = {type="shell_r", code=val, file=n, line=lpos}
skip(f, 2)
elseif (lc == "\r" or lc == "\n") then
if (lc == "\r" and peek(f) == "\n") then
skip(f)
end
lpos = lpos+1
ilpos = 1
code = code .. "\n"
else
code = code .. (lc or "")
ilpos = ilpos+1
end
end
add_code()
return tree
end

View File

@ -386,6 +386,19 @@ do
return ematch
end
function ast.add_debugging_info(list, str, sx, sy)
if DEBUGGING then
local node = list[#list]
node.sx = sx
node.sy = sy
node.ey, node.ex = str:get_yx()
node.file = str.file
if not str.file then
luacomp.error("Node has no file!\n"..debug.traceback())
end
end
end
-- And now we parse
function ast.parse(str)
local cast = {}
@ -408,11 +421,13 @@ do
end
end
return true
end, "--#", "$".."[[", "@".."[[", "$".."[{", "@".."[{", "$".."(") -- trust me, this was needed
end, "--".."#", "$".."[[", "@".."[[", "$".."[{", "@".."[{", "$".."(", "//".."##") -- trust me, this was needed
--dprint("searched")
local sy, sx = str:get_yx()
if not match then
--dprint("not found")
table.insert(cast, {type="content", val=str:next(str:size())})
ast.add_debugging_info(cast, str, sx, sy)
break
end
local epos = str:tell()
@ -422,11 +437,12 @@ do
local chunk = str:next(size)
if not chunk:match("^%s+$") then
table.insert(cast, {type="content", val=chunk})
ast.add_debugging_info(cast, str, sx, sy)
end
str:skip(#match)
end
--dprint("match: "..match)
if match == "--#" then
if match == "--".."#" or match == "//".."##" then
--str:skip(3)
table.insert(cast, ast.parse_directive(str))
elseif match == "$".."[[" then
@ -446,9 +462,10 @@ do
local var = ast.parse_envvar(str)
table.insert(cast, {type="evar", val=var})
else
ast.parser_error(str, "what")
ast.parser_error(str, "internal compiler error")
end
--dprint("Parsed")
ast.add_debugging_info(cast, str, sx, sy)
end
return cast

View File

@ -11,6 +11,7 @@
--#include "src/directives/loadmod.lua"
--#include "src/directives/error.lua"
--#include "src/directives/warning.lua"
--#include "src/directives/pragma.lua"
setmetatable(directives, {__index=function(t, i)
for i=1, #directive_paths do

View File

@ -2,14 +2,42 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
function directives.include(env, file)
local sr, err = stat.stat(file)
function directives.include(env, file, asmod)
local rfile = file
--luacomp.warning("relative_include\t"..env.pragmas.relative_include)
if env.pragmas.relative_include == "y" or luacomp.experimental.relative_include then
--luacomp.warning("Experimental relative include enabled!")
if not env.fname:find("/") then
rfile = file
else
rfile = env.fname:gsub("/[^/]+$", "/")..file
end
end
local sr, err = stat.stat(rfile)
if not sr then return false, err end
--[[local f = io.open(file, "r")
local fast = mkast(f, file)
fast.file = file
local code = generate(fast)
env.code = env.code .. code .. "\n"]]
env.code = env.code .. luacomp.process_file(file, file) .. "\n"
if asmod then env.code = env.code .. "local "..asmod.." = (function()\n" end
if env.pragmas.include_file_name == "y" then
env.code = env.code .. "-- BEGIN " .. file .. "\n"
end
local code = luacomp.process_file(rfile, rfile) .. "\n"
if env.pragmas.prefix_local_file_numbers == "y" then
local newcode = ""
local i = 1
for match in code:gmatch("[^\n]*") do
newcode = newcode .. "--[["..i.."]] "..match.."\n"
i = i + 1
end
code = newcode
end
env.code = env.code .. code
if env.pragmas.include_file_name == "y" then
env.code = env.code .. "-- END " .. file .. "\n"
end
if asmod then env.code = env.code .. "end)()\n" end
return true
end

20
src/directives/pragma.lua Normal file
View File

@ -0,0 +1,20 @@
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local pragma_hooks = {
include_once = function(env, value)
if value and value ~= 0 then
end
end
}
function directives.pragma(env, key, value)
--luacomp.warning(key.."\t"..value)
if not env.pragmas[key] then
return nil, "unknown pragma "..key
end
env.pragmas[key] = value
return true
end

View File

@ -1,3 +1,7 @@
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
function directives.warning(env, msg)
lc_warning(env.fname, msg)
return true

View File

@ -1,138 +0,0 @@
--[[
generator.lua - Generates the code.
]]
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local function lua_escape(code)
return code:gsub("\\", "\\\\"):gsub("\"", "\\\""):gsub("\n", "\\n")
end
local function shell_escape(code)
return code:gsub("\\%[", "[")
end
local function svar_escape(code)
return code:gsub("\"", "\\\""):gsub("\'", "\\\'"):gsub("`", "\\`")
end
local directives = {}
local function generate(ast, gencode)
local lua_code = ""
for i=1, #ast do
local leaf = ast[i]
if (leaf.type == "lua") then
lua_code = lua_code .. leaf.code
elseif (leaf.type == "directive") then
local stargs = {}
for i=1, #leaf.args do
local arg = leaf.args[i]
if (type(arg) == "string") then
stargs[i] = "\""..lua_escape(arg).."\""
elseif (type(arg) == "number") then
stargs[i] = tostring(arg)
elseif (type(arg) == "table" and arg.type=="lua_var") then
stargs[i] = arg[1]
end
end
lua_code = lua_code .. "call_directive(\""..leaf.file..":"..tostring(leaf.line).."\",\""..leaf.name.."\","..table.concat(stargs, ",")..")"
elseif (leaf.type == "envvar") then
lua_code = lua_code .. "put_env(\""..leaf.file..":"..tostring(leaf.line).."\",\""..leaf.var.."\")"
elseif (leaf.type == "code") then
lua_code = lua_code .. "put_code(\""..leaf.file..":"..tostring(leaf.line).."\",\"" .. lua_escape(leaf.data) .. "\")"
elseif (leaf.type == "lua_r") then
lua_code = lua_code .. "put_code(\""..leaf.file..":"..tostring(leaf.line).."\",tostring("..leaf.code.."))"
elseif (leaf.type == "shell") then
lua_code = lua_code .. "put_shell_out(\""..leaf.file..":"..tostring(leaf.line).."\",\""..lua_escape(leaf.code).."\")"
elseif (leaf.type == "shell_r") then
lua_code = lua_code .. "put_svar(\""..leaf.file..":"..tostring(leaf.line).."\",\""..leaf.code.."\")"
else
io.stderr:write("ERROR: Internal catastrophic failure, unknown type "..leaf.type.."\n")
os.exit(1)
end
lua_code = lua_code .. "\n"
end
local env = {code = ""}
local function run_away_screaming(fpos, err)
io.stdout:write("ERROR: "..fpos..": "..err.."\n")
os.exit(1)
end
local function bitch(fpos, err)
io.stdout:write("WARNING: "..fpos..": "..err.."\n")
end
local function call_directive(fpos, dname, ...)
if (not directives[dname]) then
run_away_screaming(fpos, "Invalid directive name `"..dname.."'")
end
local r, er = directives[dname](env, ...)
if (not r) then
run_away_screaming(fpos, er)
end
end
local function put_env(fpos, evar)
local e = svar.get(evar)
if not e then
run_away_screaming(fpos, "Enviroment variable `"..evar.."' does not exist!")
end
env.code = env.code .. "\""..lua_escape(e).."\""
end
local function put_code(fpos, code)
env.code = env.code .. code --not much that can fail here...
end
local function put_shell_out(fpos, code)
local tname = os.tmpname()
local f = os.tmpname()
local fh = io.open(f, "w")
fh:write(code)
fh:close()
os.execute("chmod +x "..f)
local vars = svar.get_all()
local vstr = ""
for k, v in pairs(vars) do
vstr = vstr .. k.."=".."\""..svar_escape(v).."\" "
end
dprint("Shell", vstr .. f.." "..tname)
local h = io.popen(vstr .. f.." "..tname, "r")
local output = h:read("*a"):gsub("\n$", "")
local ok, sig, code = h:close()
fh = io.open(tname, "r")
local stderr = fh:read("*a"):gsub("\n$", "")
fh:close()
os.remove(f)
os.remove(tname)
if not ok then
run_away_screaming(fpos, "Shell exit code "..code..", SIG_"..sig:upper().."\n"..stderr)
elseif #stderr > 0 then
bitch(fpos, stderr)
end
env.code = env.code .. output
end
local function put_svar(fpos, evar)
local e = svar.get(evar)
if not e then
run_away_screaming(fpos, "Enviroment variable `"..evar.."' does not exist!")
end
env.code = env.code .. e
end
local fenv = {}
for k, v in pairs(_G) do
fenv[k] = v
end
if gencode then
return lua_code
end
fenv._G = fenv
fenv._ENV = fenv
fenv.call_directive = call_directive
fenv.put_code = put_code
fenv.put_env = put_env
fenv.put_svar = put_svar
fenv.put_shell_out = put_shell_out
fenv._GENERATOR=env
local func = assert(load(lua_code, "=(generated code: "..ast.file..")", "t", fenv))
func()
return env.code
end

View File

@ -10,6 +10,23 @@ do
local gcode = ""
for i=1, #ast do
local leaf = ast[i]
if DEBUGGING then
if not leaf.file then
local linfo = {}
for k, v in pairs(leaf) do
table.insert(linfo, tostring(k).."\t"..tostring(v))
end
luacomp.error("Node has no file!\n"..debug.traceback().."\n"..table.concat(linfo, "\n"))
end
table.insert(__DSYM, {
sx = leaf.sx,
sy = leaf.sy,
ex = leaf.ex,
ey = leaf.ey,
file = leaf.file
})
gcode = gcode .. string.format("push_debuginfo(%d)\n", #__DSYM)
end
if leaf.type == "directive" then
gcode = gcode .. string.format("call_directive(%q,", leaf.name)
local pargs = {}
@ -41,21 +58,67 @@ do
end
function generator.run_gcode(fname, gcode)
local env = {code = "", fname=fname}
fname = fname or "(unknown)"
local env = {
code = "",
fname = fname,
pragmas = {
include_file_name = "n",
prefix_local_file_numbers = "n",
wrap_includes = "n",
relative_include = "n"
}
}
local fenv = {}
for k, v in pairs(_G) do
fenv[k] = v
end
fenv._G = fenv
fenv._GENERATOR = env
local lsym
function fenv.push_debuginfo(idx)
local ent = __DSYM[idx]
local linecount = 0
for l in env.code:gmatch("[^\n]*") do
linecount = linecount+1
end
local x = 1
while true do
x = x + 1
local c = env.code:sub(#env.code-x, #env.code-x)
if c == "\n" or c == "" then
break
end
end
ent.fx = x-1
ent.fy = linecount
if lsym then
local lent = __DSYM[idx]
lent.fey = ent.fy
end
lsym = idx
end
local function debug_add_tag(ttype, ...)
if not DEBUGGING then return end
local alist = table.pack(...)
for i=1, #alist do
alist[i] = tostring(alist[i])
end
__DSYM[lsym].tag = string.format("TYPE[%s=%s]", ttype, table.concat(alist,","))
__DSYM[lsym].tagv = {type=ttype, vals=table.pack(...)}
end
function fenv.call_directive(dname, ...)
if not directives[dname] then lc_error("@[{_GENERATOR.fname}]", "invalid directive "..dname) end
local r, er = directives[dname](env, ...)
if not r then lc_error("@[{_GENERATOR.fname}]", er) end
if not r then lc_error("directive "..dname, er) end
debug_add_tag("CALL_DIR", dname, ...)
end
function fenv.write_out(code)
env.code = env.code .. code
debug_add_tag("CODE", #tostring(code))
end
function fenv.shell_write(cmd)
@ -64,12 +127,18 @@ do
f:write(cmd)
f:close()
local h = io.popen(os.getenv("SHELL").." "..tmpname, "r")
env.code = env.code .. h:read("*a")
local r = h:read("*a"):gsub("%s+$", ""):gsub("^%s+", "")
env.code = env.code .. r
h:close()
debug_add_tag("SHELL", cmd, #r)
end
assert(load(gcode, "="..fname, "t", fenv))()
if DEBUGGING then
end
return env.code
end
end

View File

@ -3,14 +3,18 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local luacomp = {}
if arg and arg[0] == "luacomp" then
_G.luacomp = luacomp
local function dprint(...)
local args = {...}
for i=1, #args do
args[i] = tostring(args[i])
end
if (false) then
io.stderr:write("DEBUG\t"..table.concat(args,"\t"),"\n")
end
end
local directives = {}
local stat = require("posix.sys.stat")
local dirent = require("posix.dirent")
local unistd = require("posix.unistd")
local function lc_error(name, msg)
@ -30,14 +34,26 @@ local function lc_warning(name, msg)
end
end
local luacomp = {}
local directives = {}
--#include "src/shell_var.lua"
--#include "src/luacomp_vars.lua"
--#include "src/directive_provider.lua"
--#include "src/cfg/minifier_providers.lua"
@[[if not svar.get("LIBLUACOMP") then]]
_G.luacomp = luacomp
@[[end]]
function luacomp.error(msg)
local inf = debug.getinfo(1)
local inf = debug.getinfo(2)
local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp"
lc_error(name, msg)
end
function luacomp.warning(msg)
local inf = debug.getinfo(1)
local inf = debug.getinfo(2)
local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp"
lc_warning(name, msg)
end
@ -46,7 +62,9 @@ end
--#include "src/generator2.lua"
function luacomp.process_file(file, fname, dry)
@[[if not svar.get("LIBLUACOMP") then]]
io.stderr:write("PROC\t", fname, "\n")
@[[end]]
if type(file) == "string" then
file = io.open(file, "r")
end
@ -64,4 +82,22 @@ function luacomp.process_string(str, name, dry)
end
--error("TODO: implement generation")
return generator.run_gcode(name, gcode)
end
end
function luacomp.run_minifier(minifier, code)
local min = providers[minifier]
if not minifier then
lc_error("luacomp", "Postprocessor "..minifier.." not found!")
--io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n")
--os.exit(1)
end
local rcode, err = min(code)
if (not rcode) then
lc_error(args.minifier, "Postprocessor error:\n"..err)
end
return rcode
end
@[[if svar.get("LIBLUACOMP") then]]
return luacomp
@[[end]]

View File

@ -10,6 +10,10 @@ end
_sv("LUACOMP_V_MAJ", 2)
_sv("LUACOMP_V_MIN", 0)
_sv("LUACOMP_V_PAT", 2)
_sv("LUACOMP_V_PAT", 4)
_sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT)
_sv("LUACOMP_NAME", "LuaComp")
_sv("LUACOMP_NAME", "LuaComp")
luacomp.experimental = {
relative_include = false
}

2
tests/asmod.lua Normal file
View File

@ -0,0 +1,2 @@
--#include "mod.lua" "foo"
foo()

3
tests/mod.lua Normal file
View File

@ -0,0 +1,3 @@
return function()
print("hello, world")
end

View File

@ -0,0 +1,5 @@
--#pragma "include_file_name" "y"
--#include "nopreproc.lua"
--#include "lua_test.lua"
--#include "shell.lua"
--#include "warning.lua"

View File

@ -0,0 +1,6 @@
--#pragma "include_file_name" "y"
--#pragma "prefix_local_file_numbers" "y"
--#include "nopreproc.lua"
--#include "lua_test.lua"
--#include "shell.lua"
--#include "warning.lua"