Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expressions,base-env: improve diagnostics for prim_func_type and collect_tuple #52

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions alicorn-expressions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ end

local function collect_tuple_pair_too_many_handler(args)
local target, env = args:unwrap()
return false, "tuple has too many elements", nil, nil, env
return false, "tuple has too many elements for checked collect_tuple", nil, nil, env
end

local function collect_tuple_nil_handler(args)
Expand All @@ -528,7 +528,7 @@ end

local function collect_tuple_nil_too_few_handler(args)
local target, env = args:unwrap()
return false, "tuple has too few elements", nil, nil, env
return false, "tuple has too few elements for checked collect_tuple", nil, nil, env
end

collect_tuple = metalanguage.reducer(function(syntax, args)
Expand Down Expand Up @@ -567,6 +567,14 @@ collect_tuple = metalanguage.reducer(function(syntax, args)
collected_terms:append(next_term)
end
end
if not ok then
continue = continue
.. " (should have "
.. tostring(#closures)
.. ", found "
.. tostring(#collected_terms)
.. " so far)"
end
-- else we don't know how many elems so nil or pair are both valid
else
ok, continue, next_term, syntax, env = syntax:match({
Expand Down Expand Up @@ -627,6 +635,14 @@ collect_prim_tuple = metalanguage.reducer(function(syntax, args)
collected_terms:append(next_term)
end
end
if not ok then
continue = continue
.. " (should have "
.. tostring(#closures)
.. ", found "
.. tostring(#collected_terms)
.. " so far)"
end
-- else we don't know how many elems so nil or pair are both valid
else
ok, continue, next_term, syntax, env = syntax:match({
Expand Down
45 changes: 15 additions & 30 deletions base-env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,30 +297,22 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)
end

local names = gen.declare_array(gen.builtin_string)()
print("is env an environment? (before loop)")
--p(env)
print(env.get)
print(env.enter_block)
if not env.enter_block then

if not env or 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
while ok and continue do
ok, head, tail = syntax:match({ metalang.ispair(metalang.accept_handler) }, metalang.failure_handler, env)
print(env)
if not ok then
break
end

print("is env an environment? (in loop)")
--p(env)
print(env.get)
print(env.enter_block)

print "args in loop is"
print(args:pretty_print())
if not env or not env.enter_block then
error "env isn't an environment in prim_func_type_impl_reducer"
end

ok, continue, name, type_val, type_env = head:match({
metalang.symbol_exact(function()
Expand All @@ -331,19 +323,12 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)
end, env, build_type_term(args), names),
}, metalang.failure_handler, env)

if continue then
-- type_env:bind_local(terms.binding.let(name, type_val))
-- local arg = nil
-- args = cons(args, arg)
end
if ok and continue then
env = type_env
args = cons(args, type_val)
names = names:copy()
names:append(name)
end
print("arg", ok, continue, name, type_val, type_env)
--error "TODO use ascribed_name results"

syntax = tail
end
Expand All @@ -357,11 +342,15 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)
local results = empty
while ok and continue do
ok, head, tail = syntax:match({ metalang.ispair(metalang.accept_handler) }, metalang.failure_handler, env)
print(env)

if not ok then
break
end

if not env or not env.enter_block then
error "env isn't an environment in prim_func_type_impl_reducer"
end

ok, continue, name, type_val, type_env = head:match({
metalang.isnil(function()
return true, false
Expand All @@ -370,7 +359,8 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)
return ok, true, ...
end, env, build_type_term(results), names),
}, metalang.failure_handler, env)
print("result", ok, continue, name, type_val, type_env)

--print("result", ok, continue, name, type_val, type_env)

if ok and continue then
env = type_env
Expand All @@ -380,6 +370,7 @@ local prim_func_type_impl_reducer = metalang.reducer(function(syntax, env)
end

if not ok then
--print("prim_func_type_impl failed and returning ", continue)
return false, continue
end

Expand Down Expand Up @@ -408,20 +399,14 @@ 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 and got")
print(fn_type_term:pretty_print())
if not ok then
return ok, fn_type_term
end
if not env.enter_block then
print("finished matching prim_func_type_impl and got (ok, fn_type_term)", ok, fn_type_term)
if not env or 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:
-- enter a block, perform lambda binding, perform tuple destrucutring binding, parse out the type of the ascription

--local ok,
end

local forall_type_impl_reducer = metalang.reducer(function(syntax, env)
Expand Down