From dbf1d5b2618308b3e4ed862b2530965bacfecbb5 Mon Sep 17 00:00:00 2001 From: "Allan K. Koech" Date: Thu, 5 Feb 2026 01:49:18 +0300 Subject: [PATCH] Added modulus, comparators and relational ops --- include/expr-eval/backend/runtime.h | 14 +++++ include/expr-eval/frontend/lexer.h | 12 ++++ include/expr-eval/frontend/parser.h | 8 +++ src/backend/runtime.cpp | 88 +++++++++++++++++++++++++++++ src/frontend/ast.cpp | 11 ++++ src/frontend/lexer.cpp | 49 ++++++++++++++++ src/frontend/parser.cpp | 59 ++++++++++++++++++- 7 files changed, 239 insertions(+), 2 deletions(-) diff --git a/include/expr-eval/backend/runtime.h b/include/expr-eval/backend/runtime.h index 7978d36..65ec5e0 100644 --- a/include/expr-eval/backend/runtime.h +++ b/include/expr-eval/backend/runtime.h @@ -39,6 +39,20 @@ 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; + + RuntimeVar operator!=(const RuntimeVar& other) const; + + 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/include/expr-eval/frontend/lexer.h b/include/expr-eval/frontend/lexer.h index 2beb4b3..aa04fee 100644 --- a/include/expr-eval/frontend/lexer.h +++ b/include/expr-eval/frontend/lexer.h @@ -16,6 +16,18 @@ enum class TokenType { TOK_SUB_OP, TOK_DIV_OP, TOK_MULT_OP, + TOK_MOD_OP, + + TOK_OR, // || + TOK_AND, // && + + TOK_EQ, // == + TOK_NEQ, // != + TOK_LT, // < + TOK_LT_EQ, // <= + TOK_GT, // > + TOK_GT_EQ, // >= + TOK_OPEN_PAREN, // ( TOK_CLOSE_PAREN, // ) diff --git a/include/expr-eval/frontend/parser.h b/include/expr-eval/frontend/parser.h index 0f371a8..9e02c4a 100644 --- a/include/expr-eval/frontend/parser.h +++ b/include/expr-eval/frontend/parser.h @@ -20,6 +20,14 @@ public: private: std::unique_ptr parseExpr(); + std::unique_ptr parseOr(); // || + + std::unique_ptr parseAnd(); // && + + std::unique_ptr parseEquality(); // == != + + std::unique_ptr parseRelational(); // < > <= >= + std::unique_ptr parseAdditives(); std::unique_ptr parseFactors(); diff --git a/src/backend/runtime.cpp b/src/backend/runtime.cpp index adbff07..9d1b367 100644 --- a/src/backend/runtime.cpp +++ b/src/backend/runtime.cpp @@ -1,4 +1,5 @@ #include "../../include/expr-eval/backend/runtime.h" +#include #include RuntimeVar::RuntimeVar() @@ -109,3 +110,90 @@ 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 { + if (type != other.type) + throw std::runtime_error(std::format("Expected same types to op '+' but found {} and {}.", + typeStr(), other.typeStr())); + + if (type == RuntimeVarType::NUMBER) { + const auto res = std::fmod(toDouble(), other.toDouble()); // + return RuntimeVar{res}; + } + + throw std::runtime_error(std::format("Op '%' not supported for type {}", typeStr())); +} + +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 {}.", + typeStr(), other.typeStr())); + + const auto res = value == other.value; + return RuntimeVar{res}; +} + +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 {}.", + typeStr(), other.typeStr())); + + const auto res = value != other.value; + return RuntimeVar{res}; +} + +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 {}.", + typeStr(), other.typeStr())); + + // TODO ... + if (type == RuntimeVarType::NUMBER || type == RuntimeVarType::STRING) { + const auto res = value > other.value; + return RuntimeVar{res}; + } + + throw std::runtime_error(std::format("Op '>' not supported for type {}", typeStr())); +} + +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 {}.", + typeStr(), other.typeStr())); + + // TODO ... + if (type == RuntimeVarType::NUMBER || type == RuntimeVarType::STRING) { + const auto res = value >= other.value; + return RuntimeVar{res}; + } + + throw std::runtime_error(std::format("Op '>=' not supported for type {}", typeStr())); +} + +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 {}.", + typeStr(), other.typeStr())); + + // TODO ... + if (type == RuntimeVarType::NUMBER || type == RuntimeVarType::STRING) { + const auto res = value < other.value; + return RuntimeVar{res}; + } + + throw std::runtime_error(std::format("Op '<' not supported for type {}", typeStr())); +} + +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 {}.", + typeStr(), other.typeStr())); + + // TODO ... + if (type == RuntimeVarType::NUMBER || type == RuntimeVarType::STRING) { + const auto res = value <= other.value; + return RuntimeVar{res}; + } + + throw std::runtime_error(std::format("Op '<=' not supported for type {}", typeStr())); +} diff --git a/src/frontend/ast.cpp b/src/frontend/ast.cpp index 343199f..668f03b 100644 --- a/src/frontend/ast.cpp +++ b/src/frontend/ast.cpp @@ -82,6 +82,17 @@ 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; + + // Relational + if (op == ">") return _l > _r; + if (op == ">=") return _l >= _r; + if (op == "<") return _l < _r; + if (op == "<=") return _l <= _r; throw std::runtime_error(std::format("Unimplemented op `{}`", op)); } diff --git a/src/frontend/lexer.cpp b/src/frontend/lexer.cpp index 8f51f2f..7fae1c6 100644 --- a/src/frontend/lexer.cpp +++ b/src/frontend/lexer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include Token::Token(const TokenType _type, std::string _name, std::string _value) : type(_type), @@ -104,6 +105,54 @@ void Lexer::tokenizeOperators() { m_tokens.emplace_back(TokenType::TOK_MULT_OP, "TOK_MULT_LIT", std::string{op}); else if (op == '/') m_tokens.emplace_back(TokenType::TOK_DIV_OP, "TOK_DIV_LIT", std::string{op}); + else if (op == '%') + m_tokens.emplace_back(TokenType::TOK_MOD_OP, "TOK_MOD_OP", std::string{op}); + + else if (op == '|') { + if(peek() == '|') { // || + m_tokens.emplace_back(TokenType::TOK_OR, "TOK_OR", std::string{"||"}); + advance(); + } + } + + else if (op == '&') { + if(peek() == '&') { // && + m_tokens.emplace_back(TokenType::TOK_AND, "TOK_AND", std::string{"&&"}); + advance(); + } + } + + else if (op == '=') { + if(peek() == '=') { // == + m_tokens.emplace_back(TokenType::TOK_EQ, "TOK_EQ", std::string{"=="}); + advance(); + } + } + + else if (op == '!') { + if(peek() == '=') { // != + m_tokens.emplace_back(TokenType::TOK_NEQ, "TOK_NEQ", std::string{"!="}); + advance(); + } + } + + else if (op == '<') { + if(peek() == '=') { // <= + m_tokens.emplace_back(TokenType::TOK_LT_EQ, "TOK_LT_EQ", std::string{"<="}); + advance(); + } else { // < + m_tokens.emplace_back(TokenType::TOK_LT, "TOK_LT", std::string{"<"}); + } + } + + else if (op == '>') { + if(peek() == '=') { // >= + m_tokens.emplace_back(TokenType::TOK_GT_EQ, "TOK_GT_EQ", std::string{">="}); + advance(); + } else { // > + m_tokens.emplace_back(TokenType::TOK_GT, "TOK_GT", std::string{">"}); + } + } else throw std::runtime_error(std::format("Unknown Character `{}` in the input string", peek())); } diff --git a/src/frontend/parser.cpp b/src/frontend/parser.cpp index 42a3ae1..08ab0c1 100644 --- a/src/frontend/parser.cpp +++ b/src/frontend/parser.cpp @@ -26,7 +26,61 @@ Program &Parser::root() { } std::unique_ptr Parser::parseExpr() { - return parseAdditives(); + return parseOr(); +} + +// a || b +std::unique_ptr Parser::parseOr() { + auto left = parseAnd(); + while (!eof() && ( + peek().type == TokenType::TOK_OR + )) { + std::string op = advance().value; + auto right = parseAnd(); + + left = std::make_unique(std::move(left), op, std::move(right)); + } + + return left; +} + +std::unique_ptr Parser::parseAnd() { + auto left = parseEquality(); + while (!eof() && ( peek().type == TokenType::TOK_AND )) { + std::string op = advance().value; + auto right = parseEquality(); + + left = std::make_unique(std::move(left), op, std::move(right)); + } + + return left; +} + +std::unique_ptr Parser::parseEquality() { + auto left = parseRelational(); + while (!eof() && ( peek().type == TokenType::TOK_EQ || peek().type == TokenType::TOK_NEQ )) { + std::string op = advance().value; + auto right = parseRelational(); + + left = std::make_unique(std::move(left), op, std::move(right)); + } + + return left; +} + +std::unique_ptr Parser::parseRelational() { + auto left = parseAdditives(); + while (!eof() && ( peek().type == TokenType::TOK_LT + || peek().type == TokenType::TOK_LT_EQ + || peek().type == TokenType::TOK_GT + || peek().type == TokenType::TOK_GT_EQ )) { + std::string op = advance().value; + auto right = parseAdditives(); + + left = std::make_unique(std::move(left), op, std::move(right)); + } + + return left; } std::unique_ptr Parser::parseAdditives() { @@ -48,7 +102,8 @@ 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_DIV_OP || + peek().type == TokenType::TOK_MOD_OP )) { std::string op = advance().value; auto right = parsePrimary();