1
0
mirror of https://github.com/Adorable-Catgirl/LuaComp.git synced 2024-11-23 02:18:06 +11:00

Pragma, libluacomp

This commit is contained in:
Sam Roxanne 2021-06-28 11:00:15 -05:00
parent bb1bf0cf1b
commit 72af06da59
13 changed files with 110 additions and 33 deletions

View File

@ -10,9 +10,11 @@ os.execute("rm -rf build")
os.execute("mkdir build") os.execute("mkdir build")
for i=1, #luaexec do 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].." -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-"..luaexec[i])
os.execute("chmod +x build/luacomp-static-"..luaexec[i]) --os.execute("chmod +x build/luacomp-static-"..luaexec[i])
end end
os.execute("LIBLUACOMP=y luacomp -O build/libluacomp.lua src/libluacomp.lua")
os.execute("cp -v build/luacomp-".._VERSION:sub(5).." luacomp") os.execute("cp -v build/luacomp-".._VERSION:sub(5).." luacomp")

View File

@ -2,24 +2,7 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this -- 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/. -- 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/libluacomp.lua"
--#include "src/directive_provider.lua"
--#include "src/cfg/minifier_providers.lua"
local parser = argparse(arg[0]:match("[^/]+$"), "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.") local parser = argparse(arg[0]:match("[^/]+$"), "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.")
parser:argument("input", "Input file (- for STDIN)") parser:argument("input", "Input file (- for STDIN)")

View File

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

View File

@ -2,7 +2,7 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this -- 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/. -- file, You can obtain one at https://mozilla.org/MPL/2.0/.
function directives.include(env, file) function directives.include(env, file, asmod)
local sr, err = stat.stat(file) local sr, err = stat.stat(file)
if not sr then return false, err end if not sr then return false, err end
--[[local f = io.open(file, "r") --[[local f = io.open(file, "r")
@ -10,6 +10,21 @@ function directives.include(env, file)
fast.file = file fast.file = file
local code = generate(fast) local code = generate(fast)
env.code = env.code .. code .. "\n"]] 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 .. "-- " .. file .. "\n"
end
local code = luacomp.process_file(file, file) .. "\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 asmod then env.code = env.code .. "end)()\n" end
return true return true
end end

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

@ -0,0 +1,11 @@
-- 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.pragma(env, key, 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) function directives.warning(env, msg)
lc_warning(env.fname, msg) lc_warning(env.fname, msg)
return true return true

View File

@ -41,7 +41,16 @@ do
end end
function generator.run_gcode(fname, gcode) 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"
}
}
local fenv = {} local fenv = {}
for k, v in pairs(_G) do for k, v in pairs(_G) do
fenv[k] = v fenv[k] = v
@ -51,7 +60,7 @@ do
function fenv.call_directive(dname, ...) function fenv.call_directive(dname, ...)
if not directives[dname] then lc_error("@[{_GENERATOR.fname}]", "invalid directive "..dname) end if not directives[dname] then lc_error("@[{_GENERATOR.fname}]", "invalid directive "..dname) end
local r, er = directives[dname](env, ...) 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
end end
function fenv.write_out(code) function fenv.write_out(code)

View File

@ -3,14 +3,18 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this -- 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/. -- file, You can obtain one at https://mozilla.org/MPL/2.0/.
local luacomp = {} local function dprint(...)
local args = {...}
if arg and arg[0] == "luacomp" then for i=1, #args do
_G.luacomp = luacomp args[i] = tostring(args[i])
end
if (false) then
io.stderr:write("DEBUG\t"..table.concat(args,"\t"),"\n")
end
end end
local directives = {} local stat = require("posix.sys.stat")
local dirent = require("posix.dirent")
local unistd = require("posix.unistd") local unistd = require("posix.unistd")
local function lc_error(name, msg) local function lc_error(name, msg)
@ -30,14 +34,26 @@ local function lc_warning(name, msg)
end end
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) 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" local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp"
lc_error(name, msg) lc_error(name, msg)
end end
function luacomp.warning(msg) 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" local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp"
lc_warning(name, msg) lc_warning(name, msg)
end end
@ -46,7 +62,9 @@ end
--#include "src/generator2.lua" --#include "src/generator2.lua"
function luacomp.process_file(file, fname, dry) function luacomp.process_file(file, fname, dry)
@[[if not svar.get("LIBLUACOMP") then]]
io.stderr:write("PROC\t", fname, "\n") io.stderr:write("PROC\t", fname, "\n")
@[[end]]
if type(file) == "string" then if type(file) == "string" then
file = io.open(file, "r") file = io.open(file, "r")
end end
@ -64,4 +82,22 @@ function luacomp.process_string(str, name, dry)
end end
--error("TODO: implement generation") --error("TODO: implement generation")
return generator.run_gcode(name, gcode) 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,6 @@ end
_sv("LUACOMP_V_MAJ", 2) _sv("LUACOMP_V_MAJ", 2)
_sv("LUACOMP_V_MIN", 0) _sv("LUACOMP_V_MIN", 0)
_sv("LUACOMP_V_PAT", 2) _sv("LUACOMP_V_PAT", 3)
_sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT) _sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT)
_sv("LUACOMP_NAME", "LuaComp") _sv("LUACOMP_NAME", "LuaComp")

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"