From fb06f6f483ad44826bf6743d83a6bb3c70b0d792 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 29 Jun 2025 20:24:01 +0100 Subject: [PATCH] Remove unnecessary code --- cclox_src/parser/environment.cc | 13 +- cclox_src/parser/expr.cc | 254 +------------------------------- cclox_src/parser/expr.hh | 6 +- cclox_src/parser/stmt.cc | 141 +----------------- cclox_src/parser/stmt.hh | 6 +- 5 files changed, 24 insertions(+), 396 deletions(-) diff --git a/cclox_src/parser/environment.cc b/cclox_src/parser/environment.cc index 086d274..ac19317 100644 --- a/cclox_src/parser/environment.cc +++ b/cclox_src/parser/environment.cc @@ -6,19 +6,14 @@ 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)} { - other.enclosing.reset(); -} +Environment::Environment(Environment &&other) noexcept + : enclosing{std::move(other.enclosing)}, values{std::move(other.values)} {} Environment &Environment::operator=(const Environment &other) { if (this == &other) { return *this; } - if (enclosing) { - enclosing.reset(); - } - enclosing = other.enclosing; values = other.values; @@ -30,10 +25,6 @@ Environment &Environment::operator=(Environment &&other) noexcept { return *this; } - if (enclosing) { - enclosing.reset(); - } - enclosing = std::move(other.enclosing); values = std::move(other.values); diff --git a/cclox_src/parser/expr.cc b/cclox_src/parser/expr.cc index b1f02bf..6d5e1d9 100644 --- a/cclox_src/parser/expr.cc +++ b/cclox_src/parser/expr.cc @@ -1,94 +1,14 @@ #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); - assert(ptr != nullptr); - value = std::make_shared<_Binary>(*ptr); - break; - } - case ExprType::GROUPING: { - std::shared_ptr<_Grouping> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Grouping>(*ptr); - break; - } - case ExprType::LITERAL: { - std::shared_ptr<_Literal> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Literal>(*ptr); - break; - } - case ExprType::UNARY: { - std::shared_ptr<_Unary> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Unary>(*ptr); - break; - } - case ExprType::VARIABLE: { - std::shared_ptr<_Variable> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Variable>(*ptr); - break; - } - case ExprType::ASSIGN: { - std::shared_ptr<_Assign> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Assign>(*ptr); - break; - } - } -} +Expr::Expr(const Expr &other) : type{other.type}, value{other.value} {} // Move constructor -Expr::Expr(Expr &&other) : type{other.type}, value{std::move(other.value)} { - switch(type) { - case ExprType::BINARY: { - std::shared_ptr<_Binary> ptr = std::get>(other.value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::GROUPING: { - std::shared_ptr<_Grouping> ptr = std::get>(other.value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::LITERAL: { - std::shared_ptr<_Literal> ptr = std::get>(other.value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::UNARY: { - std::shared_ptr<_Unary> ptr = std::get>(other.value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::VARIABLE: { - std::shared_ptr<_Variable> ptr = std::get>(other.value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::ASSIGN: { - std::shared_ptr<_Assign> ptr = std::get>(other.value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - } -} +Expr::Expr(Expr &&other) noexcept : type{other.type}, value{std::move(other.value)} {} // Copy assignment Expr &Expr::operator=(const Expr &other) { @@ -96,180 +16,20 @@ Expr &Expr::operator=(const Expr &other) { return *this; } - switch(type) { - case ExprType::BINARY: { - std::shared_ptr<_Binary> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::GROUPING: { - std::shared_ptr<_Grouping> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::LITERAL: { - std::shared_ptr<_Literal> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::UNARY: { - std::shared_ptr<_Unary> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::VARIABLE: { - std::shared_ptr<_Variable> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::ASSIGN: { - std::shared_ptr<_Assign> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - } - - type = other.type; - - switch(type) { - case ExprType::BINARY: { - std::shared_ptr<_Binary> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Binary>(*ptr); - break; - } - case ExprType::GROUPING: { - std::shared_ptr<_Grouping> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Grouping>(*ptr); - break; - } - case ExprType::LITERAL: { - std::shared_ptr<_Literal> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Literal>(*ptr); - break; - } - case ExprType::UNARY: { - std::shared_ptr<_Unary> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Unary>(*ptr); - break; - } - case ExprType::VARIABLE: { - std::shared_ptr<_Variable> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Variable>(*ptr); - break; - } - case ExprType::ASSIGN: { - std::shared_ptr<_Assign> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::make_shared<_Assign>(*ptr); - break; - } - } + type = other.type; + value = other.value; return *this; } // Move assignment -Expr &Expr::operator=(Expr &&other) { +Expr &Expr::operator=(Expr &&other) noexcept { if (this == &other) { return *this; } - switch(type) { - case ExprType::BINARY: { - std::shared_ptr<_Binary> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::GROUPING: { - std::shared_ptr<_Grouping> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::LITERAL: { - std::shared_ptr<_Literal> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::UNARY: { - std::shared_ptr<_Unary> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::VARIABLE: { - std::shared_ptr<_Variable> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - case ExprType::ASSIGN: { - std::shared_ptr<_Assign> ptr = std::get>(value); - assert(ptr != nullptr); - ptr.reset(); - break; - } - } - - type = other.type; - - switch(type) { - case ExprType::BINARY: { - std::shared_ptr<_Binary> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::move(other.value); - ptr.reset(); - break; - } - case ExprType::GROUPING: { - std::shared_ptr<_Grouping> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::move(other.value); - ptr.reset(); - break; - } - case ExprType::LITERAL: { - std::shared_ptr<_Literal> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::move(other.value); - ptr.reset(); - break; - } - case ExprType::UNARY: { - std::shared_ptr<_Unary> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::move(other.value); - ptr.reset(); - break; - } - case ExprType::VARIABLE: { - std::shared_ptr<_Variable> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::move(other.value); - ptr.reset(); - break; - } - case ExprType::ASSIGN: { - std::shared_ptr<_Assign> ptr = std::get>(other.value); - assert(ptr != nullptr); - value = std::move(other.value); - ptr.reset(); - break; - } - } + type = std::move(other.type); + value = std::move(other.value); return *this; } diff --git a/cclox_src/parser/expr.hh b/cclox_src/parser/expr.hh index 4ba2ddb..ee9bf25 100644 --- a/cclox_src/parser/expr.hh +++ b/cclox_src/parser/expr.hh @@ -45,13 +45,15 @@ struct Expr { Expr(const Expr &other); // Move constructor - Expr(Expr &&other); + Expr(Expr &&other) noexcept; // Copy assignment Expr &operator=(const Expr &other); // Move assignment - Expr &operator=(Expr &&other); + Expr &operator=(Expr &&other) noexcept; + + ~Expr() = default; ExprType type; ExprVariant value; diff --git a/cclox_src/parser/stmt.cc b/cclox_src/parser/stmt.cc index a9d2aba..e89cd4d 100644 --- a/cclox_src/parser/stmt.cc +++ b/cclox_src/parser/stmt.cc @@ -5,52 +5,10 @@ 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; - } - } -} +Stmt::Stmt(const Stmt &other) : type{other.type}, value{other.value} {} // 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; - } - } -} +Stmt::Stmt(Stmt &&other) noexcept : type{other.type}, value{std::move(other.value)} {} // Copy assignment Stmt &Stmt::operator=(const Stmt &other) { @@ -58,105 +16,20 @@ Stmt &Stmt::operator=(const Stmt &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; - } - } + type = other.type; + value = other.value; return *this; } // Move assignment -Stmt &Stmt::operator=(const Stmt &&other) { +Stmt &Stmt::operator=(Stmt &&other) noexcept { 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; - } - } + type = std::move(other.type); + value = std::move(other.value); return *this; } diff --git a/cclox_src/parser/stmt.hh b/cclox_src/parser/stmt.hh index 3d7fc0c..b437292 100644 --- a/cclox_src/parser/stmt.hh +++ b/cclox_src/parser/stmt.hh @@ -33,13 +33,15 @@ struct Stmt { Stmt(const Stmt &other); // Move constructor - Stmt(const Stmt &&other); + Stmt(Stmt &&other) noexcept; // Copy assignment Stmt &operator=(const Stmt &other); // Move assignment - Stmt &operator=(const Stmt &&other); + Stmt &operator=(Stmt &&other) noexcept; + + ~Stmt() = default; StmtType type; StmtVariant value;