Skip to content

Commit

Permalink
progress on intrinsic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LunNova committed Nov 26, 2023
1 parent 6e32f6c commit 240c6e3
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 180 deletions.
14 changes: 14 additions & 0 deletions alicorn-expressions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ local function inferred_expression_pairhandler(env, a, b)
-- temporary, while it isn't a Maybe
local data = operative_result_val.elements[1].primitive_value
local env = operative_result_val.elements[2].primitive_value
if not env then
print("operative_result_val.elements[2]", operative_result_val.elements[2]:pretty_print())
error "operative_result_val missing env"
end

-- FIXME: assert type is an inferrable term using new API once it exists
if not inferrable_term.value_check(data) then
Expand Down Expand Up @@ -342,12 +346,22 @@ end, "expressions")
-- return self.val(ops, env)
-- end

---@param fn fun(syntax : any, env : Environment) : boolean, any, Environment
---@return inferrable_term.operative_cons
local function primitive_operative(fn)
local aborting_fn = function(syn, env)
if not env or not env.exit_block then
error("env passed to primitive_operative isn't an env or is nil", env)
end
local ok, res, env = fn(syn, env)
if not ok then
error("Primitive operative apply failure, NYI convert to Maybe.\nError was:" .. tostring(res))
end
if not env or not env.exit_block then
local dinfo = debug.getinfo(fn)
print("env returned from fn passed to alicorn-expressins.primitive_operative isn't an env or is nil", env, " in ", dinfo.short_src, dinfo.linedefined)
error("invalid env from primitive_operative fn")
end
return res, env
end
-- what we're going for:
Expand Down
32 changes: 29 additions & 3 deletions base-env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ local function record_build(syntax, env)
return true, terms.inferrable_term.record_cons(map), env
end

---@param syntax any
---@param env Environment
---@return boolean
---@return any
---@return Environment
local function intrinsic(syntax, env)
local ok, str_env, syntax = syntax:match({
metalang.listtail(
Expand All @@ -111,12 +116,19 @@ local function intrinsic(syntax, env)
return ok, str_env
end
env = str_env.env
if not env then
error "env nil in base-env.intrinsic"
end
local str = terms.checkable_term.inferrable(str_env.val) -- workaround for not having exprs.checked_expression yet
local ok, type, env = syntax:match({
metalang.listmatch(metalang.accept_handler, exprs.inferred_expression(metalang.accept_handler, env)),
local ok, type_env = syntax:match({
metalang.listmatch(metalang.accept_handler, exprs.inferred_expression(utils.accept_with_env, env)),
}, metalang.failure_handler, nil)
if not ok then
return ok, type
return ok, type_env
end
local type, env = type_env.val, type_env.env
if not env then
error "env nil in base-env.intrinsic"
end
return true, terms.inferrable_term.prim_intrinsic(str, terms.checkable_term.inferrable(type)), env
end
Expand Down Expand Up @@ -297,6 +309,9 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)
--p(env)
print(env.get)
print(env.enter_block)
if not env.enter_block then
error "env isn't an environment in prim_func_type_impl_reducer"
end

local head, tail, name, type_val, type_env
local ok, continue = true, true
Expand Down Expand Up @@ -381,16 +396,27 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)

local fn_type_term = terms.inferrable_term.qtype(unrestricted_term, terms.inferrable_term.prim_function_type(args, results))
print("reached end of function type construction")
if not env.enter_block then
error "env isn't an environment at end in prim_func_type_impl_reducer"
end
return true, fn_type_term, env
end, "prim_func_type_impl")

-- TODO: abstract so can reuse for func type and prim func type
---@param syntax any
---@param env Environment
---@return unknown
---@return unknown
---@return unknown
local function prim_func_type_impl(syntax, env)
print("in prim_func_type_impl")
local ok, fn_type_term, env =
syntax:match({ prim_func_type_impl_reducer(metalang.accept_handler, env) }, metalang.failure_handler, env)
print("finished matching prim_func_type_impl")
if not ok then return ok, fn_type_term end
if not env.enter_block then
error "env isn't an environment at end in prim_func_type_impl"
end
return ok, fn_type_term, env
-- parse sequence of ascribed names, arrow, then sequence of ascribed names
-- for each ascribed name:
Expand Down
Loading

0 comments on commit 240c6e3

Please sign in to comment.