Skip to content

Commit

Permalink
Improved handling of invalid or unknown functions
Browse files Browse the repository at this point in the history
  • Loading branch information
phorward committed Oct 23, 2023
1 parent 77f5b18 commit 7ca2591
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
15 changes: 10 additions & 5 deletions logics-js/logics.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,31 @@ export default class Logics {
}
},
call: () => {
let fname = node.children[0].match;
let args = [];

if (node.children.length === 2) {
this.traverse(node.children[1], stack, values);
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
for (let i in args) {
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: () => {
Expand Down
2 changes: 1 addition & 1 deletion logics-js/test/logics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 9 additions & 5 deletions logics-py/logics/logics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion logics-py/tests/test_logics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 5 additions & 0 deletions tests/function_calls.lgx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo()
#EXPECT:"#ERR:Call to unknown function foo()"

upper()
#EXPECT:"#ERR:Invalid call to upper()"

0 comments on commit 7ca2591

Please sign in to comment.