mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-28 00:59:31 +00:00
Allow adding overload to overload
This commit is contained in:
parent
f198286870
commit
67c952bc21
3 changed files with 14 additions and 8 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
if Overload:is(val) then
|
||||||
|
for _, overloadable in ipairs(val.list) do
|
||||||
|
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()))
|
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)
|
table.insert(self.list, val)
|
||||||
self._signatures[val:hash_signature()] = true
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue