mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Change format_priority to a method
This commit is contained in:
parent
d42b900388
commit
07cb44256c
23 changed files with 115 additions and 58 deletions
|
|
@ -35,7 +35,6 @@ ArgumentTuple = ast.abstract.Node {
|
|||
assert(not self.assignment)
|
||||
self.arity = self.arity + 1
|
||||
self.assignment = val
|
||||
self.format_priority = operator_priority["_=_"]
|
||||
end,
|
||||
|
||||
_format = function(self, state, priority, ...)
|
||||
|
|
@ -56,6 +55,13 @@ ArgumentTuple = ast.abstract.Node {
|
|||
end
|
||||
return s
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
if self.assignment then
|
||||
return operator_priority["_=_"]
|
||||
else
|
||||
return math.huge
|
||||
end
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
for i=1, self.arity do
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ local Assignment = ast.abstract.Node {
|
|||
|
||||
identifier = nil,
|
||||
expression = nil,
|
||||
format_priority = operator_priority["_=_"],
|
||||
|
||||
init = function(self, identifier, expression)
|
||||
self.identifier = identifier
|
||||
|
|
@ -18,6 +17,9 @@ local Assignment = ast.abstract.Node {
|
|||
_format = function(self, ...)
|
||||
return self.identifier:format(...).." = "..self.expression:format_right(...)
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["_=_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.identifier, ...)
|
||||
|
|
|
|||
|
|
@ -19,26 +19,10 @@ Call = ast.abstract.Node {
|
|||
|
||||
func = nil,
|
||||
arguments = nil, -- ArgumentTuple
|
||||
format_priority = infix["_!"], -- often overwritten in :init
|
||||
|
||||
init = function(self, func, arguments)
|
||||
self.func = func
|
||||
self.arguments = arguments
|
||||
|
||||
-- get priority: operators
|
||||
if Identifier:is(self.func) then
|
||||
local name, arity = self.func.name, self.arguments.arity
|
||||
if infix[name] and arity == 2 then
|
||||
self.format_priority = infix[name]
|
||||
elseif prefix[name] and arity == 1 then
|
||||
self.format_priority = prefix[name]
|
||||
elseif suffix[name] and arity == 1 then
|
||||
self.format_priority = suffix[name]
|
||||
end
|
||||
end
|
||||
if self.arguments.assignment then
|
||||
self.format_priority = operator_priority["_=_"]
|
||||
end
|
||||
end,
|
||||
|
||||
_format = function(self, ...)
|
||||
|
|
@ -66,6 +50,22 @@ Call = ast.abstract.Node {
|
|||
return self.func:format(...)..self.arguments:format(...) -- no need for format_right, we already handle the assignment priority here
|
||||
end
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
if Identifier:is(self.func) then
|
||||
local name, arity = self.func.name, self.arguments.arity
|
||||
if infix[name] and arity == 2 then
|
||||
return infix[name]
|
||||
elseif prefix[name] and arity == 1 then
|
||||
return prefix[name]
|
||||
elseif suffix[name] and arity == 1 then
|
||||
return suffix[name]
|
||||
end
|
||||
end
|
||||
if self.arguments.assignment then
|
||||
return operator_priority["_=_"]
|
||||
end
|
||||
return operator_priority["_!"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.func, ...)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ Choice = ast.abstract.Runtime {
|
|||
|
||||
text = nil,
|
||||
func = nil,
|
||||
format_priority = operator_priority["_|>_"],
|
||||
|
||||
init = function(self, text, func)
|
||||
self.text = text
|
||||
|
|
@ -24,6 +23,9 @@ Choice = ast.abstract.Runtime {
|
|||
_format = function(self, ...)
|
||||
return ("%s |> %s"):format(self.text:format(...), self.func:format_right(...))
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["_|>_"]
|
||||
end,
|
||||
|
||||
build_event_data = function(self, state, event_buffer)
|
||||
local l = {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ local Definition = ast.abstract.Node {
|
|||
|
||||
symbol = nil,
|
||||
expression = nil,
|
||||
format_priority = operator_priority["_=_"],
|
||||
|
||||
init = function(self, symbol, expression)
|
||||
self.symbol = symbol
|
||||
|
|
@ -18,6 +17,9 @@ local Definition = ast.abstract.Node {
|
|||
_format = function(self, ...)
|
||||
return self.symbol:format(...).." = "..self.expression:format_right(...)
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["_=_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.symbol, ...)
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ local VariableMetadata = ast.abstract.Runtime {
|
|||
|
||||
symbol = nil,
|
||||
branched = nil,
|
||||
format_priority = operator_priority["_=_"],
|
||||
|
||||
init = function(self, state, symbol, value)
|
||||
self.symbol = symbol
|
||||
|
|
@ -45,6 +44,10 @@ local VariableMetadata = ast.abstract.Runtime {
|
|||
_format = function(self, ...)
|
||||
return ("%s=%s"):format(self.symbol:format(...), self.branched:format(...))
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["_=_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.symbol, ...)
|
||||
fn(self.branched, ...)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ Function = Overloadable {
|
|||
|
||||
parameters = nil, -- ParameterTuple
|
||||
expression = nil,
|
||||
format_priority = operator_priority["$_"],
|
||||
|
||||
exports = nil, -- { [sym] = exp, ... }, exctracted from expression during :prepare
|
||||
|
||||
|
|
@ -29,6 +28,9 @@ Function = Overloadable {
|
|||
return "$"..self.parameters:format(...).." "..self.expression:format_right(...)
|
||||
end
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["$_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.parameters, ...)
|
||||
|
|
|
|||
|
|
@ -13,11 +13,6 @@ FunctionParameter = ast.abstract.Node {
|
|||
self.identifier = identifier
|
||||
self.default = default
|
||||
self.type_check = type_check
|
||||
if default then
|
||||
self.format_priority = operator_priority["_=_"]
|
||||
elseif type_check then -- type_check has higher prio than assignment in any case
|
||||
self.format_priority = operator_priority["_::_"]
|
||||
end
|
||||
end,
|
||||
|
||||
_format = function(self, state, prio, ...)
|
||||
|
|
@ -30,6 +25,15 @@ FunctionParameter = ast.abstract.Node {
|
|||
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, ...)
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ local List
|
|||
List = ast.abstract.Runtime {
|
||||
type = "list",
|
||||
|
||||
format_priority = operator_priority["*_"],
|
||||
|
||||
-- note: yeah technically this isn't mutable, only .branched is
|
||||
|
||||
-- note: this a Branched of Tuple, and we *will* forcefully mutate the tuples, so make sure to not disseminate any reference to them outside the List
|
||||
|
|
@ -23,6 +21,9 @@ List = ast.abstract.Runtime {
|
|||
_format = function(self, ...)
|
||||
return "*"..self.branched:format_right(...)
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["*_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.branched, ...)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ LuaFunction = ast.abstract.Runtime(Overloadable) {
|
|||
|
||||
parameters = nil, -- ParameterTuple
|
||||
func = nil, -- lua function
|
||||
format_priority = operator_priority["$_"],
|
||||
|
||||
init = function(self, parameters, func)
|
||||
self.parameters = parameters
|
||||
|
|
@ -28,6 +27,9 @@ LuaFunction = ast.abstract.Runtime(Overloadable) {
|
|||
return "$"..self.parameters:format(...).." <lua function>"
|
||||
end
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["$_"]
|
||||
end,
|
||||
|
||||
compatible_with_arguments = function(self, state, args)
|
||||
return args:match_parameter_tuple(state, self.parameters)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ return ast.abstract.Runtime {
|
|||
|
||||
name = nil,
|
||||
value = nil,
|
||||
format_priority = operator_priority["_:_"],
|
||||
|
||||
init = function(self, name, value)
|
||||
self.name = name
|
||||
|
|
@ -22,4 +21,7 @@ return ast.abstract.Runtime {
|
|||
_format = function(self, ...)
|
||||
return ("%s:%s"):format(self.name:format(...), self.value:format(...))
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["_:_"]
|
||||
end,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ ParameterTuple = ast.abstract.Node {
|
|||
insert_assignment = function(self, val) -- only for construction
|
||||
self:insert(val)
|
||||
self.assignment = true
|
||||
self.format_priority = operator_priority["_=_"]
|
||||
end,
|
||||
|
||||
_format = function(self, state, prio, ...)
|
||||
|
|
@ -43,6 +42,12 @@ ParameterTuple = ast.abstract.Node {
|
|||
end
|
||||
return s
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
if self.assignment then
|
||||
return operator_priority["_=_"]
|
||||
end
|
||||
return math.huge
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
for _, e in ipairs(self.list) do
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ PartialScope = ast.abstract.Node {
|
|||
self.expression = expression
|
||||
self.definitions = {}
|
||||
self._identifiers = {}
|
||||
self.format_priority = self.expression.format_priority
|
||||
end,
|
||||
define = function(self, symbol, value) -- for construction only
|
||||
assert(not self.definitions[symbol], ("%s already defined in partial layer"):format(symbol))
|
||||
|
|
@ -36,6 +35,9 @@ PartialScope = ast.abstract.Node {
|
|||
return self.expression:format(state, priority, indentation, ...)
|
||||
end
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return self.expression:format_priority()
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.expression, ...)
|
||||
|
|
|
|||
|
|
@ -15,12 +15,14 @@ Quote = ast.abstract.Node {
|
|||
|
||||
init = function(self, expression)
|
||||
self.expression = expression
|
||||
self.format_priority = expression.format_priority
|
||||
end,
|
||||
|
||||
_format = function(self, ...)
|
||||
return self.expression:format(...) -- Quote is generated transparently by operators
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return self.expression:format_priority()
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.expression, ...)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ Return = ast.abstract.Node {
|
|||
type = "return",
|
||||
|
||||
expression = nil,
|
||||
format_priority = operator_priority["@_"],
|
||||
|
||||
init = function(self, expression)
|
||||
self.expression = expression
|
||||
|
|
@ -16,6 +15,9 @@ Return = ast.abstract.Node {
|
|||
_format = function(self, ...)
|
||||
return ("@%s"):format(self.expression:format_right(...))
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["@_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.expression, ...)
|
||||
|
|
|
|||
|
|
@ -10,12 +10,14 @@ local ReturnBoundary = ast.abstract.Node {
|
|||
|
||||
init = function(self, expression)
|
||||
self.expression = expression
|
||||
self.format_priority = self.expression.format_priority
|
||||
end,
|
||||
|
||||
_format = function(self, ...)
|
||||
return self.expression:format(...)
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return self.expression:format_priority()
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.expression, ...)
|
||||
|
|
|
|||
|
|
@ -19,14 +19,14 @@ local StringInterpolation = ast.abstract.Node {
|
|||
end
|
||||
end,
|
||||
|
||||
_format = function(self, ...)
|
||||
_format = function(self, state, prio, ...)
|
||||
local l = {}
|
||||
for _, e in ipairs(self.list) do
|
||||
if String:is(e) then
|
||||
local t = e.string:gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\t", "\\t"):gsub("\"", "\\\"")
|
||||
table.insert(l, t)
|
||||
else
|
||||
table.insert(l, ("{%s}"):format(e:format(...)))
|
||||
table.insert(l, ("{%s}"):format(e:format(state, 0, ...)))
|
||||
end
|
||||
end
|
||||
return ("\"%s\""):format(table.concat(l))
|
||||
|
|
|
|||
|
|
@ -24,9 +24,6 @@ Symbol = ast.abstract.Node {
|
|||
self.alias = modifiers.alias
|
||||
self.confined_to_branch = modifiers.confined_to_branch
|
||||
self.exported = modifiers.exported
|
||||
if self.type_check then
|
||||
self.format_priority = operator_priority["_::_"]
|
||||
end
|
||||
end,
|
||||
|
||||
_eval = function(self, state)
|
||||
|
|
@ -66,6 +63,12 @@ Symbol = ast.abstract.Node {
|
|||
end
|
||||
return s
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
if self.type_check then
|
||||
return operator_priority["_::_"]
|
||||
end
|
||||
return math.huge
|
||||
end,
|
||||
|
||||
to_lua = function(self, state)
|
||||
return self.string
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ local Table
|
|||
Table = ast.abstract.Runtime {
|
||||
type = "table",
|
||||
|
||||
format_priority = operator_priority["*_"],
|
||||
|
||||
-- note: technically this isn't mutable, only .branched is
|
||||
|
||||
-- note: this a Branched of Struct, and we *will* forcefully mutate the tuples, so make sure to not disseminate any reference to them outside the Table
|
||||
|
|
@ -23,6 +21,9 @@ Table = ast.abstract.Runtime {
|
|||
_format = function(self, ...)
|
||||
return "*"..self.branched:format_right(...)
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
return operator_priority["*_"]
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.branched, ...)
|
||||
|
|
|
|||
|
|
@ -21,14 +21,14 @@ local TextInterpolation = ast.abstract.Node {
|
|||
end
|
||||
end,
|
||||
|
||||
_format = function(self, ...)
|
||||
_format = function(self, state, prio, ...)
|
||||
local l = {}
|
||||
for _, e in ipairs(self.list) do
|
||||
if String:is(e) then
|
||||
local t = e.string:gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\t", "\\t"):gsub("\"", "\\\"")
|
||||
table.insert(l, t)
|
||||
else
|
||||
table.insert(l, ("{%s}"):format(e:format(...)))
|
||||
table.insert(l, ("{%s}"):format(e:format(state, 0, ...)))
|
||||
end
|
||||
end
|
||||
return ("| %s |"):format(table.concat(l))
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ local translation_manager
|
|||
|
||||
local Translatable = ast.abstract.Node {
|
||||
type = "translatable",
|
||||
format_priority = operator_priority["%_"],
|
||||
|
||||
expression = nil,
|
||||
|
||||
|
|
@ -15,9 +14,6 @@ local Translatable = ast.abstract.Node {
|
|||
self.expression = expression
|
||||
self.context = ast.Struct:new()
|
||||
self.context:set(String:new("source"), String:new(self.expression.source))
|
||||
if TextInterpolation:is(self.expression) then
|
||||
self.format_priority = expression.format_priority
|
||||
end
|
||||
end,
|
||||
|
||||
_format = function(self, ...)
|
||||
|
|
@ -27,6 +23,13 @@ local Translatable = ast.abstract.Node {
|
|||
return "%"..self.expression:format_right(...)
|
||||
end
|
||||
end,
|
||||
_format_priority = function(self)
|
||||
if TextInterpolation:is(self.expression) then
|
||||
return self.expression:format_priority()
|
||||
else
|
||||
return operator_priority["%_"]
|
||||
end
|
||||
end,
|
||||
|
||||
traverse = function(self, fn, ...)
|
||||
fn(self.expression, ...)
|
||||
|
|
|
|||
|
|
@ -257,9 +257,9 @@ Node = class {
|
|||
indentation_level = indentation_level or 0
|
||||
parent_priority = parent_priority or 0
|
||||
|
||||
local s = self:_format(state, self.format_priority, indentation_level)
|
||||
local s = self:_format(state, self:format_priority(), indentation_level)
|
||||
|
||||
if self.format_priority < parent_priority then
|
||||
if self:format_priority() < parent_priority then
|
||||
s = ("(%s)"):format(s)
|
||||
end
|
||||
|
||||
|
|
@ -273,9 +273,9 @@ Node = class {
|
|||
indentation_level = indentation_level or 0
|
||||
parent_priority = parent_priority or 0
|
||||
|
||||
local s = self:_format(state, self.format_priority, indentation_level)
|
||||
local s = self:_format(state, self:format_priority(), indentation_level)
|
||||
|
||||
if self.format_priority <= parent_priority then
|
||||
if self:format_priority() <= parent_priority then
|
||||
s = ("(%s)"):format(s)
|
||||
end
|
||||
|
||||
|
|
@ -288,9 +288,20 @@ Node = class {
|
|||
_format = function(self, state, self_priority, identation)
|
||||
error("format not implemented for "..self.type)
|
||||
end,
|
||||
-- priority of the node that will be used in :format to add eventually needed parentheses.
|
||||
-- should not be modified after object construction!
|
||||
format_priority = math.huge, -- by default, assumes primary node, i.e. never wrap in parentheses
|
||||
-- compute the priority of the node that will be used in :format to add eventually needed parentheses.
|
||||
-- should alwaus return the same value after object construction (will be cached anyway)
|
||||
-- redefine _format_priority, not this function
|
||||
format_priority = function(self)
|
||||
if not self._format_priority_cache then
|
||||
self._format_priority_cache = self:_format_priority()
|
||||
end
|
||||
return self._format_priority_cache
|
||||
end,
|
||||
-- redefine this to compute the priority, see :format_priority
|
||||
_format_priority = function(self)
|
||||
return math.huge -- by default, assumes primary node, i.e. never wrap in parentheses
|
||||
end,
|
||||
_format_priority_cache = nil, -- cached priority
|
||||
|
||||
-- return Lua value
|
||||
-- this should probably be only called on a Node that is already evaluated
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
• (c::($(x) <lua function>), s::($(x) <lua function>)) = v: expected 3 arguments, received 2
|
||||
• (c::($(x) <lua function>), s::($(x) <lua function>)): type check failure for parameter c in function (c::($(x) <lua function>), s::($(x) <lua function>))[0m
|
||||
↳ from [4mtest/tests/function separate variable from variants.ans:10:4[0m in call: [2mf . "a"[0m[0m
|
||||
↳ from [4mtest/tests/function separate variable from variants.ans:10:1[0m in text interpolation: [2m| {(f . "a")} = 2 |[0m[0m
|
||||
↳ from [4mtest/tests/function separate variable from variants.ans:10:1[0m in translatable: [2m| {(f . "a")} = 2 |[0m[0m
|
||||
↳ from [4mtest/tests/function separate variable from variants.ans:10:1[0m in text interpolation: [2m| {f . "a"} = 2 |[0m[0m
|
||||
↳ from [4mtest/tests/function separate variable from variants.ans:10:1[0m in translatable: [2m| {f . "a"} = 2 |[0m[0m
|
||||
↳ from [4m?[0m in block: [2m:f = ($() _)…[0m
|
||||
--# saved #--
|
||||
{}
|
||||
Loading…
Add table
Add a link
Reference in a new issue