diff --git a/make_release.lua b/make_release.lua index a3577c1..3fbd541 100644 --- a/make_release.lua +++ b/make_release.lua @@ -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") \ No newline at end of file diff --git a/src/application.lua b/src/application.lua index 5043ff4..4dd7945 100644 --- a/src/application.lua +++ b/src/application.lua @@ -2,24 +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 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" local parser = argparse(arg[0]:match("[^/]+$"), "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.") parser:argument("input", "Input file (- for STDIN)") diff --git a/src/directive_provider.lua b/src/directive_provider.lua index 4adb426..2059e84 100644 --- a/src/directive_provider.lua +++ b/src/directive_provider.lua @@ -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 diff --git a/src/directives/include.lua b/src/directives/include.lua index 46e5451..662fce6 100644 --- a/src/directives/include.lua +++ b/src/directives/include.lua @@ -2,7 +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/. -function directives.include(env, file) +function directives.include(env, file, asmod) local sr, err = stat.stat(file) if not sr then return false, err end --[[local f = io.open(file, "r") @@ -10,6 +10,21 @@ function directives.include(env, 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 .. "-- " .. 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 end \ No newline at end of file diff --git a/src/directives/pragma.lua b/src/directives/pragma.lua new file mode 100644 index 0000000..20431bd --- /dev/null +++ b/src/directives/pragma.lua @@ -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 \ No newline at end of file diff --git a/src/directives/warning.lua b/src/directives/warning.lua index f55ac36..98a8e89 100644 --- a/src/directives/warning.lua +++ b/src/directives/warning.lua @@ -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 diff --git a/src/generator2.lua b/src/generator2.lua index 8d081f8..977a035 100644 --- a/src/generator2.lua +++ b/src/generator2.lua @@ -41,7 +41,16 @@ 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" + } + } local fenv = {} for k, v in pairs(_G) do fenv[k] = v @@ -51,7 +60,7 @@ do 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 end function fenv.write_out(code) diff --git a/src/libluacomp.lua b/src/libluacomp.lua index f63560f..d1f9979 100644 --- a/src/libluacomp.lua +++ b/src/libluacomp.lua @@ -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 \ No newline at end of file +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]] \ No newline at end of file diff --git a/src/luacomp_vars.lua b/src/luacomp_vars.lua index 1ec4067..5abda40 100644 --- a/src/luacomp_vars.lua +++ b/src/luacomp_vars.lua @@ -10,6 +10,6 @@ end _sv("LUACOMP_V_MAJ", 2) _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_NAME", "LuaComp") \ No newline at end of file diff --git a/tests/asmod.lua b/tests/asmod.lua new file mode 100644 index 0000000..c1620c3 --- /dev/null +++ b/tests/asmod.lua @@ -0,0 +1,2 @@ +--#include "mod.lua" "foo" +foo() \ No newline at end of file diff --git a/tests/mod.lua b/tests/mod.lua new file mode 100644 index 0000000..9dba345 --- /dev/null +++ b/tests/mod.lua @@ -0,0 +1,3 @@ +return function() + print("hello, world") +end \ No newline at end of file diff --git a/tests/pragma_include_file_names.lua b/tests/pragma_include_file_names.lua new file mode 100644 index 0000000..24d59c1 --- /dev/null +++ b/tests/pragma_include_file_names.lua @@ -0,0 +1,5 @@ +--#pragma "include_file_name" "y" +--#include "nopreproc.lua" +--#include "lua_test.lua" +--#include "shell.lua" +--#include "warning.lua" \ No newline at end of file diff --git a/tests/pragma_include_file_names_local_file_numbers.lua b/tests/pragma_include_file_names_local_file_numbers.lua new file mode 100644 index 0000000..6bade9c --- /dev/null +++ b/tests/pragma_include_file_names_local_file_numbers.lua @@ -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" \ No newline at end of file