Re-added callstack callsite info. It's a bit hacky, but better than

nothing.
This commit is contained in:
Bruce Hill 2018-04-09 18:58:05 -07:00
parent 86925c5b3a
commit d42d20c50a
5 changed files with 69 additions and 55 deletions

View File

@ -5,7 +5,7 @@
# Compile-time action to make compile-time actions:
immediately:
lua> ".."
nomsu:define_compile_action("compile %actions to %lua", \(!! code location !!), function(\%actions, \%lua)
nomsu:define_compile_action("compile %actions to %lua", \(!! code location !!), function(__callsite, \%actions, \%lua)
local stubs = {};
for i, action in ipairs(\%actions.value) do
stubs[i] = nomsu:tree_to_named_stub(action);
@ -30,6 +30,7 @@ immediately:
if #undeclared_locals > 0 then
body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code;
end
table.insert(args, 1, "__callsite");
local lua_fn_args = table.concat(args, ", ");
local def_metadata = nomsu.tree_metadata[nomsu.compilestack[#nomsu.compilestack]];
local code_location = (def_metadata and ("%s:%s,%s"):format(def_metadata.filename, def_metadata.start, def_metadata.stop)
@ -66,6 +67,7 @@ immediately:
if #undeclared_locals > 0 then
body_code = "local "..table.concat(undeclared_locals, ", ")..";\\n"..body_code;
end
table.insert(args, 1, "__callsite");
local lua_fn_args = table.concat(args, ", ");
local def_metadata = nomsu.tree_metadata[nomsu.compilestack[#nomsu.compilestack]];
local code_location = (def_metadata and ("%s:%s,%s"):format(def_metadata.filename, def_metadata.start, def_metadata.stop)
@ -88,6 +90,7 @@ immediately:
for i,tok in ipairs(\%shorthand.value[1].value) do
if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
end
table.insert(args, 1, "__callsite");
local lua_fn_args = table.concat(args, ", ");
local template;
if \%longhand.type == "Block" then

View File

@ -32,7 +32,7 @@ lua> ".."
};
for name, e in pairs(escapes) do
local lua = "'"..e.."'";
nomsu:define_compile_action(name, \(!! code location !!), function() return {expr=lua}; end);
nomsu:define_compile_action(name, \(!! code location !!), function(__callsite) return {expr=lua}; end);
end
local colors = {
["reset color"]="\\\\27[0m", bright="\\\\27[1m", dim="\\\\27[2m", underscore="\\\\27[4m",
@ -47,8 +47,8 @@ lua> ".."
for name, c in pairs(colors) do
local color = "'"..c.."'";
local reset = "'"..colors["reset color"].."'";
nomsu:define_compile_action(name, \(!! code location !!), function() return {expr=color}; end);
nomsu:define_compile_action(name.." %", \(!! code location !!), function(\%)
nomsu:define_compile_action(name, \(!! code location !!), function(__callsite) return {expr=color}; end);
nomsu:define_compile_action(name.." %", \(!! code location !!), function(__callsite, \%)
return {expr=color..".."..nomsu:tree_to_lua(\%).expr..".."..reset};
end);
end

View File

@ -168,13 +168,13 @@ compile [define object %classname %class_body] to:
__lt=\%class_identifier['< %'],
__le=\%class_identifier['<= %'],
};
nomsu:define_action("instances of "..\%class_identifier.name, "lib/class.nom", function()
nomsu:define_action("instances of "..\%class_identifier.name, "lib/class.nom", function(__callsite)
return utils.keys(\%class_identifier.instances);
end, "");
nomsu:define_action("new "..\%class_identifier.name.." %instance", "lib/class.nom", function(_instance)
nomsu:define_action("new "..\%class_identifier.name.." %instance", "lib/class.nom", function(__callsite, _instance)
return \%class_identifier(_instance);
end, "");
nomsu:define_action("new "..\%class_identifier.name, "lib/class.nom", function()
nomsu:define_action("new "..\%class_identifier.name, "lib/class.nom", function(__callsite)
return \%class_identifier({});
end, "");
end -- End of definition of \%class_identifier

View File

@ -213,7 +213,7 @@ do
ident <- [a-zA-Z_][a-zA-Z0-9_]*
comment <- "--" [^%nl]*
]])
local nomsu_peg = peg_tidier:match(io.open("nomsu.peg"):read("*a"))
local nomsu_peg = peg_tidier:match(io.open("nomsu.peg"):read("a"))
NOMSU = re.compile(nomsu_peg, NOMSU_DEFS)
end
local NomsuCompiler
@ -351,13 +351,9 @@ do
end
local line_starts = self.file_metadata[metadata.filename].line_starts
local first_line = 1
while first_line < #line_starts and line_starts[first_line + 1] < metadata.start do
while first_line < #line_starts and line_starts[first_line + 1] <= metadata.start do
first_line = first_line + 1
end
local last_line = first_line
while last_line < #line_starts and line_starts[last_line + 1] < metadata.stop do
last_line = last_line + 1
end
return tostring(metadata.filename) .. ":" .. tostring(first_line)
end,
get_source_code = function(self, tree)
@ -449,7 +445,7 @@ do
end
if filename:match(".*%.lua") then
local file = io.open(filename)
local contents = file:read("*a")
local contents = file:read("a")
file:close()
return assert(load(contents, nil, nil, self.environment))()
end
@ -457,7 +453,7 @@ do
if not self.skip_precompiled then
local file = io.open(filename:gsub("%.nom", ".lua"), "r")
if file then
local lua_code = file:read("*a")
local lua_code = file:read("a")
file:close()
return self:run_lua(lua_code)
end
@ -466,7 +462,7 @@ do
if not file then
error("File does not exist: " .. tostring(filename), 0)
end
local nomsu_code = file:read('*a')
local nomsu_code = file:read('a')
file:close()
return self:run(nomsu_code, filename)
else
@ -1038,6 +1034,7 @@ do
end
args = _accum_0
end
table.insert(args, 1, self:get_line_number(tree))
if metadata and metadata.arg_orders then
local new_args
do
@ -1090,7 +1087,9 @@ do
expr = "(" .. tostring(concat(bits, " ")) .. ")"
}
end
local args = { }
local args = {
repr(self:get_line_number(tree))
}
local _list_1 = tree.value
for _index_0 = 1, #_list_1 do
local _continue_0 = false
@ -1446,7 +1445,8 @@ do
if not (args) then
error("Failed to match arg pattern on alias: " .. tostring(repr(alias)), 0)
end
for j = 1, #args do
table.insert(args, 1, '__callsite')
for j = 2, #args do
args[j] = self:var_to_lua_identifier(args[j])
end
stub_args[i] = args
@ -1491,7 +1491,7 @@ do
end
return concat(concat_parts)
end
self:define_compile_action("immediately %block", get_line_no(), function(_block)
self:define_compile_action("immediately %block", get_line_no(), function(__callsite, _block)
local lua = nomsu:tree_to_lua(_block)
local lua_code = lua.statements or (lua.expr .. ";")
if lua.locals and #lua.locals > 0 then
@ -1503,7 +1503,7 @@ do
locals = lua.locals
}
end)
self:define_compile_action("lua> %code", get_line_no(), function(_code)
self:define_compile_action("lua> %code", get_line_no(), function(__callsite, _code)
if _code.type == "Text" then
local lua = nomsu_string_as_lua(_code)
return {
@ -1511,17 +1511,17 @@ do
}
else
return {
statements = "nomsu:run_lua(" .. tostring(nomsu:tree_to_lua(_code).expr) .. ");"
statements = "nomsu:run_lua(" .. tostring(nomsu:tree_to_lua(__callsite, _code).expr) .. ");"
}
end
end)
self:define_compile_action("=lua %code", get_line_no(), function(_code)
self:define_compile_action("=lua %code", get_line_no(), function(__callsite, _code)
local lua = nomsu_string_as_lua(_code)
return {
expr = lua
}
end)
self:define_compile_action("!! code location !!", get_line_no(), function()
self:define_compile_action("!! code location !!", get_line_no(), function(__callsite)
local tree = nomsu.compilestack[#nomsu.compilestack - 1]
local metadata = self.tree_metadata[tree]
if metadata then
@ -1534,10 +1534,10 @@ do
}
end
end)
self:define_action("run file %filename", get_line_no(), function(_filename)
self:define_action("run file %filename", get_line_no(), function(__callsite, _filename)
return nomsu:run_file(_filename)
end)
return self:define_compile_action("use %filename", get_line_no(), function(_filename)
return self:define_compile_action("use %filename", get_line_no(), function(__callsite, _filename)
local filename = nomsu:tree_to_value(_filename)
nomsu:use_file(filename)
return {
@ -1708,7 +1708,7 @@ if arg and debug_getinfo(2).func ~= require then
local files = setmetatable({ }, {
__index = function(self, filename)
local file = io.open(filename)
local source = file:read("*a")
local source = file:read("a")
file:close()
self[filename] = source
return source
@ -1742,6 +1742,13 @@ if arg and debug_getinfo(2).func ~= require then
end
info.short_src, info.linedefined = filename, line_no
info.currentline = line_no
if type(select(1, ...)) == 'number' then
local varname, callsite = debug.getlocal(select(1, ...), 1)
if varname == "__callsite" then
info.short_src, info.currentline = callsite:match("^(.*):(%d+)$")
info.currentline = tonumber(info.currentline)
end
end
info.source = file
else
info.source = "@" .. metadata.source
@ -1779,7 +1786,7 @@ if arg and debug_getinfo(2).func ~= require then
else
local retval, code
if args.input == '-' then
retval, code = nomsu:run(io.read('*a'), 'stdin')
retval, code = nomsu:run(io.read('a'), 'stdin')
else
retval, code = nomsu:run_file(args.input)
end
@ -1834,7 +1841,7 @@ if arg and debug_getinfo(2).func ~= require then
end
end
local nomsu_file = io.open("nomsu.moon")
local nomsu_source = nomsu_file:read("*a")
local nomsu_source = nomsu_file:read("a")
local _, line_table = to_lua(nomsu_source)
nomsu_file:close()
local level = 2
@ -1860,7 +1867,7 @@ if arg and debug_getinfo(2).func ~= require then
if metadata then
local filename, start, stop = metadata.source:match("([^:]*):([0-9]*),([0-9]*)")
if filename then
local file = io.open(filename):read("*a")
local file = io.open(filename):read("a")
local line_no = 1
for _ in file:sub(1, tonumber(start)):gmatch("\n") do
line_no = line_no + 1
@ -1903,7 +1910,6 @@ if arg and debug_getinfo(2).func ~= require then
end
return os.exit(false, true)
end
local ldt = require('ldt')
ldt.guard(run)
require('ldt').guard(run)
end
return NomsuCompiler

View File

@ -149,7 +149,7 @@ NOMSU = do
ident <- [a-zA-Z_][a-zA-Z0-9_]*
comment <- "--" [^%nl]*
]]
nomsu_peg = peg_tidier\match(io.open("nomsu.peg")\read("*a"))
nomsu_peg = peg_tidier\match(io.open("nomsu.peg")\read("a"))
re.compile(nomsu_peg, NOMSU_DEFS)
class NomsuCompiler
@ -263,11 +263,11 @@ class NomsuCompiler
error "Failed to find file metatdata for file: #{metadata.filename}", 0
line_starts = @file_metadata[metadata.filename].line_starts
first_line = 1
while first_line < #line_starts and line_starts[first_line+1] < metadata.start
while first_line < #line_starts and line_starts[first_line+1] <= metadata.start
first_line += 1
last_line = first_line
while last_line < #line_starts and line_starts[last_line+1] < metadata.stop
last_line += 1
--last_line = first_line
--while last_line < #line_starts and line_starts[last_line+1] <= metadata.stop
-- last_line += 1
--return first_line == last_line and "#{metadata.filename}:#{first_line}" or "#{metadata.filename}:#{first_line}-#{last_line}"
return "#{metadata.filename}:#{first_line}"
@ -342,20 +342,20 @@ class NomsuCompiler
if filename\match(".*%.lua")
file = io.open(filename)
contents = file\read("*a")
contents = file\read("a")
file\close!
return assert(load(contents, nil, nil, @environment))!
if filename\match(".*%.nom")
if not @skip_precompiled -- Look for precompiled version
file = io.open(filename\gsub("%.nom", ".lua"), "r")
if file
lua_code = file\read("*a")
lua_code = file\read("a")
file\close!
return @run_lua(lua_code)
file = file or io.open(filename)
if not file
error("File does not exist: #{filename}", 0)
nomsu_code = file\read('*a')
nomsu_code = file\read('a')
file\close!
return @run(nomsu_code, filename)
else
@ -714,6 +714,7 @@ class NomsuCompiler
metadata = @action_metadata[fn]
if metadata and metadata.compile_time
args = [arg for arg in *tree.value when arg.type != "Word"]
table.insert args, 1, @get_line_number(tree)
if metadata and metadata.arg_orders
new_args = [args[p] for p in *metadata.arg_orders[stub]]
args = new_args
@ -740,7 +741,7 @@ class NomsuCompiler
remove @compilestack
return expr:"(#{concat bits, " "})"
args = {}
args = {repr(@get_line_number(tree))}
for tok in *tree.value
if tok.type == "Word" then continue
lua = @tree_to_lua(tok)
@ -992,7 +993,8 @@ class NomsuCompiler
args = var_pattern\match(alias)
unless args
error("Failed to match arg pattern on alias: #{repr alias}", 0)
for j=1,#args do args[j] = @var_to_lua_identifier(args[j])
table.insert(args,1,'__callsite')
for j=2,#args do args[j] = @var_to_lua_identifier(args[j])
stub_args[i] = args
return stub_args
@ -1022,7 +1024,7 @@ class NomsuCompiler
insert concat_parts, lua.expr
return concat(concat_parts)
@define_compile_action "immediately %block", get_line_no!, (_block)->
@define_compile_action "immediately %block", get_line_no!, (__callsite,_block)->
lua = nomsu\tree_to_lua(_block)
lua_code = lua.statements or (lua.expr..";")
if lua.locals and #lua.locals > 0
@ -1030,18 +1032,18 @@ class NomsuCompiler
nomsu\run_lua(lua_code)
return statements:"if IMMEDIATE then\n#{lua_code}\nend", locals:lua.locals
@define_compile_action "lua> %code", get_line_no!, (_code)->
@define_compile_action "lua> %code", get_line_no!, (__callsite,_code)->
if _code.type == "Text"
lua = nomsu_string_as_lua(_code)
return statements:lua
else
return statements:"nomsu:run_lua(#{nomsu\tree_to_lua(_code).expr});"
return statements:"nomsu:run_lua(#{nomsu\tree_to_lua(__callsite,_code).expr});"
@define_compile_action "=lua %code", get_line_no!, (_code)->
@define_compile_action "=lua %code", get_line_no!, (__callsite,_code)->
lua = nomsu_string_as_lua(_code)
return expr:lua
@define_compile_action "!! code location !!", get_line_no!, ->
@define_compile_action "!! code location !!", get_line_no!, (__callsite)->
tree = nomsu.compilestack[#nomsu.compilestack-1]
metadata = @tree_metadata[tree]
if metadata
@ -1049,10 +1051,10 @@ class NomsuCompiler
else
return expr: repr("<dynamically generated>")
@define_action "run file %filename", get_line_no!, (_filename)->
@define_action "run file %filename", get_line_no!, (__callsite,_filename)->
return nomsu\run_file(_filename)
@define_compile_action "use %filename", get_line_no!, (_filename)->
@define_compile_action "use %filename", get_line_no!, (__callsite,_filename)->
filename = nomsu\tree_to_value(_filename)
nomsu\use_file(filename)
return expr:"nomsu:use_file(#{repr filename})"
@ -1081,7 +1083,7 @@ if arg and debug_getinfo(2).func != require
files = setmetatable {}, {
__index: (filename)=>
file = io.open(filename)
source = file\read("*a")
source = file\read("a")
file\close!
self[filename] = source
return source
@ -1105,8 +1107,12 @@ if arg and debug_getinfo(2).func != require
line_no = 1
for _ in file\sub(1,tonumber(start))\gmatch("\n") do line_no += 1
info.short_src, info.linedefined = filename, line_no
-- TODO: Fix this to use actual current line
info.currentline = line_no
if type(select(1, ...)) == 'number'
varname, callsite = debug.getlocal(select(1,...), 1)
if varname == "__callsite"
info.short_src, info.currentline = callsite\match("^(.*):(%d+)$")
info.currentline = tonumber(info.currentline)
info.source = file
else
info.source = "@"..metadata.source
@ -1140,7 +1146,7 @@ if arg and debug_getinfo(2).func != require
else
local retval, code
if args.input == '-'
retval, code = nomsu\run(io.read('*a'), 'stdin')
retval, code = nomsu\run(io.read('a'), 'stdin')
else
retval, code = nomsu\run_file(args.input)
if compiled_output
@ -1181,7 +1187,7 @@ if arg and debug_getinfo(2).func != require
ok, to_lua = pcall -> require('moonscript.base').to_lua
if not ok then to_lua = -> nil
nomsu_file = io.open("nomsu.moon")
nomsu_source = nomsu_file\read("*a")
nomsu_source = nomsu_file\read("a")
_, line_table = to_lua(nomsu_source)
nomsu_file\close!
@ -1198,7 +1204,7 @@ if arg and debug_getinfo(2).func != require
if metadata = nomsu.action_metadata[calling_fn.func]
filename, start, stop = metadata.source\match("([^:]*):([0-9]*),([0-9]*)")
if filename
file = io.open(filename)\read("*a")
file = io.open(filename)\read("a")
line_no = 1
for _ in file\sub(1,tonumber(start))\gmatch("\n") do line_no += 1
offending_statement = file\sub(tonumber(start),tonumber(stop))
@ -1229,8 +1235,7 @@ if arg and debug_getinfo(2).func != require
-- Note: xpcall has a slightly different API in Lua <=5.1 vs. >=5.2, but this works
-- for both APIs
-- TODO: revert back to old error handler
ldt = require 'ldt'
ldt.guard run
require('ldt').guard run
--xpcall(run, err_hand)
return NomsuCompiler