From 7b26527e95ec3960e81c6b4a26a98b619ab1115a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 5 Feb 2023 06:28:18 +0000 Subject: [PATCH] Nesting index multiplication assignment fixed This affects all arithmetic types applied to inner-nested compounds. --- scripts/small.toy | 9 +++------ source/toy_compiler.c | 2 +- test/scripts/index-arrays.toy | 8 ++++++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/small.toy b/scripts/small.toy index 39d1e7f..e48d13d 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,9 +1,6 @@ +var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; -var d = ["foo" : ["bar" : ["bazz": ["fizz" : 5]]]]; +a[1][1] *= 10; -print d; - -d["foo"]["bar"]["bazz"]["fizz"] = 66; - -print d; \ No newline at end of file +print a; \ No newline at end of file diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 0f5520e..f28cef9 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -312,7 +312,7 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode //return this if... Toy_Opcode ret = Toy_writeCompilerWithJumps(compiler, node->binary.right, breakAddressesPtr, continueAddressesPtr, jumpOffsets, rootNode); - if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY && rootNode->binary.opcode == TOY_OP_VAR_ASSIGN) { //why var assign? + if (node->binary.opcode == TOY_OP_INDEX && rootNode->type == TOY_AST_NODE_BINARY && (rootNode->binary.opcode >= TOY_OP_VAR_ASSIGN && rootNode->binary.opcode <= TOY_OP_VAR_MODULO_ASSIGN)) { //range-based check for assignment type return TOY_OP_INDEX_ASSIGN_INTERMEDIATE; } diff --git a/test/scripts/index-arrays.toy b/test/scripts/index-arrays.toy index 2f94a25..6030574 100644 --- a/test/scripts/index-arrays.toy +++ b/test/scripts/index-arrays.toy @@ -71,6 +71,14 @@ assert a[0][0] == [42], "nested indexing failed"; } +//test nested indexing multipliciation assignment +{ + var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; + + a[1][1] *= 10; + + assert a == [[1, 2, 3], [4, 50, 6], [7, 8, 9]], "nested indexing multipliciation assignment failed"; +} //test combine example {