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

Allow adding overload to overload

This commit is contained in:
Étienne Fildadut 2024-01-07 18:22:55 +01:00
parent f198286870
commit 67c952bc21
3 changed files with 14 additions and 8 deletions

View file

@ -1,5 +1,5 @@
local ast = require("anselme.ast") local ast = require("anselme.ast")
local Nil, Overloadable local Nil, Overloadable, Overload
local operator_priority = require("anselme.common").operator_priority local operator_priority = require("anselme.common").operator_priority
@ -30,7 +30,7 @@ local Definition = ast.abstract.Node {
local symbol = self.symbol:eval(state) local symbol = self.symbol:eval(state)
local val = self.expression:eval(state) local val = self.expression:eval(state)
if Overloadable:issub(val) then if Overloadable:issub(val) or Overload:is(val) then
state.scope:define_overloadable(symbol, val) state.scope:define_overloadable(symbol, val)
else else
state.scope:define(symbol, val) state.scope:define(symbol, val)
@ -41,6 +41,6 @@ local Definition = ast.abstract.Node {
} }
package.loaded[...] = Definition package.loaded[...] = Definition
Nil, Overloadable = ast.Nil, ast.abstract.Overloadable Nil, Overloadable, Overload = ast.Nil, ast.abstract.Overloadable, ast.Overload
return Definition return Definition

View file

@ -123,7 +123,7 @@ local Environment = ast.abstract.Runtime {
end, end,
-- define or redefine new overloadable variable in current environment, inheriting existing overload variants from (parent) scopes -- define or redefine new overloadable variable in current environment, inheriting existing overload variants from (parent) scopes
define_overloadable = function(self, state, symbol, exp) define_overloadable = function(self, state, symbol, exp)
assert(Overloadable:issub(exp), "trying to add an non-overloadable value to an overload") assert(Overloadable:issub(exp) or Overload:is(exp), "trying to add an non-overloadable value to an overload")
local identifier = symbol:to_identifier() local identifier = symbol:to_identifier()

View file

@ -17,9 +17,15 @@ Overload = ast.abstract.Node {
end end
end, end,
insert = function(self, val) -- only for construction insert = function(self, val) -- only for construction
assert0(not self._signatures[val:hash_signature()], ("a function with parameters %s is already defined in the overload"):format(val:format_signature())) if Overload:is(val) then
table.insert(self.list, val) for _, overloadable in ipairs(val.list) do
self._signatures[val:hash_signature()] = true self:insert(overloadable)
end
else
assert0(not self._signatures[val:hash_signature()], ("a function with parameters %s is already defined in the overload"):format(val:format_signature()))
table.insert(self.list, val)
self._signatures[val:hash_signature()] = true
end
end, end,
_format = function(self, ...) _format = function(self, ...)
@ -61,7 +67,7 @@ Overload = ast.abstract.Node {
if success then if success then
return success, args return success, args
else else
return nil, ("no function match arguments %s, possible functions were:\n\t• %s"):format(args:format(state), table.concat(failure, "\n\t")) return nil, ("no function match arguments %s, possible functions were:\n\t• %s"):format(args:format_short(state), table.concat(failure, "\n\t"))
end end
end end
} }