From 2edfbbe3efe1215ef45c033b39488ac363f8fdc4 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 17 Mar 2023 14:01:16 +1100 Subject: [PATCH] Found a compiler bug, thanks Aedan! --- scripts/example.toy | 27 ++++++++++++++++++++++++ source/toy_compiler.c | 34 +++++++++++++++++++++++++++--- test/scripts/dot-modulo-bugfix.toy | 23 ++++++++++++++++++++ test/test_interpreter.c | 1 + 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 scripts/example.toy create mode 100644 test/scripts/dot-modulo-bugfix.toy diff --git a/scripts/example.toy b/scripts/example.toy new file mode 100644 index 0000000..7649981 --- /dev/null +++ b/scripts/example.toy @@ -0,0 +1,27 @@ +var colors = [ + "red", + "orange", + "yellow", + "green", + "blue", + "violet", + "indigo" +]; + +fn getRainbowColor(index) { + return colors[index]; +} + + +import standard; +import random; + +var rng: opaque = createRandomGenerator(hash(clock())); + + + +print getRainbowColor(rng.generateRandomNumber() % colors.length()); +print getRainbowColor(rng.generateRandomNumber() % colors.length()); +print getRainbowColor(rng.generateRandomNumber() % colors.length()); +print getRainbowColor(rng.generateRandomNumber() % colors.length()); +print getRainbowColor(rng.generateRandomNumber() % colors.length()); diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 9dce1b8..4629daa 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -331,9 +331,37 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode return node->binary.opcode; } - if (ret != TOY_OP_EOF && (node->binary.opcode == TOY_OP_VAR_ASSIGN || node->binary.opcode == TOY_OP_AND || node->binary.opcode == TOY_OP_OR || (node->binary.opcode >= TOY_OP_COMPARE_EQUAL && node->binary.opcode <= TOY_OP_INVERT))) { - compiler->bytecode[compiler->count++] = (unsigned char)ret; //1 byte - ret = TOY_OP_EOF; //untangle in this case + //untangle in these cases - (WTF, are you serious?) + if (ret != TOY_OP_EOF) { + switch(node->binary.opcode) { + case TOY_OP_NEGATE: + case TOY_OP_ADDITION: + case TOY_OP_SUBTRACTION: + case TOY_OP_MULTIPLICATION: + case TOY_OP_DIVISION: + case TOY_OP_MODULO: + case TOY_OP_VAR_ASSIGN: + case TOY_OP_VAR_ADDITION_ASSIGN: + case TOY_OP_VAR_SUBTRACTION_ASSIGN: + case TOY_OP_VAR_MULTIPLICATION_ASSIGN: + case TOY_OP_VAR_DIVISION_ASSIGN: + case TOY_OP_VAR_MODULO_ASSIGN: + case TOY_OP_COMPARE_EQUAL: + case TOY_OP_COMPARE_NOT_EQUAL: + case TOY_OP_COMPARE_LESS: + case TOY_OP_COMPARE_LESS_EQUAL: + case TOY_OP_COMPARE_GREATER: + case TOY_OP_COMPARE_GREATER_EQUAL: + case TOY_OP_INVERT: + case TOY_OP_AND: + case TOY_OP_OR: + //place the rhs result before the outer instruction + compiler->bytecode[compiler->count++] = (unsigned char)ret; //1 byte + ret = TOY_OP_EOF; + + default: + break; + } } compiler->bytecode[compiler->count++] = (unsigned char)node->binary.opcode; //1 byte diff --git a/test/scripts/dot-modulo-bugfix.toy b/test/scripts/dot-modulo-bugfix.toy new file mode 100644 index 0000000..6ab1080 --- /dev/null +++ b/test/scripts/dot-modulo-bugfix.toy @@ -0,0 +1,23 @@ +var days = [ + "sunday", + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday" +]; + +var rng = 10; //for chosen at random + + +var index = rng % days.length(); + +assert index == 3, "dot modulo bugfix failed"; + +rng %= days.length(); + +assert rng == 3, "dot modulo assign bugfix failed"; + + +print "All good"; diff --git a/test/test_interpreter.c b/test/test_interpreter.c index 615490d..e2be6ab 100644 --- a/test/test_interpreter.c +++ b/test/test_interpreter.c @@ -114,6 +114,7 @@ int main() { "dot-and-matrix.toy", "dot-assignments-bugfix.toy", "dot-chaining.toy", + "dot-modulo-bugfix.toy", "dottify-bugfix.toy", "functions.toy", "index-arrays.toy",