#include "expr.hh" #include "../tokenizer.hh" #include #include #include Expr::Expr(ExprType type, ExprVariant value) : type{type}, value{value} {} // Copy constructor Expr::Expr(const Expr &other) : type{other.type} { switch(type) { case ExprType::BINARY: { std::shared_ptr<_Binary> ptr = std::get>(other.value); value = std::make_shared<_Binary>(*ptr); break; } case ExprType::GROUPING: { std::shared_ptr<_Grouping> ptr = std::get>(other.value); value = std::make_shared<_Grouping>(*ptr); break; } case ExprType::LITERAL: { std::shared_ptr<_Literal> ptr = std::get>(other.value); value = std::make_shared<_Literal>(*ptr); break; } case ExprType::UNARY: { std::shared_ptr<_Unary> ptr = std::get>(other.value); value = std::make_shared<_Unary>(*ptr); break; } } } // Move constructor Expr::Expr(const Expr &&other) : type{other.type}, value{std::move(other.value)} { switch(type) { case ExprType::BINARY: { std::shared_ptr<_Binary> ptr = std::get>(other.value); ptr.reset(); break; } case ExprType::GROUPING: { std::shared_ptr<_Grouping> ptr = std::get>(other.value); ptr.reset(); break; } case ExprType::LITERAL: { std::shared_ptr<_Literal> ptr = std::get>(other.value); ptr.reset(); break; } case ExprType::UNARY: { std::shared_ptr<_Unary> ptr = std::get>(other.value); ptr.reset(); break; } } } // Copy assignment Expr &Expr::operator=(const Expr &other) { if (this == &other) { return *this; } switch(type) { case ExprType::BINARY: { std::shared_ptr<_Binary> ptr = std::get>(value); ptr.reset(); break; } case ExprType::GROUPING: { std::shared_ptr<_Grouping> ptr = std::get>(value); ptr.reset(); break; } case ExprType::LITERAL: { std::shared_ptr<_Literal> ptr = std::get>(value); ptr.reset(); break; } case ExprType::UNARY: { std::shared_ptr<_Unary> ptr = std::get>(value); ptr.reset(); break; } } type = other.type; switch(type) { case ExprType::BINARY: { std::shared_ptr<_Binary> ptr = std::get>(other.value); value = std::make_shared<_Binary>(*ptr); break; } case ExprType::GROUPING: { std::shared_ptr<_Grouping> ptr = std::get>(other.value); value = std::make_shared<_Grouping>(*ptr); break; } case ExprType::LITERAL: { std::shared_ptr<_Literal> ptr = std::get>(other.value); value = std::make_shared<_Literal>(*ptr); break; } case ExprType::UNARY: { std::shared_ptr<_Unary> ptr = std::get>(other.value); value = std::make_shared<_Unary>(*ptr); break; } } return *this; } // Move assignment Expr &Expr::operator=(const Expr &&other) { if (this == &other) { return *this; } switch(type) { case ExprType::BINARY: { std::shared_ptr<_Binary> ptr = std::get>(value); ptr.reset(); break; } case ExprType::GROUPING: { std::shared_ptr<_Grouping> ptr = std::get>(value); ptr.reset(); break; } case ExprType::LITERAL: { std::shared_ptr<_Literal> ptr = std::get>(value); ptr.reset(); break; } case ExprType::UNARY: { std::shared_ptr<_Unary> ptr = std::get>(value); ptr.reset(); break; } } type = other.type; switch(type) { case ExprType::BINARY: { std::shared_ptr<_Binary> optr = std::get>(other.value); value = std::move(other.value); optr.reset(); break; } case ExprType::GROUPING: { std::shared_ptr<_Grouping> optr = std::get>(other.value); value = std::move(other.value); optr.reset(); break; } case ExprType::LITERAL: { std::shared_ptr<_Literal> optr = std::get>(other.value); value = std::move(other.value); optr.reset(); break; } case ExprType::UNARY: { std::shared_ptr<_Unary> optr = std::get>(other.value); value = std::move(other.value); optr.reset(); break; } } return *this; }