diff --git a/logics-js/logics.js b/logics-js/logics.js index 58ce5ab..9548567 100644 --- a/logics-js/logics.js +++ b/logics-js/logics.js @@ -198,6 +198,7 @@ export default class Logics { } }, call: () => { + let fname = node.children[0].match; let args = []; if (node.children.length === 2) { @@ -205,7 +206,7 @@ export default class Logics { args = stack.pop().toList(); } - let fn = this.functions[node.children[0].match]; + let fn = this.functions[fname]; if (fn !== undefined) { // Convert all args to Logics values @@ -213,11 +214,15 @@ export default class Logics { args[i] = new Value(args[i]); } - // todo: Handle invalid parameters - stack.op0(fn(...args)); + try { + stack.op0(fn(...args)); + } + catch (e) { + stack.op0(`#ERR:Invalid call to ${fname}()`) + } + } else { - // todo: should this return a string? - throw new Error(`Call to unknown function: ${node.children[0].match}`); + stack.op0(`#ERR:Call to unknown function ${fname}()`) } }, comprehension: () => { diff --git a/logics-js/test/logics.js b/logics-js/test/logics.js index a952075..004554d 100644 --- a/logics-js/test/logics.js +++ b/logics-js/test/logics.js @@ -23,7 +23,7 @@ function testcase(code) { switch (cmd[0].toLowerCase()) { case "expect": // Verify the expected result. - let expect = cmd[1]; + let expect = cmd.slice(1).join(":"); // Ensure that the previous result is not undefined. assert.notStrictEqual(lastResult, undefined); diff --git a/logics-py/logics/logics.py b/logics-py/logics/logics.py index 75c33f9..4dc7176 100644 --- a/logics-py/logics/logics.py +++ b/logics-py/logics/logics.py @@ -181,18 +181,22 @@ def _run(self, node: LogicsNode, stack: _Stack, values: dict): return case "call": + fname = node.children[0].match + if len(node.children) > 1: self._run(node.children[1], stack, values) args = stack.pop().list() else: args = () - if fn := self.functions.get(node.children[0].match): - # todo: Handle invalid parameters - stack.op0(fn(*args)) + if fn := self.functions.get(fname): + try: + stack.op0(fn(*args)) + except TypeError: + # TODO: Improve parameter validation + stack.op0(f"#ERR:Invalid call to {fname}()") else: - # todo: should this return a string? - raise NotImplementedError(f"Call to unknown function: {node.children[0].match!r}") + stack.op0(f"#ERR:Call to unknown function {fname}()") return diff --git a/logics-py/tests/test_logics.py b/logics-py/tests/test_logics.py index baf657b..f963197 100644 --- a/logics-py/tests/test_logics.py +++ b/logics-py/tests/test_logics.py @@ -19,7 +19,7 @@ def test_testcase(input): action = cmd[0].lower() if action == "expect": - expect = cmd[1] + expect = ":".join(cmd[1:]) assert last_result is not None, "#EXPECT used before evaluation" diff --git a/tests/function_calls.lgx b/tests/function_calls.lgx new file mode 100644 index 0000000..6d59e40 --- /dev/null +++ b/tests/function_calls.lgx @@ -0,0 +1,5 @@ +foo() +#EXPECT:"#ERR:Call to unknown function foo()" + +upper() +#EXPECT:"#ERR:Invalid call to upper()"