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:
parent
38b2a6ae69
commit
90e5a2d9cf
3 changed files with 8 additions and 11 deletions
|
|
@ -191,12 +191,10 @@ common = {
|
||||||
for k, v in pairs(common.to_lua(val)) do new[k] = v end
|
for k, v in pairs(common.to_lua(val)) do new[k] = v end
|
||||||
-- add
|
-- add
|
||||||
table.insert(state.interpreter.tags, new)
|
table.insert(state.interpreter.tags, new)
|
||||||
return self:len(state)
|
|
||||||
end,
|
end,
|
||||||
--- same but do not merge with last stack item
|
--- same but do not merge with last stack item
|
||||||
push_lua_no_merge = function(self, state, val)
|
push_lua_no_merge = function(self, state, val)
|
||||||
table.insert(state.interpreter.tags, val)
|
table.insert(state.interpreter.tags, val)
|
||||||
return self:len(state)
|
|
||||||
end,
|
end,
|
||||||
-- pop tag table on top of the stack
|
-- pop tag table on top of the stack
|
||||||
pop = function(self, state)
|
pop = function(self, state)
|
||||||
|
|
@ -211,8 +209,7 @@ common = {
|
||||||
return #state.interpreter.tags
|
return #state.interpreter.tags
|
||||||
end,
|
end,
|
||||||
--- pop item until we reached desired stack length
|
--- 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
|
-- so in case there's a possibility to 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)
|
|
||||||
trim = function(self, state, len)
|
trim = function(self, state, len)
|
||||||
while #state.interpreter.tags > len do
|
while #state.interpreter.tags > len do
|
||||||
self:pop(state)
|
self:pop(state)
|
||||||
|
|
@ -342,9 +339,9 @@ common = {
|
||||||
-- execute in expected tag & event capture state
|
-- execute in expected tag & event capture state
|
||||||
local capture_state = state.interpreter.event_capture_stack
|
local capture_state = state.interpreter.event_capture_stack
|
||||||
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)
|
local _, e = run_block(state, choice.block)
|
||||||
common.tags:trim(state, i-1)
|
common.tags:pop(state)
|
||||||
state.interpreter.event_capture_stack = capture_state
|
state.interpreter.event_capture_stack = capture_state
|
||||||
if e then return nil, e end
|
if e then return nil, e end
|
||||||
-- we discard return value from choice block as the execution is delayed until an event flush
|
-- we discard return value from choice block as the execution is delayed until an event flush
|
||||||
|
|
|
||||||
|
|
@ -138,9 +138,9 @@ local function eval(state, exp)
|
||||||
elseif exp.type == "#" then
|
elseif exp.type == "#" then
|
||||||
local right, righte = eval(state, exp.right)
|
local right, righte = eval(state, exp.right)
|
||||||
if not right then return right, righte end
|
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)
|
local left, lefte = eval(state, exp.left)
|
||||||
tags:trim(state, i-1)
|
tags:pop(state)
|
||||||
if not left then return left, lefte end
|
if not left then return left, lefte end
|
||||||
return left
|
return left
|
||||||
-- variable
|
-- variable
|
||||||
|
|
|
||||||
|
|
@ -47,9 +47,9 @@ run_line = function(state, line)
|
||||||
elseif line.type == "tag" then
|
elseif line.type == "tag" then
|
||||||
local v, e = eval(state, line.expression)
|
local v, e = eval(state, line.expression)
|
||||||
if not v then return v, ("%s; at %s"):format(e, line.source) end
|
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)
|
v, e = run_block(state, line.child)
|
||||||
tags:trim(state, i-1)
|
tags:pop(state)
|
||||||
if e then return v, e end
|
if e then return v, e end
|
||||||
if v then return v end
|
if v then return v end
|
||||||
elseif line.type == "return" then
|
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)
|
local v, e = run_block(state, block, resume_from_there, i, j)
|
||||||
-- return to previous tag state
|
-- return to previous tag state
|
||||||
-- when resuming is done, tag stack pop when exiting the tag block
|
-- 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
|
if resume_from_there then
|
||||||
tags:trim(state, tags_len)
|
tags:trim(state, tags_len)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue