1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00
anselme/anselme/ast/FunctionParameter.lua

49 lines
1.4 KiB
Lua

local ast = require("anselme.ast")
local operator_priority = require("anselme.common").operator_priority
local FunctionParameter
FunctionParameter = ast.abstract.Node {
type = "function parameter",
identifier = nil,
default = nil, -- can be nil
type_check = nil, -- can be nil
init = function(self, identifier, default, type_check)
self.identifier = identifier
self.default = default
self.type_check = type_check
end,
_format = function(self, state, prio, ...)
local s = self.identifier:format(state, prio, ...)
if self.type_check then
s = s .. "::" .. self.type_check:format_right(state, operator_priority["_::_"], ...)
end
if self.default then
s = s .. "=" .. self.default:format_right(state, operator_priority["_=_"], ...)
end
return s
end,
_format_priority = function(self)
if self.default then
return operator_priority["_=_"]
elseif self.type_check then -- type_check has higher prio than assignment in any case
return operator_priority["_::_"]
else
return math.huge
end
end,
traverse = function(self, fn, ...)
fn(self.identifier, ...)
if self.default then fn(self.default, ...) end
if self.type_check then fn(self.type_check, ...) end
end,
_eval = function(self, state)
return FunctionParameter:new(self.identifier, self.default, self.type_check and self.type_check:eval(state))
end
}
return FunctionParameter