mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-28 00:59:31 +00:00
Prefix type checkers with "is", stdlib cleanup
This commit is contained in:
parent
98917c2ca4
commit
7f71569e07
25 changed files with 163 additions and 160 deletions
43
anselme/stdlib/typed.lua
Normal file
43
anselme/stdlib/typed.lua
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
local ast = require("anselme.ast")
|
||||
local ArgumentTuple, String, Typed = ast.ArgumentTuple, ast.String, ast.Typed
|
||||
|
||||
return {
|
||||
{
|
||||
"_::_", "(value, check)",
|
||||
function(state, value, check)
|
||||
local r = check:call(state, ArgumentTuple:new(value))
|
||||
if r:truthy() then
|
||||
return value
|
||||
else
|
||||
error(("type check failure: %s does not satisfy %s"):format(value:format(state), check:format(state)), 0)
|
||||
end
|
||||
end
|
||||
},
|
||||
|
||||
{
|
||||
"type", "(value)",
|
||||
function(state, v)
|
||||
if v.type == "typed" then
|
||||
return v.type_expression
|
||||
else
|
||||
return String:new(v.type)
|
||||
end
|
||||
end
|
||||
},
|
||||
{
|
||||
"type", "(value, type)",
|
||||
function(state, v, t)
|
||||
return Typed:new(v, t)
|
||||
end
|
||||
},
|
||||
{
|
||||
"value", "(value)",
|
||||
function(state, v)
|
||||
if v.type == "typed" then
|
||||
return v.expression
|
||||
else
|
||||
return v
|
||||
end
|
||||
end
|
||||
},
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue