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 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) local vm = setmetatable({ state = state }, vm_mt)
-- bootscript
local boot = assert(vm:loadstring(bootscript, "", "boot script")) local boot = assert(vm:loadstring(bootscript, "", "boot script"))
local _, e = vm:eval(boot) local _, e = vm:eval(boot)
if e then error(e) end 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 return vm
end 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: 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. 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... would allow more flexibility esp. for tags...

View file

@ -6,8 +6,8 @@ local function mark_as_modified(v)
table.insert(modified, v) table.insert(modified, v)
end end
local functions local lua_functions
functions = { lua_functions = {
-- discard left -- discard left
["_;_(a, b)"] = { ["_;_(a, b)"] = {
mode = "raw", mode = "raw",
@ -314,20 +314,25 @@ functions = {
end end
return anselme.running:run(f, anselme.running:current_namespace()) return anselme.running:run(f, anselme.running:current_namespace())
end, end,
["random(l...)"] = function(l) }
return anselme.running:run(l[math.random(1, #l)], anselme.running:current_namespace())
end, local anselme_functions = [[
["next(l...)"] = function(l) $ random(l...)
local f = l[#l] ~ l(rand(1, l!len))!
for j=1, #l-1 do
local seen = assert(anselme.running:eval(l[j]..".👁️", anselme.running:current_namespace())) $ next(l...)
if seen == 0 then ~ l!len == 1 | l(1).👁 == 0
f = l[j] ~ l(1)!
break ~~
end ~ l!remove(1)
end ~ next(l=l)
return anselme.running:run(f, anselme.running:current_namespace())
end (TODO: cycle)
]]
local functions = {
lua = lua_functions,
anselme = anselme_functions
} }
package.loaded[...] = 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")) local pcommon = require((...):gsub("stdlib%.functions$", "parser.common"))
identifier_pattern, format_identifier, find = pcommon.identifier_pattern, pcommon.format_identifier, pcommon.find identifier_pattern, format_identifier, find = pcommon.identifier_pattern, pcommon.format_identifier, pcommon.find
anselme = require((...):gsub("stdlib%.functions$", "anselme")) anselme = require((...):gsub("stdlib%.functions$", "anselme"))
return functions

View file

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

View file

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