From f05eb62e62bccceddb33f09cc2e77eb77566a13c Mon Sep 17 00:00:00 2001 From: "Allan K. Koech" Date: Thu, 5 Feb 2026 02:18:16 +0300 Subject: [PATCH] Added &&, || and fixed implicit AND --- include/expr-eval/backend/runtime.h | 4 ++++ src/backend/runtime.cpp | 31 +++++++++++++++++++---------- src/frontend/ast.cpp | 4 ++++ src/frontend/parser.cpp | 24 ++++++++++++++-------- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/include/expr-eval/backend/runtime.h b/include/expr-eval/backend/runtime.h index 65ec5e0..241fb87 100644 --- a/include/expr-eval/backend/runtime.h +++ b/include/expr-eval/backend/runtime.h @@ -53,6 +53,10 @@ struct RuntimeVar { RuntimeVar operator<(const RuntimeVar& other) const; RuntimeVar operator<=(const RuntimeVar& other) const; + + RuntimeVar operator||(const RuntimeVar& other) const; + + RuntimeVar operator&&(const RuntimeVar& other) const; }; #endif // RUNTIME_H diff --git a/src/backend/runtime.cpp b/src/backend/runtime.cpp index 9d1b367..2f1ed4a 100644 --- a/src/backend/runtime.cpp +++ b/src/backend/runtime.cpp @@ -1,5 +1,6 @@ #include "../../include/expr-eval/backend/runtime.h" #include +#include #include RuntimeVar::RuntimeVar() @@ -74,7 +75,7 @@ RuntimeVar RuntimeVar::operator+(const RuntimeVar &other) const { RuntimeVar RuntimeVar::operator-(const RuntimeVar &other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '-' but found {} and {}.", typeStr(), other.typeStr())); if (type == RuntimeVarType::NUMBER) { @@ -87,7 +88,7 @@ RuntimeVar RuntimeVar::operator-(const RuntimeVar &other) const { RuntimeVar RuntimeVar::operator*(const RuntimeVar &other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '*' but found {} and {}.", typeStr(), other.typeStr())); if (type == RuntimeVarType::NUMBER) { @@ -100,7 +101,7 @@ RuntimeVar RuntimeVar::operator*(const RuntimeVar &other) const { RuntimeVar RuntimeVar::operator/(const RuntimeVar &other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '/' but found {} and {}.", typeStr(), other.typeStr())); if (type == RuntimeVarType::NUMBER) { @@ -113,7 +114,7 @@ RuntimeVar RuntimeVar::operator/(const RuntimeVar &other) const { RuntimeVar RuntimeVar::operator%(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '%' but found {} and {}.", typeStr(), other.typeStr())); if (type == RuntimeVarType::NUMBER) { @@ -126,7 +127,7 @@ RuntimeVar RuntimeVar::operator%(const RuntimeVar& other) const { RuntimeVar RuntimeVar::operator==(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '==' but found {} and {}.", typeStr(), other.typeStr())); const auto res = value == other.value; @@ -135,7 +136,7 @@ RuntimeVar RuntimeVar::operator==(const RuntimeVar& other) const { RuntimeVar RuntimeVar::operator!=(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '!=' but found {} and {}.", typeStr(), other.typeStr())); const auto res = value != other.value; @@ -144,7 +145,7 @@ RuntimeVar RuntimeVar::operator!=(const RuntimeVar& other) const { RuntimeVar RuntimeVar::operator>(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '>' but found {} and {}.", typeStr(), other.typeStr())); // TODO ... @@ -158,7 +159,7 @@ RuntimeVar RuntimeVar::operator>(const RuntimeVar& other) const { RuntimeVar RuntimeVar::operator>=(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '>=' but found {} and {}.", typeStr(), other.typeStr())); // TODO ... @@ -172,7 +173,7 @@ RuntimeVar RuntimeVar::operator>=(const RuntimeVar& other) const { RuntimeVar RuntimeVar::operator<(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '<' but found {} and {}.", typeStr(), other.typeStr())); // TODO ... @@ -186,7 +187,7 @@ RuntimeVar RuntimeVar::operator<(const RuntimeVar& other) const { RuntimeVar RuntimeVar::operator<=(const RuntimeVar& other) const { if (type != other.type) - throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + throw std::runtime_error(std::format("Expected same types to op '<=' but found {} and {}.", typeStr(), other.typeStr())); // TODO ... @@ -197,3 +198,13 @@ RuntimeVar RuntimeVar::operator<=(const RuntimeVar& other) const { throw std::runtime_error(std::format("Op '<=' not supported for type {}", typeStr())); } + +RuntimeVar RuntimeVar::operator||(const RuntimeVar& other) const { + const auto res = toBool() || other.toBool(); + return RuntimeVar{res}; +} + +RuntimeVar RuntimeVar::operator&&(const RuntimeVar& other) const { + const auto res = toBool() && other.toBool(); + return RuntimeVar{res}; +} diff --git a/src/frontend/ast.cpp b/src/frontend/ast.cpp index 668f03b..839c95e 100644 --- a/src/frontend/ast.cpp +++ b/src/frontend/ast.cpp @@ -84,6 +84,10 @@ RuntimeVar BinaryExpr::eval(std::unordered_map &vars) { if (op == "/") return _l / _r; if (op == "%") return _l % _r; + // ||, && + if (op == "||") return _l || _r; + if (op == "&&") return _l && _r; + // Equality if (op == "==") return _l == _r; if (op == "!=") return _l != _r; diff --git a/src/frontend/parser.cpp b/src/frontend/parser.cpp index 08ab0c1..c22c083 100644 --- a/src/frontend/parser.cpp +++ b/src/frontend/parser.cpp @@ -100,15 +100,23 @@ std::unique_ptr Parser::parseAdditives() { std::unique_ptr Parser::parseFactors() { auto left = parsePrimary(); - while (!eof() && ( - peek().type == TokenType::TOK_MULT_OP || - peek().type == TokenType::TOK_DIV_OP || - peek().type == TokenType::TOK_MOD_OP - )) { - std::string op = advance().value; - auto right = parsePrimary(); + while (!eof()) { + if ( peek().type == TokenType::TOK_MULT_OP || + peek().type == TokenType::TOK_DIV_OP || + peek().type == TokenType::TOK_MOD_OP) { + std::string op = advance().value; + auto right = parsePrimary(); - left = std::make_unique(std::move(left), op, std::move(right)); + left = std::make_unique(std::move(left), op, std::move(right)); + } + + else if(peek().type == TokenType::TOK_OPEN_PAREN) { + auto right = parsePrimary(); + left = std::make_unique(std::move(left), "*", std::move(right)); + } + + else + break; } return left;