163 lines
4.2 KiB
C++
163 lines
4.2 KiB
C++
#include "stmt.hh"
|
|
#include <cassert>
|
|
#include <utility>
|
|
|
|
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<std::shared_ptr<_Expression>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::make_shared<_Expression>(*ptr);
|
|
break;
|
|
}
|
|
case StmtType::PRINT: {
|
|
std::shared_ptr<_Print> ptr = std::get<std::shared_ptr<_Print>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::make_shared<_Print>(*ptr);
|
|
break;
|
|
}
|
|
case StmtType::VAR: {
|
|
std::shared_ptr<_Var> ptr = std::get<std::shared_ptr<_Var>>(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<std::shared_ptr<_Expression>>(other.value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::PRINT: {
|
|
std::shared_ptr<_Print> ptr = std::get<std::shared_ptr<_Print>>(other.value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::VAR: {
|
|
std::shared_ptr<_Var> ptr = std::get<std::shared_ptr<_Var>>(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<std::shared_ptr<_Expression>>(value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::PRINT: {
|
|
std::shared_ptr<_Print> ptr = std::get<std::shared_ptr<_Print>>(value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::VAR: {
|
|
std::shared_ptr<_Var> ptr = std::get<std::shared_ptr<_Var>>(value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
}
|
|
|
|
type = other.type;
|
|
|
|
switch(type) {
|
|
case StmtType::EXPRESSION: {
|
|
std::shared_ptr<_Expression> ptr = std::get<std::shared_ptr<_Expression>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::make_shared<_Expression>(*ptr);
|
|
break;
|
|
}
|
|
case StmtType::PRINT: {
|
|
std::shared_ptr<_Print> ptr = std::get<std::shared_ptr<_Print>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::make_shared<_Print>(*ptr);
|
|
break;
|
|
}
|
|
case StmtType::VAR: {
|
|
std::shared_ptr<_Var> ptr = std::get<std::shared_ptr<_Var>>(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<std::shared_ptr<_Expression>>(value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::PRINT: {
|
|
std::shared_ptr<_Print> ptr = std::get<std::shared_ptr<_Print>>(value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::VAR: {
|
|
std::shared_ptr<_Var> ptr = std::get<std::shared_ptr<_Var>>(value);
|
|
assert(ptr != nullptr);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
}
|
|
|
|
type = other.type;
|
|
|
|
switch(type) {
|
|
case StmtType::EXPRESSION: {
|
|
std::shared_ptr<_Expression> ptr = std::get<std::shared_ptr<_Expression>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::move(other.value);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::PRINT: {
|
|
std::shared_ptr<_Print> ptr = std::get<std::shared_ptr<_Print>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::move(other.value);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
case StmtType::VAR: {
|
|
std::shared_ptr<_Var> ptr = std::get<std::shared_ptr<_Var>>(other.value);
|
|
assert(ptr != nullptr);
|
|
value = std::move(other.value);
|
|
ptr.reset();
|
|
break;
|
|
}
|
|
}
|
|
|
|
return *this;
|
|
}
|