Skip to content

Commit 508b565

Browse files
fixed shell bug
1 parent 72af06d commit 508b565

File tree

5 files changed

+116
-6
lines changed

5 files changed

+116
-6
lines changed

src/application.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
--#include "src/libluacomp.lua"
66

7+
__DSYM = {}
8+
79
local parser = argparse(arg[0]:match("[^/]+$"), "LuaComp v"..LUACOMP_VERSION.."\nA preprocessor+postprocessor written in Lua.")
810
parser:argument("input", "Input file (- for STDIN)")
911
parser:option("-O --output", "Output file. (- for STDOUT)", "-")
1012
parser:option("-m --minifier", "Sets the postprocessor", "none")
1113
parser:option("-x --executable", "Makes the script an executable (default: current lua version)"):args "?"
14+
parser:flag("-g --debugging", "Adds inline debugging utils to assist in debugging."):action(function() DEBUGGING=true end)
1215
parser:flag("--generator-code", "Outputs only the code from the generator.")
1316
parser:flag("--verbose", "Verbose output. (Debugging)"):action(function() VERBOSE=true end)
1417
parser:flag("--post-processors", "Lists postprocessors"):action(function()
@@ -48,6 +51,35 @@ else
4851
f = io.stdin
4952
end
5053
local ocode = luacomp.process_file(f, (file == "-") and "stdin" or file, args.generator_code)
54+
if DEBUGGING then
55+
-- generate debugging symbols
56+
local dsyms = {"LEM:LCDEBUG!!!"}
57+
for i=1, #__DSYM do
58+
local sym = __DSYM[i]
59+
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)
60+
if sym.tag then
61+
sym_str = sym_str .. ":"..sym.tag
62+
end
63+
table.insert(dsyms, sym_str)
64+
end
65+
ocode = ocode .. "\n--[["..table.concat(dsyms, "\n").."\n]]"
66+
end
67+
68+
if DEBUGGING then
69+
local dsymt = {}
70+
for i=1, #__DSYM do
71+
local sym = __DSYM[i]
72+
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)
73+
if sym.tagv then
74+
for i=1, #sym.tagv.vals do
75+
sym.tagv.vals[i]=string.format("%q", sym.tagv.vals[i])
76+
end
77+
symstr = symstr .. ",tag={" ..string.format("type=%q,vals={%s}", sym.tagv.type, table.concat(sym.tagv.vals, ",")).."}"
78+
end
79+
table.insert(dsymt,"{"..symstr.."}")
80+
end
81+
ocode = "_G['LCDEBUG!!!'] = {\n"..table.concat(dsymt, ",\n").."\n}\n" .. ocode
82+
end
5183

5284
local minifier = providers[args.minifier]
5385
dprint("Minifier: "..args.minifier, minifier)

src/ast2.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,19 @@ do
386386
return ematch
387387
end
388388

389+
function ast.add_debugging_info(list, str, sx, sy)
390+
if DEBUGGING then
391+
local node = list[#list]
392+
node.sx = sx
393+
node.sy = sy
394+
node.ey, node.ex = str:get_yx()
395+
node.file = str.file
396+
if not str.file then
397+
luacomp.error("Node has no file!\n"..debug.traceback())
398+
end
399+
end
400+
end
401+
389402
-- And now we parse
390403
function ast.parse(str)
391404
local cast = {}
@@ -408,11 +421,13 @@ do
408421
end
409422
end
410423
return true
411-
end, "--#", "$".."[[", "@".."[[", "$".."[{", "@".."[{", "$".."(") -- trust me, this was needed
424+
end, "--".."#", "$".."[[", "@".."[[", "$".."[{", "@".."[{", "$".."(", "//".."##") -- trust me, this was needed
412425
--dprint("searched")
426+
local sy, sx = str:get_yx()
413427
if not match then
414428
--dprint("not found")
415429
table.insert(cast, {type="content", val=str:next(str:size())})
430+
ast.add_debugging_info(cast, str, sx, sy)
416431
break
417432
end
418433
local epos = str:tell()
@@ -422,11 +437,12 @@ do
422437
local chunk = str:next(size)
423438
if not chunk:match("^%s+$") then
424439
table.insert(cast, {type="content", val=chunk})
440+
ast.add_debugging_info(cast, str, sx, sy)
425441
end
426442
str:skip(#match)
427443
end
428444
--dprint("match: "..match)
429-
if match == "--#" then
445+
if match == "--".."#" or match == "//".."##" then
430446
--str:skip(3)
431447
table.insert(cast, ast.parse_directive(str))
432448
elseif match == "$".."[[" then
@@ -446,9 +462,10 @@ do
446462
local var = ast.parse_envvar(str)
447463
table.insert(cast, {type="evar", val=var})
448464
else
449-
ast.parser_error(str, "what")
465+
ast.parser_error(str, "internal compiler error")
450466
end
451467
--dprint("Parsed")
468+
ast.add_debugging_info(cast, str, sx, sy)
452469
end
453470

454471
return cast

src/directives/include.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function directives.include(env, file, asmod)
1212
env.code = env.code .. code .. "\n"]]
1313
if asmod then env.code = env.code .. "local "..asmod.." = (function()\n" end
1414
if env.pragmas.include_file_name == "y" then
15-
env.code = env.code .. "-- " .. file .. "\n"
15+
env.code = env.code .. "-- BEGIN " .. file .. "\n"
1616
end
1717
local code = luacomp.process_file(file, file) .. "\n"
1818
if env.pragmas.prefix_local_file_numbers == "y" then
@@ -25,6 +25,9 @@ function directives.include(env, file, asmod)
2525
code = newcode
2626
end
2727
env.code = env.code .. code
28+
if env.pragmas.include_file_name == "y" then
29+
env.code = env.code .. "-- END " .. file .. "\n"
30+
end
2831
if asmod then env.code = env.code .. "end)()\n" end
2932
return true
3033
end

src/generator2.lua

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ do
1010
local gcode = ""
1111
for i=1, #ast do
1212
local leaf = ast[i]
13+
if DEBUGGING then
14+
if not leaf.file then
15+
local linfo = {}
16+
for k, v in pairs(leaf) do
17+
table.insert(linfo, tostring(k).."\t"..tostring(v))
18+
end
19+
luacomp.error("Node has no file!\n"..debug.traceback().."\n"..table.concat(linfo, "\n"))
20+
end
21+
table.insert(__DSYM, {
22+
sx = leaf.sx,
23+
sy = leaf.sy,
24+
ex = leaf.ex,
25+
ey = leaf.ey,
26+
file = leaf.file
27+
})
28+
gcode = gcode .. string.format("push_debuginfo(%d)\n", #__DSYM)
29+
end
1330
if leaf.type == "directive" then
1431
gcode = gcode .. string.format("call_directive(%q,", leaf.name)
1532
local pargs = {}
@@ -57,14 +74,49 @@ do
5774
end
5875
fenv._G = fenv
5976
fenv._GENERATOR = env
77+
local lsym
78+
function fenv.push_debuginfo(idx)
79+
local ent = __DSYM[idx]
80+
local linecount = 0
81+
for l in env.code:gmatch("[^\n]*") do
82+
linecount = linecount+1
83+
end
84+
local x = 1
85+
while true do
86+
x = x + 1
87+
local c = env.code:sub(#env.code-x, #env.code-x)
88+
if c == "\n" or c == "" then
89+
break
90+
end
91+
end
92+
ent.fx = x-1
93+
ent.fy = linecount
94+
if lsym then
95+
local lent = __DSYM[idx]
96+
lent.fey = ent.fy
97+
end
98+
lsym = idx
99+
end
100+
101+
local function debug_add_tag(ttype, ...)
102+
local alist = table.pack(...)
103+
for i=1, #alist do
104+
alist[i] = tostring(alist[i])
105+
end
106+
__DSYM[lsym].tag = string.format("TYPE[%s=%s]", ttype, table.concat(alist,","))
107+
__DSYM[lsym].tagv = {type=ttype, vals=table.pack(...)}
108+
end
109+
60110
function fenv.call_directive(dname, ...)
61111
if not directives[dname] then lc_error("@[{_GENERATOR.fname}]", "invalid directive "..dname) end
62112
local r, er = directives[dname](env, ...)
63113
if not r then lc_error("directive "..dname, er) end
114+
debug_add_tag("CALL_DIR", dname, ...)
64115
end
65116

66117
function fenv.write_out(code)
67118
env.code = env.code .. code
119+
debug_add_tag("CODE", #tostring(code))
68120
end
69121

70122
function fenv.shell_write(cmd)
@@ -73,12 +125,18 @@ do
73125
f:write(cmd)
74126
f:close()
75127
local h = io.popen(os.getenv("SHELL").." "..tmpname, "r")
76-
env.code = env.code .. h:read("*a")
128+
local r = h:read("*a"):gsub("%s+$", ""):gsub("^%s+", "")
129+
env.code = env.code .. r
77130
h:close()
131+
debug_add_tag("SHELL", cmd, #r)
78132
end
79133

80134
assert(load(gcode, "="..fname, "t", fenv))()
81135

136+
if DEBUGGING then
137+
138+
end
139+
82140
return env.code
83141
end
84142
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", 3)
13+
_sv("LUACOMP_V_PAT", 4)
1414
_sv("LUACOMP_VERSION", LUACOMP_V_MAJ.."."..LUACOMP_V_MIN.."."..LUACOMP_V_PAT)
1515
_sv("LUACOMP_NAME", "LuaComp")

0 commit comments

Comments
 (0)