mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
[stdlib] add call to manually invoke functions
This commit is contained in:
parent
bbf7b7a437
commit
892cb2c623
6 changed files with 38 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
local ast = require("anselme.ast")
|
||||
local unpack = table.unpack or unpack
|
||||
|
||||
local operator_priority = require("anselme.common").operator_priority
|
||||
|
||||
|
|
@ -56,6 +57,10 @@ Tuple = ast.abstract.Node {
|
|||
return l
|
||||
end,
|
||||
|
||||
to_argument_tuple = function(self)
|
||||
return ast.ArgumentTuple:new(unpack(self.list))
|
||||
end,
|
||||
|
||||
get = function(self, index)
|
||||
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
|
||||
|
|
|
|||
|
|
@ -33,6 +33,21 @@ return {
|
|||
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)",
|
||||
function(state, c, s)
|
||||
|
|
|
|||
5
test/results/manual call assignment.ans
Normal file
5
test/results/manual call assignment.ans
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
--# run #--
|
||||
--- return ---
|
||||
15
|
||||
--# saved #--
|
||||
{}
|
||||
5
test/results/manual call.ans
Normal file
5
test/results/manual call.ans
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
--# run #--
|
||||
--- return ---
|
||||
5
|
||||
--# saved #--
|
||||
{}
|
||||
4
test/tests/manual call assignment.ans
Normal file
4
test/tests/manual call assignment.ans
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
:$func(x, y) = v
|
||||
x + y + v
|
||||
|
||||
func!call[2, 3] = 10
|
||||
4
test/tests/manual call.ans
Normal file
4
test/tests/manual call.ans
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
:$func(x, y)
|
||||
x + y
|
||||
|
||||
func!call[2, 3]
|
||||
Loading…
Add table
Add a link
Reference in a new issue