mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Fixed order of operations
This commit is contained in:
@@ -341,27 +341,27 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
|
||||
switch(parser->previous.type) {
|
||||
//arithmetic
|
||||
case TOY_TOKEN_PLUS: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM);
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM + 1);
|
||||
return TOY_OP_ADDITION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MINUS: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM);
|
||||
parsePrecedence(parser, nodeHandle, PREC_TERM + 1);
|
||||
return TOY_OP_SUBTRACTION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MULTIPLY: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
|
||||
return TOY_OP_MULTIPLICATION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_DIVIDE: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
|
||||
return TOY_OP_DIVISION;
|
||||
}
|
||||
|
||||
case TOY_TOKEN_MODULO: {
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
|
||||
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
|
||||
return TOY_OP_MODULO;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,5 +38,12 @@ s += "bar";
|
||||
|
||||
assert s == "foobar", "string addition failed (wasn't sticky enough)";
|
||||
|
||||
//check order of operations
|
||||
assert 30 / 3 * 2 == 20, "Order of operations failed (raw numbers)";
|
||||
var x = 30;
|
||||
var y = 3;
|
||||
var z = 2;
|
||||
assert x / y * z == 20, "Order of operations failed (variables)";
|
||||
|
||||
|
||||
print "All good";
|
||||
Reference in New Issue
Block a user