diff --git a/cclox_src/parser/expr.cc b/cclox_src/parser/expr.cc index f7a6482..057166d 100644 --- a/cclox_src/parser/expr.cc +++ b/cclox_src/parser/expr.cc @@ -11,21 +11,25 @@ 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; } @@ -37,21 +41,25 @@ 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); + 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; } @@ -67,21 +75,25 @@ Expr &Expr::operator=(const Expr &other) { 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; } @@ -92,21 +104,25 @@ Expr &Expr::operator=(const Expr &other) { 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; } @@ -124,21 +140,25 @@ Expr &Expr::operator=(const Expr &&other) { 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; } @@ -148,27 +168,31 @@ Expr &Expr::operator=(const Expr &&other) { switch(type) { case ExprType::BINARY: { - std::shared_ptr<_Binary> optr = std::get>(other.value); + std::shared_ptr<_Binary> ptr = std::get>(other.value); + assert(ptr != nullptr); value = std::move(other.value); - optr.reset(); + ptr.reset(); break; } case ExprType::GROUPING: { - std::shared_ptr<_Grouping> optr = std::get>(other.value); + std::shared_ptr<_Grouping> ptr = std::get>(other.value); + assert(ptr != nullptr); value = std::move(other.value); - optr.reset(); + ptr.reset(); break; } case ExprType::LITERAL: { - std::shared_ptr<_Literal> optr = std::get>(other.value); + std::shared_ptr<_Literal> ptr = std::get>(other.value); + assert(ptr != nullptr); value = std::move(other.value); - optr.reset(); + ptr.reset(); break; } case ExprType::UNARY: { - std::shared_ptr<_Unary> optr = std::get>(other.value); + std::shared_ptr<_Unary> ptr = std::get>(other.value); + assert(ptr != nullptr); value = std::move(other.value); - optr.reset(); + ptr.reset(); break; } } diff --git a/cclox_src/parser/printer.hh b/cclox_src/parser/printer.hh index fecd3e2..ec66745 100644 --- a/cclox_src/parser/printer.hh +++ b/cclox_src/parser/printer.hh @@ -1,11 +1,10 @@ -#ifndef PRINTER_HH -#define PRINTER_HH +#ifndef AST_PRINTER_HH +#define AST_PRINTER_HH #include "expr.hh" #include -class AstPrinter { -public: +struct AstPrinter { std::string print(const Expr &expr); private: std::string parenthesize(const std::string &name, std::vector exprs);