mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-28 09:09:31 +00:00
55 lines
1.3 KiB
Lua
55 lines
1.3 KiB
Lua
local class = require("anselme.lib.class")
|
|
local ast = require("anselme.ast")
|
|
local ArgumentTuple
|
|
local Event = ast.abstract.Event
|
|
|
|
local operator_priority = require("anselme.common").operator_priority
|
|
|
|
local ChoiceEventData = class {
|
|
_selected = nil,
|
|
choose = function(self, choice)
|
|
self._selected = choice
|
|
end
|
|
}
|
|
|
|
local Choice
|
|
Choice = ast.abstract.Runtime(Event) {
|
|
type = "choice",
|
|
|
|
text = nil,
|
|
func = nil,
|
|
|
|
init = function(self, text, func)
|
|
self.text = text
|
|
self.func = func
|
|
end,
|
|
|
|
traverse = function(self, fn, ...)
|
|
fn(self.text, ...)
|
|
fn(self.func, ...)
|
|
end,
|
|
|
|
_format = function(self, state, prio, ...)
|
|
return ("write choice(%s, %s)"):format(self.text:format(state, operator_priority["_,_"], ...), self.func:format_right(state, operator_priority["_,_"], ...))
|
|
end,
|
|
|
|
build_event_data = function(self, state, event_buffer)
|
|
local l = ChoiceEventData:new()
|
|
for _, c in event_buffer:iter(state) do
|
|
table.insert(l, c.text)
|
|
end
|
|
return l
|
|
end,
|
|
post_flush_callback = function(self, state, event_buffer, data)
|
|
local choice = data._selected
|
|
assert(choice, "no choice made")
|
|
assert(choice > 0 and choice <= event_buffer:len(state), "choice out of bounds")
|
|
|
|
event_buffer:get(state, choice).func:call(state, ArgumentTuple:new())
|
|
end
|
|
}
|
|
|
|
package.loaded[...] = Choice
|
|
ArgumentTuple = ast.ArgumentTuple
|
|
|
|
return Choice
|