diff --git a/anselme.lua b/anselme.lua index e3e046a..3d4740e 100644 --- a/anselme.lua +++ b/anselme.lua @@ -568,10 +568,16 @@ return setmetatable(anselme, { link_next_function_definition_to_lua_function = nil -- temporarly set to tell the preparser to link a anselme function definition with a lua function } local vm = setmetatable({ state = state }, vm_mt) + -- bootscript local boot = assert(vm:loadstring(bootscript, "", "boot script")) local _, e = vm:eval(boot) if e then error(e) end - assert(vm:loadfunction(stdfuncs)) + -- lua-defined functions + assert(vm:loadfunction(stdfuncs.lua)) + -- anselme-defined functions + local ansfunc = assert(vm:loadstring(stdfuncs.anselme, "", "built-in functions")) + _, e = vm:eval(ansfunc) + if e then return error(e) end return vm end }) diff --git a/notes.txt b/notes.txt index 03184fb..751d88b 100644 --- a/notes.txt +++ b/notes.txt @@ -53,7 +53,7 @@ TODO: the function decorator feels a bit glued-on to the current syntax TODO: simplify language, it is much too complicated. Less line types? (var def, func, checkpoint, tag). Rewrite some ad hoc syntax using the expression system? -TODO: functions/checkpoint: separate from scoping and/or actual functions? +TODO: functions/checkpoint: separate from scoping and/or actual functions? with proper scoping & stuff TODO: fn/checkpoint/tag: maybe consider them a regular func call that takes children as arg; can keep compatibility using $/ยง as shortcut for the actual call. would allow more flexibility esp. for tags... diff --git a/stdlib/functions.lua b/stdlib/functions.lua index a3016c3..083e82b 100644 --- a/stdlib/functions.lua +++ b/stdlib/functions.lua @@ -6,8 +6,8 @@ local function mark_as_modified(v) table.insert(modified, v) end -local functions -functions = { +local lua_functions +lua_functions = { -- discard left ["_;_(a, b)"] = { mode = "raw", @@ -314,20 +314,25 @@ functions = { end return anselme.running:run(f, anselme.running:current_namespace()) end, - ["random(l...)"] = function(l) - return anselme.running:run(l[math.random(1, #l)], anselme.running:current_namespace()) - end, - ["next(l...)"] = function(l) - local f = l[#l] - for j=1, #l-1 do - local seen = assert(anselme.running:eval(l[j]..".๐Ÿ‘๏ธ", anselme.running:current_namespace())) - if seen == 0 then - f = l[j] - break - end - end - return anselme.running:run(f, anselme.running:current_namespace()) - end +} + +local anselme_functions = [[ +$ random(l...) + ~ l(rand(1, l!len))! + +$ next(l...) + ~ l!len == 1 | l(1).๐Ÿ‘๏ธ == 0 + ~ l(1)! + ~~ + ~ l!remove(1) + ~ next(l=l) + +(TODO: cycle) +]] + +local functions = { + lua = lua_functions, + anselme = anselme_functions } package.loaded[...] = functions @@ -336,3 +341,5 @@ truthy, compare, is_of_type, get_variable = icommon.truthy, icommon.compare, ico local pcommon = require((...):gsub("stdlib%.functions$", "parser.common")) identifier_pattern, format_identifier, find = pcommon.identifier_pattern, pcommon.format_identifier, pcommon.find anselme = require((...):gsub("stdlib%.functions$", "anselme")) + +return functions diff --git a/test/tests/function next.ans b/test/tests/function next.ans index 1ff5999..df29021 100644 --- a/test/tests/function next.ans +++ b/test/tests/function next.ans @@ -5,10 +5,14 @@ $ f b $ c c - ~ next("a","b","c") + ~ next(&a, &b, &c) ~ f + ~ f + ~ f + ~ f + ~ f \ No newline at end of file diff --git a/test/tests/function random.ans b/test/tests/function random.ans index 6d8563b..d2ef400 100644 --- a/test/tests/function random.ans +++ b/test/tests/function random.ans @@ -5,10 +5,14 @@ $ f b $ c c - ~ random("a","b","c") + ~ random(&a,&b,&c) ~ f + ~ f + ~ f + +~ f + ~ f -~ f \ No newline at end of file