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

Custom format for Typed and dispatch refactor

This commit is contained in:
Étienne Fildadut 2023-12-28 14:28:14 +01:00
parent e222f0fb28
commit 91e1311560
8 changed files with 75 additions and 26 deletions

View file

@ -2,7 +2,10 @@ local ast = require("ast")
local operator_priority = require("common").operator_priority
return ast.abstract.Runtime {
local format_identifier
local Typed
Typed = ast.abstract.Runtime {
type = "typed",
expression = nil,
@ -14,6 +17,15 @@ return ast.abstract.Runtime {
end,
_format = function(self, state, prio, ...)
-- try custom format
if state and state.scope:defined(format_identifier) then
local custom_format = format_identifier:eval(state)
local args = ast.ArgumentTuple:new(self)
local fn, d_args = custom_format:dispatch(state, args)
if fn then
return custom_format:call(state, d_args):format(state, prio, ...)
end
end
return ("type(%s, %s)"):format(self.type_expression:format(state, operator_priority["_,_"], ...), self.expression:format_right(state, operator_priority["_,_"], ...))
end,
@ -22,3 +34,8 @@ return ast.abstract.Runtime {
fn(self.expression, ...)
end
}
package.loaded[...] = Typed
format_identifier = ast.Identifier:new("format")
return Typed