From 09e128ce98de793d22669255897e4b4816d0e53e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Mon, 2 Feb 2026 09:02:39 +0000 Subject: Added logical operators --- lib/Parser/Parser.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'lib/Parser') diff --git a/lib/Parser/Parser.cc b/lib/Parser/Parser.cc index 32a6db8..c5e91f9 100644 --- a/lib/Parser/Parser.cc +++ b/lib/Parser/Parser.cc @@ -6,6 +6,9 @@ using namespace cps; Parser::Parser(Lexer &L) : Lex(L) { getNextToken(); + BinopPrecedence[tok_or] = 3; + BinopPrecedence[tok_and] = 5; + BinopPrecedence[tok_eq] = 10; BinopPrecedence[tok_ne] = 10; BinopPrecedence[tok_le] = 10; @@ -40,7 +43,6 @@ std::unique_ptr Parser::ParseNumberExpr() { getNextToken(); return Result; } - // Default to Int auto Result = std::make_unique(Lex.NumVal); getNextToken(); return Result; @@ -126,6 +128,16 @@ std::unique_ptr Parser::ParsePrimary() { } } +std::unique_ptr Parser::ParseUnary() { + if (CurTok == tok_not) { + getNextToken(); + auto Operand = ParseUnary(); + if (!Operand) return nullptr; + return std::make_unique(tok_not, std::move(Operand)); + } + return ParsePrimary(); +} + std::unique_ptr Parser::ParseBinOpRHS(int ExprPrec, std::unique_ptr LHS) { while (true) { int TokPrec = GetTokPrecedence(); @@ -135,7 +147,7 @@ std::unique_ptr Parser::ParseBinOpRHS(int ExprPrec, std::unique_ptr Parser::ParseBinOpRHS(int ExprPrec, std::unique_ptr Parser::ParseExpression() { - auto LHS = ParsePrimary(); + auto LHS = ParseUnary(); if (!LHS) return nullptr; return ParseBinOpRHS(0, std::move(LHS)); } -- cgit v1.3.1-4-g156e