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

Switch back to tag:pop instead of tag:trim

The tag operator should allow for all the user-defined tagging you need, and it already ensures the stack stays consistent so there's no need to be extra careful with tag:trim.
This commit is contained in:
Étienne Fildadut 2021-11-28 16:31:42 +01:00
parent 38b2a6ae69
commit 90e5a2d9cf
3 changed files with 8 additions and 11 deletions

View file

@ -191,12 +191,10 @@ common = {
for k, v in pairs(common.to_lua(val)) do new[k] = v end
-- add
table.insert(state.interpreter.tags, new)
return self:len(state)
end,
--- same but do not merge with last stack item
push_lua_no_merge = function(self, state, val)
table.insert(state.interpreter.tags, val)
return self:len(state)
end,
-- pop tag table on top of the stack
pop = function(self, state)
@ -211,8 +209,7 @@ common = {
return #state.interpreter.tags
end,
--- pop item until we reached desired stack length
-- try to prefer this to pop if possible, so in case we mess up the stack somehow it will restore the stack to a good state
-- (we may allow tag push/pop from the user side at some point TODO)
-- so in case there's a possibility to mess up the stack somehow, it will restore the stack to a good state
trim = function(self, state, len)
while #state.interpreter.tags > len do
self:pop(state)
@ -342,9 +339,9 @@ common = {
-- execute in expected tag & event capture state
local capture_state = state.interpreter.event_capture_stack
state.interpreter.event_capture_stack = {}
local i = common.tags:push_lua_no_merge(state, choice.tags)
common.tags:push_lua_no_merge(state, choice.tags)
local _, e = run_block(state, choice.block)
common.tags:trim(state, i-1)
common.tags:pop(state)
state.interpreter.event_capture_stack = capture_state
if e then return nil, e end
-- we discard return value from choice block as the execution is delayed until an event flush

View file

@ -138,9 +138,9 @@ local function eval(state, exp)
elseif exp.type == "#" then
local right, righte = eval(state, exp.right)
if not right then return right, righte end
local i = tags:push(state, right)
tags:push(state, right)
local left, lefte = eval(state, exp.left)
tags:trim(state, i-1)
tags:pop(state)
if not left then return left, lefte end
return left
-- variable

View file

@ -47,9 +47,9 @@ run_line = function(state, line)
elseif line.type == "tag" then
local v, e = eval(state, line.expression)
if not v then return v, ("%s; at %s"):format(e, line.source) end
local i = tags:push(state, v)
tags:push(state, v)
v, e = run_block(state, line.child)
tags:trim(state, i-1)
tags:pop(state)
if e then return v, e end
if v then return v end
elseif line.type == "return" then
@ -180,7 +180,7 @@ local function run(state, block, resume_from_there, i, j)
local v, e = run_block(state, block, resume_from_there, i, j)
-- return to previous tag state
-- when resuming is done, tag stack pop when exiting the tag block
-- stray elements may be left on the stack if there is a return before we exit all the tag block, so we trim them
-- stray elements may be left on the stack if there is a return before we go up all the tag blocks, so we trim them
if resume_from_there then
tags:trim(state, tags_len)
end