mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49: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
|
|
@ -7,7 +7,7 @@ local block_identifier = Identifier:new("_")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
"attached block", "(level::number=1, keep return=false)",
|
"attached block", "(level::is number=1, keep return=false)",
|
||||||
function(state, level, keep_return)
|
function(state, level, keep_return)
|
||||||
-- level 2: env of the function that called the function that called attached block
|
-- level 2: env of the function that called the function that called attached block
|
||||||
local env = calling_environment_manager:get_level(state, level:to_lua(state)+1)
|
local env = calling_environment_manager:get_level(state, level:to_lua(state)+1)
|
||||||
|
|
@ -25,7 +25,7 @@ return {
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attached block", "(level::number=1, keep return=false, default)",
|
"attached block", "(level::is number=1, keep return=false, default)",
|
||||||
function(state, level, keep_return, default)
|
function(state, level, keep_return, default)
|
||||||
-- level 2: env of the function that called the function that called attached block
|
-- level 2: env of the function that called the function that called attached block
|
||||||
local env = calling_environment_manager:get_level(state, level:to_lua(state)+1)
|
local env = calling_environment_manager:get_level(state, level:to_lua(state)+1)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
return [[
|
|
||||||
:@$is(t) $(x) x!type == t
|
|
||||||
:@$equal(x) $(y) x == y
|
|
||||||
]]
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
return [[
|
return [[
|
||||||
/* For loop */
|
/* For loop */
|
||||||
:@$for(symbol::symbol, var, block=attached block(keep return=true))
|
:@$for(symbol::is symbol, var, block=attached block(keep return=true))
|
||||||
:iterator = iter(var)
|
:iterator = iter(var)
|
||||||
:value = iterator()
|
:value = iterator()
|
||||||
:name = symbol!to string
|
:name = symbol!to string
|
||||||
|
|
@ -12,11 +12,12 @@ return [[
|
||||||
r
|
r
|
||||||
|
|
||||||
/* Range iterables */
|
/* Range iterables */
|
||||||
|
:@is range = is("range")
|
||||||
:@$range(stop)
|
:@$range(stop)
|
||||||
[1, stop, 1]!type("range")
|
[1, stop, 1]!type("range")
|
||||||
:@$range(start, stop, step=1)
|
:@$range(start, stop, step=1)
|
||||||
[start, stop, step]!type("range")
|
[start, stop, step]!type("range")
|
||||||
:@$iter(range::is("range"))
|
:@$iter(range::is range)
|
||||||
:v = range!value
|
:v = range!value
|
||||||
:start = v(1)
|
:start = v(1)
|
||||||
:stop = v(2)
|
:stop = v(2)
|
||||||
|
|
@ -34,8 +35,7 @@ return [[
|
||||||
return(i-step)
|
return(i-step)
|
||||||
|
|
||||||
/* List/tuple iterables */
|
/* List/tuple iterables */
|
||||||
:tuple or list = $(x) x!type == "tuple" | x!type == "list"
|
:@$iter(tuple::is sequence)
|
||||||
:@$iter(tuple::tuple or list)
|
|
||||||
:n = tuple!len
|
:n = tuple!len
|
||||||
:i = 0
|
:i = 0
|
||||||
$
|
$
|
||||||
|
|
@ -44,7 +44,7 @@ return [[
|
||||||
return(tuple(i))
|
return(tuple(i))
|
||||||
|
|
||||||
/* Table */
|
/* Table */
|
||||||
:@$iter(table::table)
|
:@$iter(table::is table)
|
||||||
:s = table!to struct
|
:s = table!to struct
|
||||||
iter(s)
|
iter(s)
|
||||||
]]
|
]]
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,13 @@ local assert0 = require("anselme.common").assert0
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
"defined", "(c::function, s::string)",
|
"defined", "(c::is function, s::is string)",
|
||||||
function(state, c, s)
|
function(state, c, s)
|
||||||
return Boolean:new(c.scope:defined_in_current(state, s:to_identifier()))
|
return Boolean:new(c.scope:defined_in_current(state, s:to_identifier()))
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"has upvalue", "(c::function, s::string)",
|
"has upvalue", "(c::is function, s::is string)",
|
||||||
function(state, c, s)
|
function(state, c, s)
|
||||||
return Boolean:new(c.scope:defined(state, s:to_identifier()))
|
return Boolean:new(c.scope:defined(state, s:to_identifier()))
|
||||||
end
|
end
|
||||||
|
|
@ -25,7 +25,7 @@ return {
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_._", "(c::function, s::string) = v",
|
"_._", "(c::is function, s::is string) = v",
|
||||||
function(state, c, s, v)
|
function(state, c, s, v)
|
||||||
local identifier = s:to_identifier()
|
local identifier = s:to_identifier()
|
||||||
assert0(c.scope:defined(state, identifier), ("no variable %q defined in closure"):format(s.string))
|
assert0(c.scope:defined(state, identifier), ("no variable %q defined in closure"):format(s.string))
|
||||||
|
|
@ -34,7 +34,7 @@ return {
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_._", "(c::function, s::symbol) = v",
|
"_._", "(c::is function, s::is symbol) = v",
|
||||||
function(state, c, s, v)
|
function(state, c, s, v)
|
||||||
state.scope:push(c.scope)
|
state.scope:push(c.scope)
|
||||||
local r = Definition:new(s, v):eval(state)
|
local r = Definition:new(s, v):eval(state)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ local function load(state, l)
|
||||||
for _, m in ipairs(l) do
|
for _, m in ipairs(l) do
|
||||||
local mod = require("anselme.stdlib."..m)
|
local mod = require("anselme.stdlib."..m)
|
||||||
if type(mod) == "string" then
|
if type(mod) == "string" then
|
||||||
parser(mod, m..".ans"):eval(state)
|
parser(mod, "stdlib/"..m..".ans"):eval(state)
|
||||||
else
|
else
|
||||||
define_lua(state, mod)
|
define_lua(state, mod)
|
||||||
end
|
end
|
||||||
|
|
@ -22,8 +22,8 @@ return function(main_state)
|
||||||
"tag",
|
"tag",
|
||||||
"conditionals",
|
"conditionals",
|
||||||
"base",
|
"base",
|
||||||
"types",
|
"typed",
|
||||||
"boot",
|
"type check",
|
||||||
"number",
|
"number",
|
||||||
"string",
|
"string",
|
||||||
"symbol",
|
"symbol",
|
||||||
|
|
|
||||||
|
|
@ -5,58 +5,58 @@ return {
|
||||||
{ "pi", Number:new(math.pi) },
|
{ "pi", Number:new(math.pi) },
|
||||||
|
|
||||||
{
|
{
|
||||||
"_<_", "(a::number, b::number)",
|
"_<_", "(a::is number, b::is number)",
|
||||||
function(state, a, b)
|
function(state, a, b)
|
||||||
if a.number < b.number then return b
|
if a.number < b.number then return b
|
||||||
else return Boolean:new(false)
|
else return Boolean:new(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{ "_<_", "(a::equal(false), b::number)", function(state, a, b) return Boolean:new(false) end },
|
{ "_<_", "(a::equal(false), b::is number)", function(state, a, b) return Boolean:new(false) end },
|
||||||
{
|
{
|
||||||
"_<=_", "(a::number, b::number)",
|
"_<=_", "(a::is number, b::is number)",
|
||||||
function(state, a, b)
|
function(state, a, b)
|
||||||
if a.number <= b.number then return b
|
if a.number <= b.number then return b
|
||||||
else return Boolean:new(false)
|
else return Boolean:new(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{ "_<=_", "(a::equal(false), b::number)", function(state, a, b) return Boolean:new(false) end },
|
{ "_<=_", "(a::equal(false), b::is number)", function(state, a, b) return Boolean:new(false) end },
|
||||||
{
|
{
|
||||||
"_>_", "(a::number, b::number)",
|
"_>_", "(a::is number, b::is number)",
|
||||||
function(state, a, b)
|
function(state, a, b)
|
||||||
if a.number > b.number then return b
|
if a.number > b.number then return b
|
||||||
else return Boolean:new(false)
|
else return Boolean:new(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{ "_>_", "(a::equal(false), b::number)", function(state, a, b) return Boolean:new(false) end },
|
{ "_>_", "(a::equal(false), b::is number)", function(state, a, b) return Boolean:new(false) end },
|
||||||
{
|
{
|
||||||
"_>=_", "(a::number, b::number)",
|
"_>=_", "(a::is number, b::is number)",
|
||||||
function(state, a, b)
|
function(state, a, b)
|
||||||
if a.number >= b.number then return b
|
if a.number >= b.number then return b
|
||||||
else return Boolean:new(false)
|
else return Boolean:new(false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{ "_>=_", "(a::equal(false), b::number)", function(state, a, b) return Boolean:new(false) end },
|
{ "_>=_", "(a::equal(false), b::is number)", function(state, a, b) return Boolean:new(false) end },
|
||||||
{ "_+_", "(a::number, b::number)", function(state, a, b) return Number:new(a.number + b.number) end },
|
{ "_+_", "(a::is number, b::is number)", function(state, a, b) return Number:new(a.number + b.number) end },
|
||||||
{ "_-_", "(a::number, b::number)", function(state, a, b) return Number:new(a.number - b.number) end },
|
{ "_-_", "(a::is number, b::is number)", function(state, a, b) return Number:new(a.number - b.number) end },
|
||||||
{ "_*_", "(a::number, b::number)", function(state, a, b) return Number:new(a.number * b.number) end },
|
{ "_*_", "(a::is number, b::is number)", function(state, a, b) return Number:new(a.number * b.number) end },
|
||||||
{ "_/_", "(a::number, b::number)", function(state, a, b) return Number:new(a.number / b.number) end },
|
{ "_/_", "(a::is number, b::is number)", function(state, a, b) return Number:new(a.number / b.number) end },
|
||||||
{ "_//_", "(a::number, b::number)", function(state, a, b) return Number:new(math.floor(a.number / b.number)) end },
|
{ "_//_", "(a::is number, b::is number)", function(state, a, b) return Number:new(math.floor(a.number / b.number)) end },
|
||||||
{ "_%_", "(a::number, b::number)", function(state, a, b) return Number:new(a.number % b.number) end },
|
{ "_%_", "(a::is number, b::is number)", function(state, a, b) return Number:new(a.number % b.number) end },
|
||||||
{ "_^_", "(a::number, b::number)", function(state, a, b) return Number:new(a.number ^ b.number) end },
|
{ "_^_", "(a::is number, b::is number)", function(state, a, b) return Number:new(a.number ^ b.number) end },
|
||||||
{ "-_", "(a::number)", function(state, a) return Number:new(-a.number) end },
|
{ "-_", "(a::is number)", function(state, a) return Number:new(-a.number) end },
|
||||||
|
|
||||||
{ "rand", "(min::number, max::number)", function(state, min, max) return Number:new(math.random(min.number, max.number)) end },
|
{ "rand", "(min::is number, max::is number)", function(state, min, max) return Number:new(math.random(min.number, max.number)) end },
|
||||||
{ "rand", "(max::number)", function(state, max) return Number:new(math.random(max.number)) end },
|
{ "rand", "(max::is number)", function(state, max) return Number:new(math.random(max.number)) end },
|
||||||
{ "rand", "()", function(state) return Number:new(math.random()) end },
|
{ "rand", "()", function(state) return Number:new(math.random()) end },
|
||||||
|
|
||||||
{ "floor", "(x::number)", function(state, x) return Number:new(math.floor(x.number)) end },
|
{ "floor", "(x::is number)", function(state, x) return Number:new(math.floor(x.number)) end },
|
||||||
{ "ceil", "(x::number)", function(state, x) return Number:new(math.ceil(x.number)) end },
|
{ "ceil", "(x::is number)", function(state, x) return Number:new(math.ceil(x.number)) end },
|
||||||
{
|
{
|
||||||
"round", "(x::number, increment=1)",
|
"round", "(x::is number, increment=1)",
|
||||||
function(state, x, increment)
|
function(state, x, increment)
|
||||||
local n = x.number / increment.number
|
local n = x.number / increment.number
|
||||||
if n >= 0 then
|
if n >= 0 then
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,13 @@ return {
|
||||||
{ "_:_", "(name, value)", function(state, a, b) return Pair:new(a,b) end },
|
{ "_:_", "(name, value)", function(state, a, b) return Pair:new(a,b) end },
|
||||||
|
|
||||||
{
|
{
|
||||||
"name", "(pair::pair)",
|
"name", "(pair::is pair)",
|
||||||
function(state, pair)
|
function(state, pair)
|
||||||
return pair.name
|
return pair.name
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"value", "(pair::pair)",
|
"value", "(pair::is pair)",
|
||||||
function(state, pair)
|
function(state, pair)
|
||||||
return pair.value
|
return pair.value
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@ local calling_environment_manager = require("anselme.state.calling_environment_m
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{
|
{
|
||||||
"from", "(function::function, anchor::anchor)",
|
"from", "(function::is function, anchor::is anchor)",
|
||||||
function(state, func, anchor)
|
function(state, func, anchor)
|
||||||
return func:resume(state, anchor)
|
return func:resume(state, anchor)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"from", "(function::function, anchor::nil)",
|
"from", "(function::is function, anchor::is nil)",
|
||||||
function(state, func)
|
function(state, func)
|
||||||
return func:call(state, ArgumentTuple:new())
|
return func:call(state, ArgumentTuple:new())
|
||||||
end
|
end
|
||||||
|
|
@ -24,7 +24,7 @@ return {
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"resuming", "(level::number=0)",
|
"resuming", "(level::is number=0)",
|
||||||
function(state, level)
|
function(state, level)
|
||||||
local env = calling_environment_manager:get_level(state, level:to_lua(state)+1)
|
local env = calling_environment_manager:get_level(state, level:to_lua(state)+1)
|
||||||
state.scope:push(env)
|
state.scope:push(env)
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,9 @@ return [[
|
||||||
|
|
||||||
:resume target = ()
|
:resume target = ()
|
||||||
|
|
||||||
fn.:check = $(anchor::anchor)
|
fn.:check = $(anchor::is anchor)
|
||||||
fn.reached(anchor) = (fn.reached(anchor) | 0) + 1
|
fn.reached(anchor) = (fn.reached(anchor) | 0) + 1
|
||||||
fn.:checkpoint = $(anchor::anchor, on resume=attached block(default=()))
|
fn.:checkpoint = $(anchor::is anchor, on resume=attached block(default=()))
|
||||||
if(on resume)
|
if(on resume)
|
||||||
fn.current checkpoint = anchor
|
fn.current checkpoint = anchor
|
||||||
if(resume target == anchor | resuming(4))
|
if(resume target == anchor | resuming(4))
|
||||||
|
|
@ -38,14 +38,14 @@ return [[
|
||||||
:@$_!(s::is script)
|
:@$_!(s::is script)
|
||||||
s!value!
|
s!value!
|
||||||
|
|
||||||
:@$_._(s::is script, k::string)
|
:@$_._(s::is script, k::is string)
|
||||||
(s!value).fn.(k)
|
(s!value).fn.(k)
|
||||||
:@$_._(s::is script, k::string) = val
|
:@$_._(s::is script, k::is string) = val
|
||||||
(s!value).fn.(k) = val
|
(s!value).fn.(k) = val
|
||||||
:@$_._(s::is script, k::symbol) = val
|
:@$_._(s::is script, k::is symbol) = val
|
||||||
(s!value).fn.(k) = val
|
(s!value).fn.(k) = val
|
||||||
|
|
||||||
:@$from(s::is script, a::anchor)
|
:@$from(s::is script, a::is anchor)
|
||||||
s.current checkpoint = a
|
s.current checkpoint = a
|
||||||
return(s!)
|
return(s!)
|
||||||
:@$from(s::is script)
|
:@$from(s::is script)
|
||||||
|
|
@ -53,7 +53,7 @@ return [[
|
||||||
return(s!)
|
return(s!)
|
||||||
|
|
||||||
/* Additionnal helpers */
|
/* Additionnal helpers */
|
||||||
:@$ cycle(l::tuple)
|
:@$ cycle(l::is tuple)
|
||||||
:i = 2
|
:i = 2
|
||||||
while($i <= l!len)
|
while($i <= l!len)
|
||||||
if(l(i).run < l(1).run)
|
if(l(i).run < l(1).run)
|
||||||
|
|
@ -61,7 +61,7 @@ return [[
|
||||||
i += 1
|
i += 1
|
||||||
l(1)!
|
l(1)!
|
||||||
|
|
||||||
:@$ next(l::tuple)
|
:@$ next(l::is tuple)
|
||||||
:i = 1
|
:i = 1
|
||||||
while($i <= l!len)
|
while($i <= l!len)
|
||||||
if(l(i).run == 0)
|
if(l(i).run == 0)
|
||||||
|
|
@ -69,6 +69,6 @@ return [[
|
||||||
i += 1
|
i += 1
|
||||||
l(i-1)!
|
l(i-1)!
|
||||||
|
|
||||||
:@$ random(l::tuple)
|
:@$ random(l::is tuple)
|
||||||
l(rand(1, l!len))!
|
l(rand(1, l!len))!
|
||||||
]]
|
]]
|
||||||
|
|
@ -3,9 +3,9 @@ local ast = require("anselme.ast")
|
||||||
local String, Number = ast.String, ast.Number
|
local String, Number = ast.String, ast.Number
|
||||||
|
|
||||||
return {
|
return {
|
||||||
{ "_+_", "(a::string, b::string)", function(state, a, b) return String:new(a.string .. b.string) end },
|
{ "_+_", "(a::is string, b::is string)", function(state, a, b) return String:new(a.string .. b.string) end },
|
||||||
{
|
{
|
||||||
"len", "(s::string)",
|
"len", "(s::is string)",
|
||||||
function(state, s)
|
function(state, s)
|
||||||
return Number:new(utf8.len(s.string))
|
return Number:new(utf8.len(s.string))
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,25 +4,25 @@ local Nil, List, Table, Number, LuaFunction, ParameterTuple, Boolean = ast.Nil,
|
||||||
return {
|
return {
|
||||||
-- tuple
|
-- tuple
|
||||||
{
|
{
|
||||||
"*_", "(t::tuple)",
|
"*_", "(t::is tuple)",
|
||||||
function(state, tuple)
|
function(state, tuple)
|
||||||
return List:new(state, tuple)
|
return List:new(state, tuple)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_!", "(l::tuple, i::number)",
|
"_!", "(l::is tuple, i::is number)",
|
||||||
function(state, l, i)
|
function(state, l, i)
|
||||||
return l:get(i.number)
|
return l:get(i.number)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"len", "(l::tuple)",
|
"len", "(l::is tuple)",
|
||||||
function(state, l)
|
function(state, l)
|
||||||
return Number:new(l:len())
|
return Number:new(l:len())
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"find", "(l::tuple, value)",
|
"find", "(l::is tuple, value)",
|
||||||
function(state, l, v)
|
function(state, l, v)
|
||||||
local i = l:find(v)
|
local i = l:find(v)
|
||||||
if i then
|
if i then
|
||||||
|
|
@ -35,26 +35,26 @@ return {
|
||||||
|
|
||||||
-- list
|
-- list
|
||||||
{
|
{
|
||||||
"_!", "(l::list, i::number)",
|
"_!", "(l::is list, i::is number)",
|
||||||
function(state, l, i)
|
function(state, l, i)
|
||||||
return l:get(state, i.number)
|
return l:get(state, i.number)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_!", "(l::list, i::number) = value",
|
"_!", "(l::is list, i::is number) = value",
|
||||||
function(state, l, i, v)
|
function(state, l, i, v)
|
||||||
l:set(state, i.number, v)
|
l:set(state, i.number, v)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"len", "(l::list)",
|
"len", "(l::is list)",
|
||||||
function(state, l)
|
function(state, l)
|
||||||
return Number:new(l:len(state))
|
return Number:new(l:len(state))
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"find", "(l::list, value)",
|
"find", "(l::is list, value)",
|
||||||
function(state, l, v)
|
function(state, l, v)
|
||||||
local i = l:find(state, v)
|
local i = l:find(state, v)
|
||||||
if i then
|
if i then
|
||||||
|
|
@ -65,35 +65,35 @@ return {
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"insert", "(l::list, value)",
|
"insert", "(l::is list, value)",
|
||||||
function(state, l, v)
|
function(state, l, v)
|
||||||
l:insert(state, v)
|
l:insert(state, v)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"insert", "(l::list, position::number, value)",
|
"insert", "(l::is list, position::is number, value)",
|
||||||
function(state, l, position, v)
|
function(state, l, position, v)
|
||||||
l:insert(state, position.number, v)
|
l:insert(state, position.number, v)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"remove", "(l::list)",
|
"remove", "(l::is list)",
|
||||||
function(state, l)
|
function(state, l)
|
||||||
l:remove(state)
|
l:remove(state)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"remove", "(l::list, position::number)",
|
"remove", "(l::is list, position::is number)",
|
||||||
function(state, l, position)
|
function(state, l, position)
|
||||||
l:remove(state, position.number)
|
l:remove(state, position.number)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"to tuple", "(l::list)",
|
"to tuple", "(l::is list)",
|
||||||
function(state, l)
|
function(state, l)
|
||||||
return l:to_tuple(state)
|
return l:to_tuple(state)
|
||||||
end
|
end
|
||||||
|
|
@ -101,25 +101,25 @@ return {
|
||||||
|
|
||||||
-- struct
|
-- struct
|
||||||
{
|
{
|
||||||
"*_", "(s::struct)",
|
"*_", "(s::is struct)",
|
||||||
function(state, struct)
|
function(state, struct)
|
||||||
return Table:new(state, struct)
|
return Table:new(state, struct)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_!", "(s::struct, key)",
|
"_!", "(s::is struct, key)",
|
||||||
function(state, s, k)
|
function(state, s, k)
|
||||||
return s:get(k)
|
return s:get(k)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"has", "(s::struct, key)",
|
"has", "(s::is struct, key)",
|
||||||
function(state, s, k)
|
function(state, s, k)
|
||||||
return Boolean:new(s:has(k))
|
return Boolean:new(s:has(k))
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"iter", "(s::struct)",
|
"iter", "(s::is struct)",
|
||||||
function(state, struct)
|
function(state, struct)
|
||||||
local iter = struct:iter()
|
local iter = struct:iter()
|
||||||
return LuaFunction:new(ParameterTuple:new(), function()
|
return LuaFunction:new(ParameterTuple:new(), function()
|
||||||
|
|
@ -132,26 +132,26 @@ return {
|
||||||
|
|
||||||
-- table
|
-- table
|
||||||
{
|
{
|
||||||
"_!", "(t::table, key)",
|
"_!", "(t::is table, key)",
|
||||||
function(state, t, key)
|
function(state, t, key)
|
||||||
return t:get(state, key)
|
return t:get(state, key)
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_!", "(t::table, key) = value",
|
"_!", "(t::is table, key) = value",
|
||||||
function(state, t, key, value)
|
function(state, t, key, value)
|
||||||
t:set(state, key, value)
|
t:set(state, key, value)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"has", "(t::table, key)",
|
"has", "(t::is table, key)",
|
||||||
function(state, t, k)
|
function(state, t, k)
|
||||||
return Boolean:new(t:has(state, k))
|
return Boolean:new(t:has(state, k))
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"to struct", "(t::table)",
|
"to struct", "(t::is table)",
|
||||||
function(state, t)
|
function(state, t)
|
||||||
return t:to_struct(state)
|
return t:to_struct(state)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
return {
|
return {
|
||||||
|
|
||||||
{
|
{
|
||||||
"to string", "(symbol::symbol)",
|
"to string", "(symbol::is symbol)",
|
||||||
function(state, sym)
|
function(state, sym)
|
||||||
return sym:to_string()
|
return sym:to_string()
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ local resume_manager = require("anselme.state.resume_manager")
|
||||||
return {
|
return {
|
||||||
-- text
|
-- text
|
||||||
{
|
{
|
||||||
"_+_", "(a::text, b::text)",
|
"_+_", "(a::is text, b::is text)",
|
||||||
function(state, a, b)
|
function(state, a, b)
|
||||||
local r = Text:new()
|
local r = Text:new()
|
||||||
for _, e in ipairs(a.list) do
|
for _, e in ipairs(a.list) do
|
||||||
|
|
@ -22,7 +22,7 @@ return {
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_!", "(txt::text)",
|
"_!", "(txt::is text)",
|
||||||
function(state, text)
|
function(state, text)
|
||||||
event_manager:write(state, text)
|
event_manager:write(state, text)
|
||||||
return Nil:new()
|
return Nil:new()
|
||||||
|
|
@ -31,7 +31,7 @@ return {
|
||||||
|
|
||||||
-- choice
|
-- choice
|
||||||
{
|
{
|
||||||
"_|>_", "(txt::text, fn)",
|
"_|>_", "(txt::is text, fn)",
|
||||||
function(state, text, func)
|
function(state, text, func)
|
||||||
if func:contains_current_resume_target(state) then
|
if func:contains_current_resume_target(state) then
|
||||||
func:call(state, ArgumentTuple:new())
|
func:call(state, ArgumentTuple:new())
|
||||||
|
|
|
||||||
26
anselme/stdlib/type check.lua
Normal file
26
anselme/stdlib/type check.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
return [[
|
||||||
|
:@$is(t) $(x) x!type == t
|
||||||
|
:@$equal(x) $(y) x == y
|
||||||
|
|
||||||
|
:@is nil = is("nil")
|
||||||
|
:@is number = is("number")
|
||||||
|
:@is string = is("string")
|
||||||
|
:@is boolean = is("boolean")
|
||||||
|
:@is symbol = is("symbol")
|
||||||
|
:@is anchor = is("anchor")
|
||||||
|
:@is pair = is("pair")
|
||||||
|
|
||||||
|
:@is text = is("text")
|
||||||
|
|
||||||
|
:@is sequence = $(x) x!type == "tuple" | x!type == "list"
|
||||||
|
:@is tuple = is("tuple")
|
||||||
|
:@is list = is("list")
|
||||||
|
|
||||||
|
:@is map = $(x) x!type == "struct" | x!type == "table"
|
||||||
|
:@is struct = is("struct")
|
||||||
|
:@is table = is("table")
|
||||||
|
|
||||||
|
:@is function = is("function")
|
||||||
|
:@is overload = is("overload")
|
||||||
|
:@is callable = $(x) x!type == "overload" | x!type == "function" | x!type == "lua function" | x!type == "quote"
|
||||||
|
]]
|
||||||
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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
local ast = require("anselme.ast")
|
|
||||||
local ArgumentTuple, Boolean, String, Typed = ast.ArgumentTuple, ast.Boolean, 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
|
|
||||||
},
|
|
||||||
|
|
||||||
{ "nil", "(x)", function(state, x) return Boolean:new(x.type == "nil") end },
|
|
||||||
{ "number", "(x)", function(state, x) return Boolean:new(x.type == "number") end },
|
|
||||||
{ "string", "(x)", function(state, x) return Boolean:new(x.type == "string") end },
|
|
||||||
{ "boolean", "(x)", function(state, x) return Boolean:new(x.type == "boolean") end },
|
|
||||||
{ "symbol", "(x)", function(state, x) return Boolean:new(x.type == "symbol") end },
|
|
||||||
{ "anchor", "(x)", function(state, x) return Boolean:new(x.type == "anchor") end },
|
|
||||||
|
|
||||||
{ "text", "(x)", function(state, x) return Boolean:new(x.type == "text") end },
|
|
||||||
|
|
||||||
{ "pair", "(x)", function(state, x) return Boolean:new(x.type == "pair") end },
|
|
||||||
{ "tuple", "(x)", function(state, x) return Boolean:new(x.type == "tuple") end },
|
|
||||||
{ "list", "(x)", function(state, x) return Boolean:new(x.type == "list") end },
|
|
||||||
{ "struct", "(x)", function(state, x) return Boolean:new(x.type == "struct") end },
|
|
||||||
{ "table", "(x)", function(state, x) return Boolean:new(x.type == "table") end },
|
|
||||||
|
|
||||||
{ "function", "(x)", function(state, x) return Boolean:new(x.type == "function") end },
|
|
||||||
{ "overload", "(x)", function(state, x) return Boolean:new(x.type == "overload") end },
|
|
||||||
{ "callable", "(x)", function(state, x) return Boolean:new(x.type == "overload" or x.type == "function" or x.type == "lua function" or x.type == "quote") end },
|
|
||||||
}
|
|
||||||
|
|
@ -76,9 +76,9 @@ local function run(path, interactive)
|
||||||
local state = anselme:new()
|
local state = anselme:new()
|
||||||
state:load_stdlib()
|
state:load_stdlib()
|
||||||
|
|
||||||
state:define("interrupt", "(code::string)", function(state, code) state:interrupt(code:to_lua(state), "interrupt") return ast.Nil:new() end, true)
|
state:define("interrupt", "(code::is string)", function(state, code) state:interrupt(code:to_lua(state), "interrupt") return ast.Nil:new() end, true)
|
||||||
state:define("interrupt", "()", function(state) state:interrupt() return ast.Nil:new() end, true)
|
state:define("interrupt", "()", function(state) state:interrupt() return ast.Nil:new() end, true)
|
||||||
state:define("wait", "(duration::number)", function(duration) coroutine.yield("wait", duration) end)
|
state:define("wait", "(duration::is number)", function(duration) coroutine.yield("wait", duration) end)
|
||||||
state:define("run in new branch", "(code)", function(code)
|
state:define("run in new branch", "(code)", function(code)
|
||||||
local parallel_state = state:branch()
|
local parallel_state = state:branch()
|
||||||
write_output("--# parallel script #--")
|
write_output("--# parallel script #--")
|
||||||
|
|
@ -87,7 +87,7 @@ local function run(path, interactive)
|
||||||
write_output("--# main script #--")
|
write_output("--# main script #--")
|
||||||
end)
|
end)
|
||||||
state:define("serialize", "(value)", function(state, value) return ast.String:new(value:serialize(state)) end, true)
|
state:define("serialize", "(value)", function(state, value) return ast.String:new(value:serialize(state)) end, true)
|
||||||
state:define("deserialize", "(str::string)", function(state, str) return ast.abstract.Node:deserialize(state, str.string) end, true)
|
state:define("deserialize", "(str::is string)", function(state, str) return ast.abstract.Node:deserialize(state, str.string) end, true)
|
||||||
|
|
||||||
local run_state = state:branch()
|
local run_state = state:branch()
|
||||||
|
|
||||||
|
|
@ -123,7 +123,7 @@ end
|
||||||
if not arg[1] or arg[1] == "update" then
|
if not arg[1] or arg[1] == "update" then
|
||||||
-- display an animated loading indicator
|
-- display an animated loading indicator
|
||||||
local loading = {
|
local loading = {
|
||||||
loop = { "⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷" },
|
loop = { "⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾" },
|
||||||
loop_pos = 1,
|
loop_pos = 1,
|
||||||
erase_code = "",
|
erase_code = "",
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
:$ a - b
|
:$ a - b
|
||||||
return("generic minus")
|
return("generic minus")
|
||||||
|
|
||||||
:$ a::string - b::string
|
:$ a::is string - b::is string
|
||||||
return(a + " minus " + b)
|
return(a + " minus " + b)
|
||||||
|
|
||||||
| {2-5}
|
| {2-5}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
|v={v}
|
|v={v}
|
||||||
a = v
|
a = v
|
||||||
|
|
||||||
:$ f(p) = v::string
|
:$ f(p) = v::is string
|
||||||
|v2={v}
|
|v2={v}
|
||||||
a = p
|
a = p
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
:$ a(x::number)
|
:$ a(x::is number)
|
||||||
return(x + 2)
|
return(x + 2)
|
||||||
|
|
||||||
:$ x
|
:$ x
|
||||||
:$ a(x::string)
|
:$ a(x::is string)
|
||||||
return(x + "heh")
|
return(x + "heh")
|
||||||
|
|
||||||
|{a("plop")}
|
|{a("plop")}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
:$ fn(x::number)
|
:$ fn(x::is number)
|
||||||
|x
|
|x
|
||||||
|
|
||||||
:$ fn(a::number)
|
:$ fn(a::is number)
|
||||||
|a
|
|a
|
||||||
|
|
||||||
fn(5)
|
fn(5)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
:$ fn(x::number)
|
:$ fn(x::is number)
|
||||||
|x
|
|x
|
||||||
|
|
||||||
:$ fn(a::string="o")
|
:$ fn(a::is string="o")
|
||||||
|a
|
|a
|
||||||
|
|
||||||
fn("s")
|
fn("s")
|
||||||
|
|
@ -10,10 +10,10 @@ fn(5)
|
||||||
|
|
||||||
fn()
|
fn()
|
||||||
|
|
||||||
:$ g(n="s", a::number=5)
|
:$ g(n="s", a::is number=5)
|
||||||
return("gn")
|
return("gn")
|
||||||
|
|
||||||
:$ g(n="s", a::string="lol")
|
:$ g(n="s", a::is string="lol")
|
||||||
return("gs")
|
return("gs")
|
||||||
|
|
||||||
|{g(n="k", a="l")}
|
|{g(n="k", a="l")}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
:$ fn(x::number)
|
:$ fn(x::is number)
|
||||||
|x
|
|x
|
||||||
|
|
||||||
:$ fn(a::string)
|
:$ fn(a::is string)
|
||||||
|a
|
|a
|
||||||
|
|
||||||
fn("s")
|
fn("s")
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
:f = ()
|
:f = ()
|
||||||
_
|
_
|
||||||
:$_+_(a::string, b)
|
:$_+_(a::is string, b)
|
||||||
"{a}{b}"
|
"{a}{b}"
|
||||||
|
|
||||||
:a=1
|
:a=1
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
:$ -f
|
:$ -f
|
||||||
return("generic minus")
|
return("generic minus")
|
||||||
|
|
||||||
:$ -f::string
|
:$ -f::is string
|
||||||
return("minus "+f)
|
return("minus "+f)
|
||||||
|
|
||||||
|{-5}
|
|{-5}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue