Skip to content

Commit 72af06d

Browse files
Pragma, libluacomp
1 parent bb1bf0c commit 72af06d

13 files changed

Lines changed: 110 additions & 33 deletions

make_release.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ os.execute("rm -rf build")
1010
os.execute("mkdir build")
1111
for i=1, #luaexec do
1212
os.execute("luacomp -xlua"..luaexec[i].." -mluamin -O build/luacomp-"..luaexec[i].." src/init.lua")
13-
os.execute("luacomp -xlua"..luaexec[i].." -mnone -O build/luacomp-static-"..luaexec[i].." src/staticinit.lua")
13+
--os.execute("luacomp -xlua"..luaexec[i].." -mnone -O build/luacomp-static-"..luaexec[i].." src/staticinit.lua")
1414
os.execute("chmod +x build/luacomp-"..luaexec[i])
15-
os.execute("chmod +x build/luacomp-static-"..luaexec[i])
15+
--os.execute("chmod +x build/luacomp-static-"..luaexec[i])
1616
end
1717

18+
os.execute("LIBLUACOMP=y luacomp -O build/libluacomp.lua src/libluacomp.lua")
19+
1820
os.execute("cp -v build/luacomp-".._VERSION:sub(5).." luacomp")

src/application.lua

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,7 @@
22
-- License, v. 2.0. If a copy of the MPL was not distributed with this
33
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5-
local function dprint(...)
6-
local args = {...}
7-
for i=1, #args do
8-
args[i] = tostring(args[i])
9-
end
10-
if (false) then
11-
io.stderr:write("DEBUG\t"..table.concat(args,"\t"),"\n")
12-
end
13-
end
14-
15-
local stat = require("posix.sys.stat")
16-
local dirent = require("posix.dirent")
17-
18-
--#include "src/shell_var.lua"
19-
--#include "src/luacomp_vars.lua"
205
--#include "src/libluacomp.lua"
21-
--#include "src/directive_provider.lua"
22-
--#include "src/cfg/minifier_providers.lua"
236

247
local parser = argparse(arg[0]:match("[^/]+$"), "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.")
258
parser:argument("input", "Input file (- for STDIN)")

src/directive_provider.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
--#include "src/directives/loadmod.lua"
1212
--#include "src/directives/error.lua"
1313
--#include "src/directives/warning.lua"
14+
--#include "src/directives/pragma.lua"
1415

1516
setmetatable(directives, {__index=function(t, i)
1617
for i=1, #directive_paths do

src/directives/include.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@
22
-- License, v. 2.0. If a copy of the MPL was not distributed with this
33
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5-
function directives.include(env, file)
5+
function directives.include(env, file, asmod)
66
local sr, err = stat.stat(file)
77
if not sr then return false, err end
88
--[[local f = io.open(file, "r")
99
local fast = mkast(f, file)
1010
fast.file = file
1111
local code = generate(fast)
1212
env.code = env.code .. code .. "\n"]]
13-
env.code = env.code .. luacomp.process_file(file, file) .. "\n"
13+
if asmod then env.code = env.code .. "local "..asmod.." = (function()\n" end
14+
if env.pragmas.include_file_name == "y" then
15+
env.code = env.code .. "-- " .. file .. "\n"
16+
end
17+
local code = luacomp.process_file(file, file) .. "\n"
18+
if env.pragmas.prefix_local_file_numbers == "y" then
19+
local newcode = ""
20+
local i = 1
21+
for match in code:gmatch("[^\n]*") do
22+
newcode = newcode .. "--[["..i.."]] "..match.."\n"
23+
i = i + 1
24+
end
25+
code = newcode
26+
end
27+
env.code = env.code .. code
28+
if asmod then env.code = env.code .. "end)()\n" end
1429
return true
1530
end

src/directives/pragma.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
function directives.pragma(env, key, value)
6+
if not env.pragmas[key] then
7+
return nil, "unknown pragma "..key
8+
end
9+
env.pragmas[key] = value
10+
return true
11+
end

src/directives/warning.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
15
function directives.warning(env, msg)
26
lc_warning(env.fname, msg)
37
return true

src/generator2.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ do
4141
end
4242

4343
function generator.run_gcode(fname, gcode)
44-
local env = {code = "", fname=fname}
44+
fname = fname or "(unknown)"
45+
local env = {
46+
code = "",
47+
fname = fname,
48+
pragmas = {
49+
include_file_name = "n",
50+
prefix_local_file_numbers = "n",
51+
wrap_includes = "n"
52+
}
53+
}
4554
local fenv = {}
4655
for k, v in pairs(_G) do
4756
fenv[k] = v
@@ -51,7 +60,7 @@ do
5160
function fenv.call_directive(dname, ...)
5261
if not directives[dname] then lc_error("@[{_GENERATOR.fname}]", "invalid directive "..dname) end
5362
local r, er = directives[dname](env, ...)
54-
if not r then lc_error("@[{_GENERATOR.fname}]", er) end
63+
if not r then lc_error("directive "..dname, er) end
5564
end
5665

5766
function fenv.write_out(code)

src/libluacomp.lua

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
-- License, v. 2.0. If a copy of the MPL was not distributed with this
44
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
local luacomp = {}
7-
8-
if arg and arg[0] == "luacomp" then
9-
_G.luacomp = luacomp
6+
local function dprint(...)
7+
local args = {...}
8+
for i=1, #args do
9+
args[i] = tostring(args[i])
10+
end
11+
if (false) then
12+
io.stderr:write("DEBUG\t"..table.concat(args,"\t"),"\n")
13+
end
1014
end
1115

12-
local directives = {}
13-
16+
local stat = require("posix.sys.stat")
17+
local dirent = require("posix.dirent")
1418
local unistd = require("posix.unistd")
1519

1620
local function lc_error(name, msg)
@@ -30,14 +34,26 @@ local function lc_warning(name, msg)
3034
end
3135
end
3236

37+
local luacomp = {}
38+
local directives = {}
39+
40+
--#include "src/shell_var.lua"
41+
--#include "src/luacomp_vars.lua"
42+
--#include "src/directive_provider.lua"
43+
--#include "src/cfg/minifier_providers.lua"
44+
45+
@[[if not svar.get("LIBLUACOMP") then]]
46+
_G.luacomp = luacomp
47+
@[[end]]
48+
3349
function luacomp.error(msg)
34-
local inf = debug.getinfo(1)
50+
local inf = debug.getinfo(2)
3551
local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp"
3652
lc_error(name, msg)
3753
end
3854

3955
function luacomp.warning(msg)
40-
local inf = debug.getinfo(1)
56+
local inf = debug.getinfo(2)
4157
local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp"
4258
lc_warning(name, msg)
4359
end
@@ -46,7 +62,9 @@ end
4662
--#include "src/generator2.lua"
4763

4864
function luacomp.process_file(file, fname, dry)
65+
@[[if not svar.get("LIBLUACOMP") then]]
4966
io.stderr:write("PROC\t", fname, "\n")
67+
@[[end]]
5068
if type(file) == "string" then
5169
file = io.open(file, "r")
5270
end
@@ -64,4 +82,22 @@ function luacomp.process_string(str, name, dry)
6482
end
6583
--error("TODO: implement generation")
6684
return generator.run_gcode(name, gcode)
67-
end
85+
end
86+
87+
function luacomp.run_minifier(minifier, code)
88+
local min = providers[minifier]
89+
if not minifier then
90+
lc_error("luacomp", "Postprocessor "..minifier.." not found!")
91+
--io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n")
92+
--os.exit(1)
93+
end
94+
local rcode, err = min(code)
95+
if (not rcode) then
96+
lc_error(args.minifier, "Postprocessor error:\n"..err)
97+
end
98+
return rcode
99+
end
100+
101+
@[[if svar.get("LIBLUACOMP") then]]
102+
return luacomp
103+
@[[end]]

src/luacomp_vars.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ end
1010

1111
_sv("LUACOMP_V_MAJ", 2)
1212
_sv("LUACOMP_V_MIN", 0)
13-
_sv("LUACOMP_V_PAT", 2)
13+
_sv("LUACOMP_V_PAT", 3)
1414
_sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT)
1515
_sv("LUACOMP_NAME", "LuaComp")

tests/asmod.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--#include "mod.lua" "foo"
2+
foo()

0 commit comments

Comments
 (0)