Fixed indexing in argument lists, resolved #102

This commit is contained in:
2023-08-09 02:25:07 +10:00
parent 401de578a5
commit 62fe86f99b
4 changed files with 32 additions and 12 deletions

View File

@@ -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";
}
//problematic line
result = max(0, array[0]);
assert result == 42, "Indexing in argument list failed";
print "All good";

View File

@@ -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
}

View File

@@ -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";

View File

@@ -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",