1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 08:39:30 +00:00
anselme/anselme/ast/Text.lua
Étienne Reuh Fildadut b534a3c4a2 [language] automatically call text when they appear directly as a statement; remove autocalling of every text returned by a statement
The previous behavior, where any Text value returned by a line in a Block would be automatically called, may lead to unexpected Text event being written as it is not obvious which line can return a Text value after evaluation.

The new behavior only triggers if a Text node directly appear in the original script as a statement.
2024-04-23 19:57:36 +02:00

47 lines
1 KiB
Lua

local ast = require("anselme.ast")
local Event, Runtime = ast.abstract.Event, ast.abstract.Runtime
local ArgumentTuple
local Text = Runtime(Event) {
type = "text",
list = nil, -- { { String, tag Table }, ... }
init = function(self)
self.list = {}
end,
insert = function(self, str, tags) -- only for construction
table.insert(self.list, { str, tags })
end,
traverse = function(self, fn, ...)
for _, e in ipairs(self.list) do
fn(e[1], ...)
fn(e[2], ...)
end
end,
_format = function(self, ...)
local t = {}
for _, e in ipairs(self.list) do
table.insert(t, ("%s%s"):format(e[2]:format(...), e[1]:format(...)))
end
return ("| %s |"):format(table.concat(t, " "))
end,
-- autocall when used directly as a statement
eval_statement = function(self, state)
return self:call(state, ArgumentTuple:new())
end,
-- Text comes from TextInterpolation which already evals the contents
to_event_data = function(self)
return self
end
}
package.loaded[...] = Text
ArgumentTuple = ast.ArgumentTuple
return Text