Found a compiler bug, thanks Aedan!

This commit is contained in:
2023-03-17 14:01:16 +11:00
parent 4b83f1f0d6
commit 2edfbbe3ef
4 changed files with 82 additions and 3 deletions

27
scripts/example.toy Normal file
View File

@@ -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());

View File

@@ -331,9 +331,37 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
return node->binary.opcode; 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))) { //untangle in these cases - (WTF, are you serious?)
compiler->bytecode[compiler->count++] = (unsigned char)ret; //1 byte if (ret != TOY_OP_EOF) {
ret = TOY_OP_EOF; //untangle in this case 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 compiler->bytecode[compiler->count++] = (unsigned char)node->binary.opcode; //1 byte

View File

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

View File

@@ -114,6 +114,7 @@ int main() {
"dot-and-matrix.toy", "dot-and-matrix.toy",
"dot-assignments-bugfix.toy", "dot-assignments-bugfix.toy",
"dot-chaining.toy", "dot-chaining.toy",
"dot-modulo-bugfix.toy",
"dottify-bugfix.toy", "dottify-bugfix.toy",
"functions.toy", "functions.toy",
"index-arrays.toy", "index-arrays.toy",