1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 08:39:30 +00:00

[stdlib] add call to manually invoke functions

This commit is contained in:
Étienne Fildadut 2024-04-24 17:50:44 +02:00
parent bbf7b7a437
commit 892cb2c623
6 changed files with 38 additions and 0 deletions

View file

@ -1,4 +1,5 @@
local ast = require("anselme.ast") local ast = require("anselme.ast")
local unpack = table.unpack or unpack
local operator_priority = require("anselme.common").operator_priority local operator_priority = require("anselme.common").operator_priority
@ -56,6 +57,10 @@ Tuple = ast.abstract.Node {
return l return l
end, end,
to_argument_tuple = function(self)
return ast.ArgumentTuple:new(unpack(self.list))
end,
get = function(self, index) get = function(self, index)
if index < 0 then index = #self.list + 1 + index end if index < 0 then index = #self.list + 1 + index end
if index > #self.list or index == 0 then error("tuple index out of bounds", 0) end if index > #self.list or index == 0 then error("tuple index out of bounds", 0) end

View file

@ -33,6 +33,21 @@ return {
end end
}, },
{
"call", "(func::is callable, args::is tuple)",
function(state, fn, args)
return fn:call(state, args:to_argument_tuple())
end
},
{
"call", "(func::is callable, args::is tuple) = v",
function(state, fn, args, v)
local argumenttuple = args:to_argument_tuple()
argumenttuple:add_assignment(v)
return fn:call(state, argumenttuple)
end
},
{ {
"_._", "(c::is function, s::is string)", "_._", "(c::is function, s::is string)",
function(state, c, s) function(state, c, s)

View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
15
--# saved #--
{}

View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
5
--# saved #--
{}

View file

@ -0,0 +1,4 @@
:$func(x, y) = v
x + y + v
func!call[2, 3] = 10

View file

@ -0,0 +1,4 @@
:$func(x, y)
x + y
func!call[2, 3]