mirror of
https://github.com/allankoechke/cpp-expr-eval.git
synced 2026-11-30 17:11:34 +00:00
Added modulus, comparators and relational ops
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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, // )
|
||||
|
||||
@@ -20,6 +20,14 @@ public:
|
||||
private:
|
||||
std::unique_ptr<Node> parseExpr();
|
||||
|
||||
std::unique_ptr<Node> parseOr(); // ||
|
||||
|
||||
std::unique_ptr<Node> parseAnd(); // &&
|
||||
|
||||
std::unique_ptr<Node> parseEquality(); // == !=
|
||||
|
||||
std::unique_ptr<Node> parseRelational(); // < > <= >=
|
||||
|
||||
std::unique_ptr<Node> parseAdditives();
|
||||
|
||||
std::unique_ptr<Node> parseFactors();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "../../include/expr-eval/backend/runtime.h"
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
@@ -82,6 +82,17 @@ RuntimeVar BinaryExpr::eval(std::unordered_map<std::string, RuntimeVar> &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));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <cctype>
|
||||
#include <stdexcept>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
@@ -26,7 +26,61 @@ Program &Parser::root() {
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parseExpr() {
|
||||
return parseAdditives();
|
||||
return parseOr();
|
||||
}
|
||||
|
||||
// a || b
|
||||
std::unique_ptr<Node> Parser::parseOr() {
|
||||
auto left = parseAnd();
|
||||
while (!eof() && (
|
||||
peek().type == TokenType::TOK_OR
|
||||
)) {
|
||||
std::string op = advance().value;
|
||||
auto right = parseAnd();
|
||||
|
||||
left = std::make_unique<BinaryExpr>(std::move(left), op, std::move(right));
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parseAnd() {
|
||||
auto left = parseEquality();
|
||||
while (!eof() && ( peek().type == TokenType::TOK_AND )) {
|
||||
std::string op = advance().value;
|
||||
auto right = parseEquality();
|
||||
|
||||
left = std::make_unique<BinaryExpr>(std::move(left), op, std::move(right));
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> 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<BinaryExpr>(std::move(left), op, std::move(right));
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> 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<BinaryExpr>(std::move(left), op, std::move(right));
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parseAdditives() {
|
||||
@@ -48,7 +102,8 @@ std::unique_ptr<Node> 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();
|
||||
|
||||
Reference in New Issue
Block a user