From f84cdff88319108d1a8e0f910700ec5d570af9f4 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 7 Mar 2023 06:49:17 +1100 Subject: [PATCH] Fixed order of operations --- source/toy_parser.c | 10 +++++----- test/scripts/arithmetic.toy | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/source/toy_parser.c b/source/toy_parser.c index f9a594d..89b53fb 100644 --- a/source/toy_parser.c +++ b/source/toy_parser.c @@ -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; } diff --git a/test/scripts/arithmetic.toy b/test/scripts/arithmetic.toy index 27d0c4f..c773ef7 100644 --- a/test/scripts/arithmetic.toy +++ b/test/scripts/arithmetic.toy @@ -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"; \ No newline at end of file