mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Found a compiler bug, thanks Aedan!
This commit is contained in:
27
scripts/example.toy
Normal file
27
scripts/example.toy
Normal 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());
|
||||||
@@ -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?)
|
||||||
|
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
|
compiler->bytecode[compiler->count++] = (unsigned char)ret; //1 byte
|
||||||
ret = TOY_OP_EOF; //untangle in this case
|
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
|
||||||
|
|||||||
23
test/scripts/dot-modulo-bugfix.toy
Normal file
23
test/scripts/dot-modulo-bugfix.toy
Normal 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";
|
||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user