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

Add eventbuffer type

This commit is contained in:
Étienne Fildadut 2021-12-02 18:39:43 +01:00
parent 02d50fb79f
commit 721464218c
10 changed files with 211 additions and 121 deletions

View file

@ -33,17 +33,38 @@ run_line = function(state, line)
elseif line.type == "choice" then
local v, e = events:make_space_for(state, "choice")
if not v then return v, ("%s; in automatic event flush at %s"):format(e, line.source) end
local currentTags = tags:current(state)
local choice_block_state = { tags = currentTags, block = line.child }
v, e = events:append(state, "choice", { _state = choice_block_state }) -- new choice
if not v then return v, e end
events:push_capture(state, "text", function(event)
local v2, e2 = events:append_in_last(state, "choice", event, { _state = choice_block_state })
if not v2 then return v2, e2 end
end)
v, e = eval(state, line.text)
events:pop_capture(state, "text")
if not v then return v, ("%s; at %s"):format(e, line.source) end
-- convert text events to choices
if v.type == "eventbuffer" then
local current_tags = tags:current(state)
local choice_block_state = { tags = current_tags, block = line.child }
local final_buffer = {}
for _, event in ipairs(v.value) do
if event.type == "text" then
-- create new choice block if needed
local last_choice_block = final_buffer[#final_buffer]
if not last_choice_block or last_choice_block.type ~= "choice" then
last_choice_block = { type = "choice" }
table.insert(final_buffer, last_choice_block)
end
-- create new choice item in choice block if needed
local last_choice = last_choice_block[#last_choice_block]
if not last_choice then
last_choice = { _state = choice_block_state }
table.insert(last_choice_block, last_choice)
end
-- add text to last choice item
for _, txt in ipairs(event) do
table.insert(last_choice, txt)
end
else
table.insert(final_buffer, event)
end
end
v, e = events:write_buffer(state, final_buffer)
if not v then return v, ("%s; at %s"):format(e, line.source) end
end
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
@ -61,6 +82,10 @@ run_line = function(state, line)
if not v then return v, ("%s; in automatic event flush at %s"):format(e, line.source) end
v, e = eval(state, line.text)
if not v then return v, ("%s; at %s"):format(e, line.source) end
if v.type == "eventbuffer" then
v, e = events:write_buffer(state, v.value)
if not v then return v, ("%s; at %s"):format(e, line.source) end
end
elseif line.type == "flush_events" then
local v, e = events:flush(state)
if not v then return v, ("%s; in event flush at %s"):format(e, line.source) end