diff --git a/modcc/lexer.cpp b/modcc/lexer.cpp index 4ada753b83ff456fe76a3b078441c6cbc9ce503f..f89c3d323fce07909b1666cc825f1d44683511f2 100644 --- a/modcc/lexer.cpp +++ b/modcc/lexer.cpp @@ -435,10 +435,10 @@ void Lexer::binop_prec_init() { return; // I have taken the operator precedence from C++ - // Note that only infix operators require precidence. + // Note that only infix operators require precedence. binop_prec_[tok::eq] = 1; - binop_prec_[tok::land] = 2; - binop_prec_[tok::lor] = 3; + binop_prec_[tok::lor] = 2; + binop_prec_[tok::land] = 3; binop_prec_[tok::equality] = 4; binop_prec_[tok::ne] = 4; binop_prec_[tok::lt] = 5; diff --git a/modcc/parser.cpp b/modcc/parser.cpp index e54c9830940ba1828e8dd2e1a9b1ecd868ad9f0b..4b80992c7d228acfe9814de51df707ff39937036 100644 --- a/modcc/parser.cpp +++ b/modcc/parser.cpp @@ -1333,7 +1333,7 @@ expression_ptr Parser::parse_expression(int prec, tok stop_token) { auto p_op = binop_precedence(op.type); // Note: all tokens that are not infix binary operators have - // precidence of -1, so expressions like function calls will short + // precedence of -1, so expressions like function calls will short // circuit this loop here. if (p_op <= prec) return lhs; diff --git a/test/unit-modcc/test_parser.cpp b/test/unit-modcc/test_parser.cpp index ba8cc363b0fa8f7dfca67f59f8ca55e423928512..5916704cea23a602699877473a2aecdafc2fba05 100644 --- a/test/unit-modcc/test_parser.cpp +++ b/test/unit-modcc/test_parser.cpp @@ -602,6 +602,13 @@ double eval(Expression* e) { case tok::minus: return lhs - rhs; case tok::times: return lhs * rhs; case tok::divide: return lhs / rhs; + case tok::lt: return lhs < rhs; + case tok::lte: return lhs <= rhs; + case tok::gt: return lhs > rhs; + case tok::gte: return lhs >= rhs; + case tok::lnot: return lhs != rhs; + case tok::land: return lhs && rhs; + case tok::lor: return lhs || rhs; case tok::pow: return std::pow(lhs, rhs); case tok::min: return std::min(lhs, rhs); case tok::max: return std::max(lhs, rhs); @@ -665,6 +672,22 @@ TEST(Parser, parse_binop) { EXPECT_TRUE(check_parse(e, &Parser::parse_expression, test_case.first)); EXPECT_NEAR(eval(e.get()), test_case.second, 1e-10); } + + std::pair<const char*, bool> bool_tests[] = { + {"0 && 0 || 1", true}, + {"(0 && 0) || 1", true}, + {"0 && (0 || 1)", false}, + {"3<2 && 1 || 4>1", true}, + {"(3<2 && 1) || 4>1", true}, + {"3<2 && (1 || 4>1)", false}, + {"(3<2) && (1 || (4>1))", false}, + }; + + for (const auto& test_case: bool_tests) { + std::unique_ptr<Expression> e; + EXPECT_TRUE(check_parse(e, &Parser::parse_expression, test_case.first)); + EXPECT_EQ(eval(e.get()), test_case.second); + } } TEST(Parser, parse_state_block) {