Add asserts
This commit is contained in:
		@@ -11,21 +11,25 @@ Expr::Expr(const Expr &other) : type{other.type} {
 | 
				
			|||||||
  switch(type) {
 | 
					  switch(type) {
 | 
				
			||||||
    case ExprType::BINARY: {
 | 
					    case ExprType::BINARY: {
 | 
				
			||||||
      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
					      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Binary>(*ptr);
 | 
					      value = std::make_shared<_Binary>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::GROUPING: {
 | 
					    case ExprType::GROUPING: {
 | 
				
			||||||
      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
					      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Grouping>(*ptr);
 | 
					      value = std::make_shared<_Grouping>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::LITERAL: {
 | 
					    case ExprType::LITERAL: {
 | 
				
			||||||
      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
					      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Literal>(*ptr);
 | 
					      value = std::make_shared<_Literal>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::UNARY: {
 | 
					    case ExprType::UNARY: {
 | 
				
			||||||
      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
					      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Unary>(*ptr);
 | 
					      value = std::make_shared<_Unary>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -37,21 +41,25 @@ Expr::Expr(const Expr &&other) : type{other.type}, value{std::move(other.value)}
 | 
				
			|||||||
  switch(type) {
 | 
					  switch(type) {
 | 
				
			||||||
    case ExprType::BINARY: {
 | 
					    case ExprType::BINARY: {
 | 
				
			||||||
      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
					      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::GROUPING: {
 | 
					    case ExprType::GROUPING: {
 | 
				
			||||||
      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
					      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::LITERAL: {
 | 
					    case ExprType::LITERAL: {
 | 
				
			||||||
      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
					      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::UNARY: {
 | 
					    case ExprType::UNARY: {
 | 
				
			||||||
      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
					      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -67,21 +75,25 @@ Expr &Expr::operator=(const Expr &other) {
 | 
				
			|||||||
  switch(type) {
 | 
					  switch(type) {
 | 
				
			||||||
    case ExprType::BINARY: {
 | 
					    case ExprType::BINARY: {
 | 
				
			||||||
      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(value);
 | 
					      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::GROUPING: {
 | 
					    case ExprType::GROUPING: {
 | 
				
			||||||
      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(value);
 | 
					      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::LITERAL: {
 | 
					    case ExprType::LITERAL: {
 | 
				
			||||||
      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(value);
 | 
					      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::UNARY: {
 | 
					    case ExprType::UNARY: {
 | 
				
			||||||
      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(value);
 | 
					      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -92,21 +104,25 @@ Expr &Expr::operator=(const Expr &other) {
 | 
				
			|||||||
  switch(type) {
 | 
					  switch(type) {
 | 
				
			||||||
    case ExprType::BINARY: {
 | 
					    case ExprType::BINARY: {
 | 
				
			||||||
      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
					      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Binary>(*ptr);
 | 
					      value = std::make_shared<_Binary>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::GROUPING: {
 | 
					    case ExprType::GROUPING: {
 | 
				
			||||||
      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
					      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Grouping>(*ptr);
 | 
					      value = std::make_shared<_Grouping>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::LITERAL: {
 | 
					    case ExprType::LITERAL: {
 | 
				
			||||||
      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
					      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Literal>(*ptr);
 | 
					      value = std::make_shared<_Literal>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::UNARY: {
 | 
					    case ExprType::UNARY: {
 | 
				
			||||||
      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
					      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::make_shared<_Unary>(*ptr);
 | 
					      value = std::make_shared<_Unary>(*ptr);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -124,21 +140,25 @@ Expr &Expr::operator=(const Expr &&other) {
 | 
				
			|||||||
  switch(type) {
 | 
					  switch(type) {
 | 
				
			||||||
    case ExprType::BINARY: {
 | 
					    case ExprType::BINARY: {
 | 
				
			||||||
      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(value);
 | 
					      std::shared_ptr<_Binary> ptr = std::get<std::shared_ptr<_Binary>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::GROUPING: {
 | 
					    case ExprType::GROUPING: {
 | 
				
			||||||
      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(value);
 | 
					      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::LITERAL: {
 | 
					    case ExprType::LITERAL: {
 | 
				
			||||||
      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(value);
 | 
					      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::UNARY: {
 | 
					    case ExprType::UNARY: {
 | 
				
			||||||
      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(value);
 | 
					      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      ptr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -148,27 +168,31 @@ Expr &Expr::operator=(const Expr &&other) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  switch(type) {
 | 
					  switch(type) {
 | 
				
			||||||
    case ExprType::BINARY: {
 | 
					    case ExprType::BINARY: {
 | 
				
			||||||
      std::shared_ptr<_Binary> optr = std::get<std::shared_ptr<_Binary>>(other.value);
 | 
					      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);
 | 
				
			||||||
      optr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::GROUPING: {
 | 
					    case ExprType::GROUPING: {
 | 
				
			||||||
      std::shared_ptr<_Grouping> optr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
					      std::shared_ptr<_Grouping> ptr = std::get<std::shared_ptr<_Grouping>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::move(other.value);
 | 
					      value = std::move(other.value);
 | 
				
			||||||
      optr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::LITERAL: {
 | 
					    case ExprType::LITERAL: {
 | 
				
			||||||
      std::shared_ptr<_Literal> optr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
					      std::shared_ptr<_Literal> ptr = std::get<std::shared_ptr<_Literal>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::move(other.value);
 | 
					      value = std::move(other.value);
 | 
				
			||||||
      optr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    case ExprType::UNARY: {
 | 
					    case ExprType::UNARY: {
 | 
				
			||||||
      std::shared_ptr<_Unary> optr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
					      std::shared_ptr<_Unary> ptr = std::get<std::shared_ptr<_Unary>>(other.value);
 | 
				
			||||||
 | 
					      assert(ptr != nullptr);
 | 
				
			||||||
      value = std::move(other.value);
 | 
					      value = std::move(other.value);
 | 
				
			||||||
      optr.reset();
 | 
					      ptr.reset();
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,10 @@
 | 
				
			|||||||
#ifndef PRINTER_HH
 | 
					#ifndef AST_PRINTER_HH
 | 
				
			||||||
#define PRINTER_HH
 | 
					#define AST_PRINTER_HH
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "expr.hh"
 | 
					#include "expr.hh"
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AstPrinter {
 | 
					struct AstPrinter {
 | 
				
			||||||
public:
 | 
					 | 
				
			||||||
  std::string print(const Expr &expr);
 | 
					  std::string print(const Expr &expr);
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
  std::string parenthesize(const std::string &name, std::vector<const Expr *> exprs);
 | 
					  std::string parenthesize(const std::string &name, std::vector<const Expr *> exprs);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user