mirror of
https://github.com/allankoechke/cpp-expr-eval.git
synced 2026-12-03 01:11:19 +00:00
Added &&, || and fixed implicit AND
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "../../include/expr-eval/backend/runtime.h"
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
@@ -84,6 +84,10 @@ 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;
|
||||
|
||||
@@ -100,15 +100,23 @@ std::unique_ptr<Node> Parser::parseAdditives() {
|
||||
|
||||
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_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<BinaryExpr>(std::move(left), op, std::move(right));
|
||||
left = std::make_unique<BinaryExpr>(std::move(left), op, std::move(right));
|
||||
}
|
||||
|
||||
else if(peek().type == TokenType::TOK_OPEN_PAREN) {
|
||||
auto right = parsePrimary();
|
||||
left = std::make_unique<BinaryExpr>(std::move(left), "*", std::move(right));
|
||||
}
|
||||
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
return left;
|
||||
|
||||
Reference in New Issue
Block a user