Remove unnecessary code

This commit is contained in:
Abdelrahman Said 2025-06-29 20:24:01 +01:00
parent 4f39417d3e
commit fb06f6f483
5 changed files with 24 additions and 396 deletions

View File

@ -6,19 +6,14 @@
Environment::Environment(const Environment &other) : enclosing{other.enclosing}, values{other.values} {} Environment::Environment(const Environment &other) : enclosing{other.enclosing}, values{other.values} {}
Environment::Environment(Environment &&other) noexcept : enclosing{std::move(other.enclosing)}, values{std::move(other.values)} { Environment::Environment(Environment &&other) noexcept
other.enclosing.reset(); : enclosing{std::move(other.enclosing)}, values{std::move(other.values)} {}
}
Environment &Environment::operator=(const Environment &other) { Environment &Environment::operator=(const Environment &other) {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
if (enclosing) {
enclosing.reset();
}
enclosing = other.enclosing; enclosing = other.enclosing;
values = other.values; values = other.values;
@ -30,10 +25,6 @@ Environment &Environment::operator=(Environment &&other) noexcept {
return *this; return *this;
} }
if (enclosing) {
enclosing.reset();
}
enclosing = std::move(other.enclosing); enclosing = std::move(other.enclosing);
values = std::move(other.values); values = std::move(other.values);

View File

@ -1,94 +1,14 @@
#include "expr.hh" #include "expr.hh"
#include "../tokenizer.hh" #include "../tokenizer.hh"
#include <cassert>
#include <memory>
#include <utility> #include <utility>
Expr::Expr(ExprType type, ExprVariant value) : type{type}, value{value} {} Expr::Expr(ExprType type, ExprVariant value) : type{type}, value{value} {}
// Copy constructor // Copy constructor
Expr::Expr(const Expr &other) : type{other.type} { Expr::Expr(const Expr &other) : type{other.type}, value{other.value} {}
switch(type) {
case ExprType::BINARY: {
std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Binary>(*ptr);
break;
}
case ExprType::GROUPING: {
std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Grouping>(*ptr);
break;
}
case ExprType::LITERAL: {
std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Literal>(*ptr);
break;
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Unary>(*ptr);
break;
}
case ExprType::VARIABLE: {
std::shared_ptr<_Variable> ptr = std::get<std::shared_ptr<_Variable>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Variable>(*ptr);
break;
}
case ExprType::ASSIGN: {
std::shared_ptr<_Assign> ptr = std::get<std::shared_ptr<_Assign>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Assign>(*ptr);
break;
}
}
}
// Move constructor // Move constructor
Expr::Expr(Expr &&other) : type{other.type}, value{std::move(other.value)} { Expr::Expr(Expr &&other) noexcept : type{other.type}, value{std::move(other.value)} {}
switch(type) {
case ExprType::BINARY: {
std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::GROUPING: {
std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::LITERAL: {
std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::VARIABLE: {
std::shared_ptr<_Variable> ptr = std::get<std::shared_ptr<_Variable>>(other.value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::ASSIGN: {
std::shared_ptr<_Assign> ptr = std::get<std::shared_ptr<_Assign>>(other.value);
assert(ptr != nullptr);
ptr.reset();
break;
}
}
}
// Copy assignment // Copy assignment
Expr &Expr::operator=(const Expr &other) { Expr &Expr::operator=(const Expr &other) {
@ -96,180 +16,20 @@ Expr &Expr::operator=(const Expr &other) {
return *this; return *this;
} }
switch(type) {
case ExprType::BINARY: {
std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::GROUPING: {
std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::LITERAL: {
std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::VARIABLE: {
std::shared_ptr<_Variable> ptr = std::get<std::shared_ptr<_Variable>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::ASSIGN: {
std::shared_ptr<_Assign> ptr = std::get<std::shared_ptr<_Assign>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
}
type = other.type; type = other.type;
value = other.value;
switch(type) {
case ExprType::BINARY: {
std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Binary>(*ptr);
break;
}
case ExprType::GROUPING: {
std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Grouping>(*ptr);
break;
}
case ExprType::LITERAL: {
std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Literal>(*ptr);
break;
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Unary>(*ptr);
break;
}
case ExprType::VARIABLE: {
std::shared_ptr<_Variable> ptr = std::get<std::shared_ptr<_Variable>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Variable>(*ptr);
break;
}
case ExprType::ASSIGN: {
std::shared_ptr<_Assign> ptr = std::get<std::shared_ptr<_Assign>>(other.value);
assert(ptr != nullptr);
value = std::make_shared<_Assign>(*ptr);
break;
}
}
return *this; return *this;
} }
// Move assignment // Move assignment
Expr &Expr::operator=(Expr &&other) { Expr &Expr::operator=(Expr &&other) noexcept {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
switch(type) { type = std::move(other.type);
case ExprType::BINARY: {
std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::GROUPING: {
std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::LITERAL: {
std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::VARIABLE: {
std::shared_ptr<_Variable> ptr = std::get<std::shared_ptr<_Variable>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
case ExprType::ASSIGN: {
std::shared_ptr<_Assign> ptr = std::get<std::shared_ptr<_Assign>>(value);
assert(ptr != nullptr);
ptr.reset();
break;
}
}
type = other.type;
switch(type) {
case ExprType::BINARY: {
std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
assert(ptr != nullptr);
value = std::move(other.value); value = std::move(other.value);
ptr.reset();
break;
}
case ExprType::GROUPING: {
std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
assert(ptr != nullptr);
value = std::move(other.value);
ptr.reset();
break;
}
case ExprType::LITERAL: {
std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
assert(ptr != nullptr);
value = std::move(other.value);
ptr.reset();
break;
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
assert(ptr != nullptr);
value = std::move(other.value);
ptr.reset();
break;
}
case ExprType::VARIABLE: {
std::shared_ptr<_Variable> ptr = std::get<std::shared_ptr<_Variable>>(other.value);
assert(ptr != nullptr);
value = std::move(other.value);
ptr.reset();
break;
}
case ExprType::ASSIGN: {
std::shared_ptr<_Assign> ptr = std::get<std::shared_ptr<_Assign>>(other.value);
assert(ptr != nullptr);
value = std::move(other.value);
ptr.reset();
break;
}
}
return *this; return *this;
} }

View File

@ -45,13 +45,15 @@ struct Expr {
Expr(const Expr &other); Expr(const Expr &other);
// Move constructor // Move constructor
Expr(Expr &&other); Expr(Expr &&other) noexcept;
// Copy assignment // Copy assignment
Expr &operator=(const Expr &other); Expr &operator=(const Expr &other);
// Move assignment // Move assignment
Expr &operator=(Expr &&other); Expr &operator=(Expr &&other) noexcept;
~Expr() = default;
ExprType type; ExprType type;
ExprVariant value; ExprVariant value;

View File

@ -5,52 +5,10 @@
Stmt::Stmt(StmtType type, StmtVariant value) : type{type}, value{value} {} Stmt::Stmt(StmtType type, StmtVariant value) : type{type}, value{value} {}
// Copy constructor // Copy constructor
Stmt::Stmt(const Stmt &other) : type{other.type} { Stmt::Stmt(const Stmt &other) : type{other.type}, value{other.value} {}
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 // Move constructor
Stmt::Stmt(const Stmt &&other) : type{other.type}, value{std::move(other.value)} { Stmt::Stmt(Stmt &&other) noexcept : 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 // Copy assignment
Stmt &Stmt::operator=(const Stmt &other) { Stmt &Stmt::operator=(const Stmt &other) {
@ -58,105 +16,20 @@ Stmt &Stmt::operator=(const Stmt &other) {
return *this; 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; type = other.type;
value = other.value;
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; return *this;
} }
// Move assignment // Move assignment
Stmt &Stmt::operator=(const Stmt &&other) { Stmt &Stmt::operator=(Stmt &&other) noexcept {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
switch(type) { type = std::move(other.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); 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; return *this;
} }

View File

@ -33,13 +33,15 @@ struct Stmt {
Stmt(const Stmt &other); Stmt(const Stmt &other);
// Move constructor // Move constructor
Stmt(const Stmt &&other); Stmt(Stmt &&other) noexcept;
// Copy assignment // Copy assignment
Stmt &operator=(const Stmt &other); Stmt &operator=(const Stmt &other);
// Move assignment // Move assignment
Stmt &operator=(const Stmt &&other); Stmt &operator=(Stmt &&other) noexcept;
~Stmt() = default;
StmtType type; StmtType type;
StmtVariant value; StmtVariant value;