mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Fix + operator overloading
This commit is contained in:
parent
ec18d2e611
commit
9028970440
3 changed files with 14 additions and 12 deletions
|
|
@ -194,7 +194,7 @@ $ f(a, b...)
|
|||
~ f("discarded")
|
||||
```
|
||||
|
||||
Functions with the same name can be defined, as long as they have a different number of argument.
|
||||
Functions with the same name can be defined, as long as they have a different number of argument. Functions will be selected based on the number of arguments given:
|
||||
|
||||
```
|
||||
$ f(a, b)
|
||||
|
|
|
|||
10
anselme.lua
10
anselme.lua
|
|
@ -7,11 +7,11 @@ local anselme = {
|
|||
}
|
||||
package.loaded[...] = anselme
|
||||
|
||||
-- TODO: for type checking functions:
|
||||
-- pour éliminer totalement les undefined
|
||||
-- ne type checker/compiler les fonctions que lorsqu'elles sont apppelées, en déterminant leur type à ce moment là selon les arguments donnés
|
||||
-- (du coup la surcharge ne se fera que selon l'arité. Mais ça sera 100% type checké)
|
||||
-- PB: les listes ont des types mixés (cf les varargs) + afficher des warnings dans le selecteur de variante lorsqu'un type de retour manque
|
||||
-- TODO: improve type checking.
|
||||
-- Right now, there is some basic type checking done at parsing - but since Lua and Anselme functions may not always define the type of
|
||||
-- their parameters and return value, a lot of checks are skipped ("undefined argument" type).
|
||||
-- Probably won't be able to remove them completely (since lists can have mixed types, etc.), but would be good to limit them.
|
||||
-- Ideally, we'd avoid runtime type checking.
|
||||
|
||||
-- load libs
|
||||
local preparse = require((...):gsub("anselme$", "parser.preparser"))
|
||||
|
|
|
|||
|
|
@ -133,12 +133,14 @@ functions = {
|
|||
-- arithmetic
|
||||
["+"] = {
|
||||
{
|
||||
arity = 2, types = { "number", "number" }, return_type = "number",
|
||||
value = function(a, b) return a + b end
|
||||
},
|
||||
{
|
||||
arity = 2, types = { "string", "string" }, return_type = "string",
|
||||
value = function(a, b) return a .. b end
|
||||
arity = 2,
|
||||
value = function(a, b)
|
||||
if type(a) == "string" then
|
||||
return a .. b
|
||||
else
|
||||
return a + b
|
||||
end
|
||||
end
|
||||
}
|
||||
},
|
||||
["-"] = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue