Skip to content

Commit f84cdff

Browse files
committed
Fixed order of operations
1 parent f869c94 commit f84cdff

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

source/toy_parser.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,27 +341,27 @@ static Toy_Opcode binary(Toy_Parser* parser, Toy_ASTNode** nodeHandle) {
341341
switch(parser->previous.type) {
342342
//arithmetic
343343
case TOY_TOKEN_PLUS: {
344-
parsePrecedence(parser, nodeHandle, PREC_TERM);
344+
parsePrecedence(parser, nodeHandle, PREC_TERM + 1);
345345
return TOY_OP_ADDITION;
346346
}
347347

348348
case TOY_TOKEN_MINUS: {
349-
parsePrecedence(parser, nodeHandle, PREC_TERM);
349+
parsePrecedence(parser, nodeHandle, PREC_TERM + 1);
350350
return TOY_OP_SUBTRACTION;
351351
}
352352

353353
case TOY_TOKEN_MULTIPLY: {
354-
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
354+
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
355355
return TOY_OP_MULTIPLICATION;
356356
}
357357

358358
case TOY_TOKEN_DIVIDE: {
359-
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
359+
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
360360
return TOY_OP_DIVISION;
361361
}
362362

363363
case TOY_TOKEN_MODULO: {
364-
parsePrecedence(parser, nodeHandle, PREC_FACTOR);
364+
parsePrecedence(parser, nodeHandle, PREC_FACTOR + 1);
365365
return TOY_OP_MODULO;
366366
}
367367

test/scripts/arithmetic.toy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,12 @@ s += "bar";
3838

3939
assert s == "foobar", "string addition failed (wasn't sticky enough)";
4040

41+
//check order of operations
42+
assert 30 / 3 * 2 == 20, "Order of operations failed (raw numbers)";
43+
var x = 30;
44+
var y = 3;
45+
var z = 2;
46+
assert x / y * z == 20, "Order of operations failed (variables)";
47+
4148

4249
print "All good";

0 commit comments

Comments
 (0)