#include "stmt.hh" #include #include Stmt::Stmt(StmtType type, StmtVariant value) : type{type}, value{value} {} // Copy constructor Stmt::Stmt(const Stmt &other) : type{other.type} { switch(type) { case StmtType::EXPRESSION: { std::shared_ptr<_Expression> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::make_shared<_Expression>(*ptr); break; } case StmtType::PRINT: { std::shared_ptr<_Print> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::make_shared<_Print>(*ptr); break; } case StmtType::VAR: { std::shared_ptr<_Var> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::make_shared<_Var>(*ptr); break; } } } // Move constructor Stmt::Stmt(const Stmt &&other) : type{other.type}, value{std::move(other.value)} { switch(type) { case StmtType::EXPRESSION: { std::shared_ptr<_Expression> ptr = std::get>(other.value); assert(ptr != nullptr); ptr.reset(); break; } case StmtType::PRINT: { std::shared_ptr<_Print> ptr = std::get>(other.value); assert(ptr != nullptr); ptr.reset(); break; } case StmtType::VAR: { std::shared_ptr<_Var> ptr = std::get>(other.value); assert(ptr != nullptr); ptr.reset(); break; } } } // Copy assignment Stmt &Stmt::operator=(const Stmt &other) { if (this == &other) { return *this; } switch(type) { case StmtType::EXPRESSION: { std::shared_ptr<_Expression> ptr = std::get>(value); assert(ptr != nullptr); ptr.reset(); break; } case StmtType::PRINT: { std::shared_ptr<_Print> ptr = std::get>(value); assert(ptr != nullptr); ptr.reset(); break; } case StmtType::VAR: { std::shared_ptr<_Var> ptr = std::get>(value); assert(ptr != nullptr); ptr.reset(); break; } } type = other.type; switch(type) { case StmtType::EXPRESSION: { std::shared_ptr<_Expression> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::make_shared<_Expression>(*ptr); break; } case StmtType::PRINT: { std::shared_ptr<_Print> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::make_shared<_Print>(*ptr); break; } case StmtType::VAR: { std::shared_ptr<_Var> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::make_shared<_Var>(*ptr); break; } } return *this; } // Move assignment Stmt &Stmt::operator=(const Stmt &&other) { if (this == &other) { return *this; } switch(type) { case StmtType::EXPRESSION: { std::shared_ptr<_Expression> ptr = std::get>(value); assert(ptr != nullptr); ptr.reset(); break; } case StmtType::PRINT: { std::shared_ptr<_Print> ptr = std::get>(value); assert(ptr != nullptr); ptr.reset(); break; } case StmtType::VAR: { std::shared_ptr<_Var> ptr = std::get>(value); assert(ptr != nullptr); ptr.reset(); break; } } type = other.type; switch(type) { case StmtType::EXPRESSION: { std::shared_ptr<_Expression> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::move(other.value); ptr.reset(); break; } case StmtType::PRINT: { std::shared_ptr<_Print> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::move(other.value); ptr.reset(); break; } case StmtType::VAR: { std::shared_ptr<_Var> ptr = std::get>(other.value); assert(ptr != nullptr); value = std::move(other.value); ptr.reset(); break; } } return *this; }