From 892cb2c623d61dac4716619bc86ea45acf93a0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Wed, 24 Apr 2024 17:50:44 +0200 Subject: [PATCH] [stdlib] add call to manually invoke functions --- anselme/ast/Tuple.lua | 5 +++++ anselme/stdlib/function.lua | 15 +++++++++++++++ test/results/manual call assignment.ans | 5 +++++ test/results/manual call.ans | 5 +++++ test/tests/manual call assignment.ans | 4 ++++ test/tests/manual call.ans | 4 ++++ 6 files changed, 38 insertions(+) create mode 100644 test/results/manual call assignment.ans create mode 100644 test/results/manual call.ans create mode 100644 test/tests/manual call assignment.ans create mode 100644 test/tests/manual call.ans diff --git a/anselme/ast/Tuple.lua b/anselme/ast/Tuple.lua index 7d8448f..2bca41b 100644 --- a/anselme/ast/Tuple.lua +++ b/anselme/ast/Tuple.lua @@ -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 diff --git a/anselme/stdlib/function.lua b/anselme/stdlib/function.lua index a04b401..47ae905 100644 --- a/anselme/stdlib/function.lua +++ b/anselme/stdlib/function.lua @@ -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) diff --git a/test/results/manual call assignment.ans b/test/results/manual call assignment.ans new file mode 100644 index 0000000..ad94dd8 --- /dev/null +++ b/test/results/manual call assignment.ans @@ -0,0 +1,5 @@ +--# run #-- +--- return --- +15 +--# saved #-- +{} \ No newline at end of file diff --git a/test/results/manual call.ans b/test/results/manual call.ans new file mode 100644 index 0000000..460e101 --- /dev/null +++ b/test/results/manual call.ans @@ -0,0 +1,5 @@ +--# run #-- +--- return --- +5 +--# saved #-- +{} \ No newline at end of file diff --git a/test/tests/manual call assignment.ans b/test/tests/manual call assignment.ans new file mode 100644 index 0000000..9d82ff2 --- /dev/null +++ b/test/tests/manual call assignment.ans @@ -0,0 +1,4 @@ +:$func(x, y) = v + x + y + v + +func!call[2, 3] = 10 diff --git a/test/tests/manual call.ans b/test/tests/manual call.ans new file mode 100644 index 0000000..0e4456c --- /dev/null +++ b/test/tests/manual call.ans @@ -0,0 +1,4 @@ +:$func(x, y) + x + y + +func!call[2, 3]