Skip to content

Commit

Permalink
Fixed a post-fix increment issue, highlighted in the game engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Jun 20, 2023
1 parent f6ec6a8 commit 0e41b00
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/toy_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#define TOY_VERSION_MAJOR 1
#define TOY_VERSION_MINOR 1
#define TOY_VERSION_PATCH 5
#define TOY_VERSION_PATCH 6
#define TOY_VERSION_BUILD Toy_private_version_build()

//platform/compiler-specific instructions
Expand Down
4 changes: 4 additions & 0 deletions source/toy_opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@ typedef enum Toy_Opcode {
TOY_OP_FN_END, //different from SECTION_END
TOY_OP_SECTION_END = 255,
//TODO: add more

//prefix & postfix signals (used internally)
TOY_OP_PREFIX,
TOY_OP_POSTFIX,
} Toy_Opcode;

15 changes: 11 additions & 4 deletions source/toy_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ static Toy_Opcode incrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle)

Toy_freeASTNode(tmpNode);

return TOY_OP_EOF;
return TOY_OP_PREFIX;
}

static Toy_Opcode incrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
Expand All @@ -710,7 +710,7 @@ static Toy_Opcode incrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {

Toy_freeASTNode(tmpNode);

return TOY_OP_EOF;
return TOY_OP_POSTFIX;
}

static Toy_Opcode decrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
Expand All @@ -727,7 +727,7 @@ static Toy_Opcode decrementPrefix(Toy_Parser* parser, Toy_ASTNode** nodeHandle)

Toy_freeASTNode(tmpNode);

return TOY_OP_EOF;
return TOY_OP_PREFIX;
}

static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
Expand All @@ -744,7 +744,7 @@ static Toy_Opcode decrementInfix(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {

Toy_freeASTNode(tmpNode);

return TOY_OP_EOF;
return TOY_OP_POSTFIX;
}

static Toy_Opcode fnCall(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
Expand Down Expand Up @@ -1287,6 +1287,13 @@ static void parsePrecedence(Toy_Parser* parser, Toy_ASTNode** nodeHandle, Preced
continue;
}

//BUGFIX: keep going, don't skip out on a postfix
if (opcode == TOY_OP_PREFIX || opcode == TOY_OP_POSTFIX) {
Toy_freeASTNode(*nodeHandle);
*nodeHandle = rhsNode;
continue;
}

Toy_emitASTNodeBinary(nodeHandle, rhsNode, opcode);

//optimise away the constants
Expand Down
15 changes: 15 additions & 0 deletions test/scripts/increment-postfix-bugfix.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var a = 0;

if (a++ >= 1) {
assert false, "increment postfix bugfix failed (first check)";
}


if (a++ >= 1) {

}

assert a == 2, "increment postfix bugfix failed (second check)";


print "All good";
1 change: 1 addition & 0 deletions test/test_interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ int main() {
"dottify-bugfix.toy",
"function-within-function-bugfix.toy",
"functions.toy",
"increment-postfix-bugfix.toy",
"index-arrays.toy",
"index-assignment-both-bugfix.toy",
"index-assignment-intermediate-bugfix.toy",
Expand Down

0 comments on commit 0e41b00

Please sign in to comment.