1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Rewrite next, random using references

This commit is contained in:
Étienne Fildadut 2021-12-09 17:23:54 +01:00
parent acb8945dec
commit 40c1616cce
5 changed files with 42 additions and 21 deletions

View file

@ -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
})

View file

@ -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...

View file

@ -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

View file

@ -5,10 +5,14 @@ $ f
b
$ c
c
~ next("a","b","c")
~ next(&a, &b, &c)
~ f
~ f
~ f
~ f
~ f

View file

@ -5,10 +5,14 @@ $ f
b
$ c
c
~ random("a","b","c")
~ random(&a,&b,&c)
~ f
~ f
~ f
~ f
~ f