From 62fe86f99bb4be8cf75561787eee0d5b2e677959 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 9 Aug 2023 02:25:07 +1000 Subject: [PATCH] Fixed indexing in argument lists, resolved #102 --- scripts/test.toy | 21 +++++++++---------- source/toy_compiler.c | 2 +- .../indexing-in-argument-list-bugfix.toy | 20 ++++++++++++++++++ test/test_interpreter.c | 1 + 4 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 test/scripts/indexing-in-argument-list-bugfix.toy diff --git a/scripts/test.toy b/scripts/test.toy index 4401bb4..762cfff 100644 --- a/scripts/test.toy +++ b/scripts/test.toy @@ -1,13 +1,12 @@ -fn doA() { - print "doA()"; - return true; -} +import standard; +var array = [42]; -fn doB() { - print "doB()"; - return true; -} +var result = null; -if (doA() || doB()) { - print "success"; -} \ No newline at end of file +//problematic line +result = max(0, array[0]); + +assert result == 42, "Indexing in argument list failed"; + + +print "All good"; diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 030ddc8..5c622a0 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -649,7 +649,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode for (int i = 0; i < node->fnCall.arguments->fnCollection.count; i++) { //reverse order, to count from the beginning in the interpreter //sub-calls if (node->fnCall.arguments->fnCollection.nodes[i].type != TOY_AST_NODE_LITERAL) { - Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, &node->fnCall.arguments->fnCollection.nodes[i], breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode); + Toy_Opcode override = Toy_writeCompilerWithJumps(compiler, &node->fnCall.arguments->fnCollection.nodes[i], breakAddressesPtr, continueAddressesPtr, jumpOffsets, node); //BUGFIX: use node as rootNode, to allow indexing within argument lists if (override != TOY_OP_EOF) {//compensate for indexing & dot notation being screwy compiler->bytecode[compiler->count++] = (unsigned char)override; //1 byte } diff --git a/test/scripts/indexing-in-argument-list-bugfix.toy b/test/scripts/indexing-in-argument-list-bugfix.toy new file mode 100644 index 0000000..f11d721 --- /dev/null +++ b/test/scripts/indexing-in-argument-list-bugfix.toy @@ -0,0 +1,20 @@ +fn max(lhs, rhs) { + if (lhs > rhs) { + return lhs; + } + else { + return rhs; + } +} + +var array = [42]; + +var result = null; + +//problematic line +result = max(0, array[0]); + +assert result == 42, "Indexing in argument list failed"; + + +print "All good"; diff --git a/test/test_interpreter.c b/test/test_interpreter.c index 0f6537d..2c12b59 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -129,6 +129,7 @@ int main() { "index-assignment-left-bugfix.toy", "index-dictionaries.toy", "index-strings.toy", + "indexing-in-argument-list-bugfix.toy", "jumps.toy", "jumps-in-functions.toy", "logicals.toy",