From c850d8b9fd7a212182551f317a6bcc6f72fd273a Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Wed, 13 Dec 2023 14:21:29 -0800 Subject: [PATCH 1/6] metalanguage: augment reducer errors with anchors --- metalanguage.lua | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/metalanguage.lua b/metalanguage.lua index 4f2fa6af..12330d73 100644 --- a/metalanguage.lua +++ b/metalanguage.lua @@ -68,9 +68,23 @@ end local reducer_mt = { __call = create_reducible } +local function augment_error(syntax, reducer_name, ok, err_msg, ...) + if not ok then + err_msg = "error occured while parsing anchor " + .. tostring(syntax.anchor) + .. " for reducer " + .. reducer_name + .. "\n" + .. tostring(err_msg) + return false, err_msg + end + -- err_msg is the first result arg otherwise + return err_msg, ... +end + local function reducer(func, name) local function funcwrapper(syntax, matcher) - return func(syntax, table.unpack(matcher.reducible)) + return augment_error(syntax, name, xpcall(func, debug.traceback, syntax, table.unpack(matcher.reducible))) end local reducer = { From 44bd1244b3adc6c2d2e6c14b2d4b5b05b457fbf5 Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Sun, 17 Dec 2023 23:27:39 -0800 Subject: [PATCH 2/6] alicorn-expressions, metalanguage: use error types to avoid repeated stack traces --- alicorn-expressions.lua | 37 +++++++++++++++++++++++++++++++------ metalanguage.lua | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/alicorn-expressions.lua b/alicorn-expressions.lua index 8435c651..daea503e 100644 --- a/alicorn-expressions.lua +++ b/alicorn-expressions.lua @@ -390,6 +390,36 @@ end, "expressions") -- return self.val(ops, env) -- end +---@class OperativeError +---@field cause any +---@field anchor any +---@field operative_name string +local OperativeError = {} +local external_error_mt = { + __tostring = function(self) + local message = "Lua error occured inside primitive operative " + .. self.operative_name + .. " at " + .. (self.anchor and tostring(self.anchor) or "") + .. ":\n" + .. tostring(self.cause) + return message + end, + __index = OperativeError, +} + +---@param cause any +---@param anchor any +---@param operative_name any +---@return OperativeError +function OperativeError.new(cause, anchor, operative_name) + return setmetatable({ + anchor = anchor, + cause = cause, + operative_name = operative_name, + }, external_error_mt) +end + ---@param fn fun(syntax : any, env : Environment) : boolean, any, Environment ---@param name string ---@return inferrable_term.operative_cons @@ -406,12 +436,7 @@ local function primitive_operative(fn, name) end local ok, res, env = fn(syn, env) if not ok then - error( - "Primitive operative " - .. debugstring - .. " apply failure, NYI convert to Maybe.\nError was:" - .. tostring(res) - ) + error(OperativeError.new(res, syn.anchor, debugstring)) end if not env or not env.exit_block then print( diff --git a/metalanguage.lua b/metalanguage.lua index 12330d73..5423538a 100644 --- a/metalanguage.lua +++ b/metalanguage.lua @@ -68,15 +68,39 @@ end local reducer_mt = { __call = create_reducible } +---@class ExternalError +---@field cause any +---@field anchor any +---@field reducer_name string +local ExternalError = {} +local external_error_mt = { + __tostring = function(self) + local message = "Lua error raised inside reducer " + .. self.reducer_name + .. " at " + .. (self.anchor and tostring(self.anchor) or "") + .. ":\n" + .. tostring(self.cause) + return message + end, + __index = ExternalError, +} + +---@param cause any +---@param anchor any +---@param reducer_name any +---@return ExternalError +function ExternalError.new(cause, anchor, reducer_name) + return setmetatable({ + anchor = anchor, + cause = cause, + reducer_name = reducer_name, + }, external_error_mt) +end + local function augment_error(syntax, reducer_name, ok, err_msg, ...) if not ok then - err_msg = "error occured while parsing anchor " - .. tostring(syntax.anchor) - .. " for reducer " - .. reducer_name - .. "\n" - .. tostring(err_msg) - return false, err_msg + return false, ExternalError.new(err_msg, syntax.anchor, reducer_name) end -- err_msg is the first result arg otherwise return err_msg, ... From 65495d1a4f2b1fbdc290ed5eff2ba4e7d2dca7b2 Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Sun, 17 Dec 2023 23:48:32 -0800 Subject: [PATCH 3/6] runtest: use filename instead of inline --- runtest.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtest.lua b/runtest.lua index a5aa5a1b..fddc1ce5 100644 --- a/runtest.lua +++ b/runtest.lua @@ -7,11 +7,12 @@ local terms = require "./terms" local exprs = require "./alicorn-expressions" local fs = require "fs" -local src = fs.readFileSync("testfile.alc") +local filename = "testfile.alc" +local src = fs.readFileSync(filename) print("read code") print(src) print("parsing code") -local code = format.read(src, "inline") +local code = format.read(src, filename) -- p(code) local env = base_env.create() From cc6e4d7643eec9eaa99774cb74a423aadccf34a3 Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Sun, 17 Dec 2023 23:49:28 -0800 Subject: [PATCH 4/6] alicorn-expressions,metalanguage: fix 'at in ' in anchor output --- alicorn-expressions.lua | 4 ++-- metalanguage.lua | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/alicorn-expressions.lua b/alicorn-expressions.lua index daea503e..b0d8e3d8 100644 --- a/alicorn-expressions.lua +++ b/alicorn-expressions.lua @@ -399,8 +399,8 @@ local external_error_mt = { __tostring = function(self) local message = "Lua error occured inside primitive operative " .. self.operative_name - .. " at " - .. (self.anchor and tostring(self.anchor) or "") + .. " " + .. (self.anchor and tostring(self.anchor) or " at unknown position") .. ":\n" .. tostring(self.cause) return message diff --git a/metalanguage.lua b/metalanguage.lua index 5423538a..37f6307c 100644 --- a/metalanguage.lua +++ b/metalanguage.lua @@ -77,8 +77,8 @@ local external_error_mt = { __tostring = function(self) local message = "Lua error raised inside reducer " .. self.reducer_name - .. " at " - .. (self.anchor and tostring(self.anchor) or "") + .. " " + .. (self.anchor and tostring(self.anchor) or "at unknown position") .. ":\n" .. tostring(self.cause) return message From 138534b33678879a36a797f8a4439d341c121571 Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Tue, 19 Dec 2023 00:49:34 -0800 Subject: [PATCH 5/6] evaluator: debugging for prim_unbox missing as_prim --- evaluator.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/evaluator.lua b/evaluator.lua index 1d0cb42a..73520af4 100644 --- a/evaluator.lua +++ b/evaluator.lua @@ -1629,7 +1629,12 @@ function evaluate(typed_term, runtime_context) local content = typed_term:unwrap_prim_box() return value.prim(evaluate(content, runtime_context)) elseif typed_term:is_prim_unbox() then - local ok, prim = typed_term:unwrap_prim_unbox():as_prim() + local unwrapped = typed_term:unwrap_prim_unbox() + if not unwrapped.as_prim then + print("unwrapped", unwrapped) + error "evaluate, is_prim_unbox: missing as_prim on unwrapped prim_unbox" + end + local ok, prim = unwrapped:as_prim() if not ok then error "evaluate, is_prim_unbox: expected a prim" end From c6b75a6c9efe1a1234d7eb15c0f8535d6df046bf Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Tue, 19 Dec 2023 00:49:50 -0800 Subject: [PATCH 6/6] testfile: fix missing unbox --- testfile.alc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testfile.alc b/testfile.alc index 43df8d0d..b291827d 100644 --- a/testfile.alc +++ b/testfile.alc @@ -41,7 +41,7 @@ let prim-array-set = array[index] = elem end : - prim-func-type (T : (boxed prim-type)) (arr : (array-type T)) (index : prim-number) (elem : (unbox prim-type T)) -> + prim-func-type (T : (boxed prim-type)) (arr : (array-type (unbox prim-type T))) (index : prim-number) (elem : (unbox prim-type T)) -> let prim-array-get = intrinsic