mirror of
https://github.com/Adorable-Catgirl/LuaComp.git
synced 2024-09-29 07:00:58 +10:00
Compare commits
No commits in common. "master" and "2.0.3" have entirely different histories.
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
||||
[submodule "libpreproc"]
|
||||
path = libpreproc
|
||||
url = git@github.com:lunaboards-dev/libpreproc.git
|
@ -1 +0,0 @@
|
||||
print("bar")
|
@ -1,3 +0,0 @@
|
||||
--#pragma "relative_include" "y"
|
||||
print("foo")
|
||||
--#include "bar.lua"
|
@ -1,2 +0,0 @@
|
||||
print("relative include test!")
|
||||
--#include "rel_include/foo.lua"
|
@ -1 +0,0 @@
|
||||
Subproject commit af74760d6113fee5e509cc983163f140a674ae07
|
@ -1,12 +0,0 @@
|
||||
--@[[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)
|
@ -1,68 +0,0 @@
|
||||
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
|
@ -4,14 +4,11 @@
|
||||
|
||||
--#include "src/libluacomp.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()
|
||||
@ -51,35 +48,6 @@ 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)
|
||||
|
286
src/ast.lua
Normal file
286
src/ast.lua
Normal file
@ -0,0 +1,286 @@
|
||||
--[[
|
||||
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
|
23
src/ast2.lua
23
src/ast2.lua
@ -386,19 +386,6 @@ 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 = {}
|
||||
@ -421,13 +408,11 @@ 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()
|
||||
@ -437,12 +422,11 @@ 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 == "--".."#" or match == "//".."##" then
|
||||
if match == "--#" then
|
||||
--str:skip(3)
|
||||
table.insert(cast, ast.parse_directive(str))
|
||||
elseif match == "$".."[[" then
|
||||
@ -462,10 +446,9 @@ do
|
||||
local var = ast.parse_envvar(str)
|
||||
table.insert(cast, {type="evar", val=var})
|
||||
else
|
||||
ast.parser_error(str, "internal compiler error")
|
||||
ast.parser_error(str, "what")
|
||||
end
|
||||
--dprint("Parsed")
|
||||
ast.add_debugging_info(cast, str, sx, sy)
|
||||
end
|
||||
|
||||
return cast
|
||||
|
@ -3,17 +3,7 @@
|
||||
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
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)
|
||||
local sr, err = stat.stat(file)
|
||||
if not sr then return false, err end
|
||||
--[[local f = io.open(file, "r")
|
||||
local fast = mkast(f, file)
|
||||
@ -22,9 +12,9 @@ function directives.include(env, file, asmod)
|
||||
env.code = env.code .. code .. "\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"
|
||||
env.code = env.code .. "-- " .. file .. "\n"
|
||||
end
|
||||
local code = luacomp.process_file(rfile, rfile) .. "\n"
|
||||
local code = luacomp.process_file(file, file) .. "\n"
|
||||
if env.pragmas.prefix_local_file_numbers == "y" then
|
||||
local newcode = ""
|
||||
local i = 1
|
||||
@ -35,9 +25,6 @@ function directives.include(env, file, asmod)
|
||||
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
|
@ -2,16 +2,7 @@
|
||||
-- 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
|
||||
|
138
src/generator.lua
Normal file
138
src/generator.lua
Normal file
@ -0,0 +1,138 @@
|
||||
--[[
|
||||
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
|
@ -10,23 +10,6 @@ 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 = {}
|
||||
@ -65,8 +48,7 @@ do
|
||||
pragmas = {
|
||||
include_file_name = "n",
|
||||
prefix_local_file_numbers = "n",
|
||||
wrap_includes = "n",
|
||||
relative_include = "n"
|
||||
wrap_includes = "n"
|
||||
}
|
||||
}
|
||||
local fenv = {}
|
||||
@ -75,50 +57,14 @@ do
|
||||
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("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)
|
||||
@ -127,18 +73,12 @@ do
|
||||
f:write(cmd)
|
||||
f:close()
|
||||
local h = io.popen(os.getenv("SHELL").." "..tmpname, "r")
|
||||
local r = h:read("*a"):gsub("%s+$", ""):gsub("^%s+", "")
|
||||
env.code = env.code .. r
|
||||
env.code = env.code .. h:read("*a")
|
||||
h:close()
|
||||
debug_add_tag("SHELL", cmd, #r)
|
||||
end
|
||||
|
||||
assert(load(gcode, "="..fname, "t", fenv))()
|
||||
|
||||
if DEBUGGING then
|
||||
|
||||
end
|
||||
|
||||
return env.code
|
||||
end
|
||||
end
|
@ -10,10 +10,6 @@ end
|
||||
|
||||
_sv("LUACOMP_V_MAJ", 2)
|
||||
_sv("LUACOMP_V_MIN", 0)
|
||||
_sv("LUACOMP_V_PAT", 4)
|
||||
_sv("LUACOMP_V_PAT", 3)
|
||||
_sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT)
|
||||
_sv("LUACOMP_NAME", "LuaComp")
|
||||
|
||||
luacomp.experimental = {
|
||||
relative_include = false
|
||||
}
|
Loading…
Reference in New Issue
Block a user