Add asserts

This commit is contained in:
Abdelrahman Said 2025-06-29 00:13:52 +01:00
parent 3aec8b6b5b
commit b3291a9e3a
2 changed files with 35 additions and 12 deletions

View File

@ -11,21 +11,25 @@ Expr::Expr(const Expr &other) : 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::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;
}
@ -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<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;
}
@ -67,21 +75,25 @@ Expr &Expr::operator=(const Expr &other) {
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;
}
@ -92,21 +104,25 @@ Expr &Expr::operator=(const Expr &other) {
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;
}
@ -124,21 +140,25 @@ Expr &Expr::operator=(const Expr &&other) {
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;
}
@ -148,27 +168,31 @@ Expr &Expr::operator=(const Expr &&other) {
switch(type) {
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);
optr.reset();
ptr.reset();
break;
}
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);
optr.reset();
ptr.reset();
break;
}
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);
optr.reset();
ptr.reset();
break;
}
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);
optr.reset();
ptr.reset();
break;
}
}

View File

@ -1,11 +1,10 @@
#ifndef PRINTER_HH
#define PRINTER_HH
#ifndef AST_PRINTER_HH
#define AST_PRINTER_HH
#include "expr.hh"
#include <string>
class AstPrinter {
public:
struct AstPrinter {
std::string print(const Expr &expr);
private:
std::string parenthesize(const std::string &name, std::vector<const Expr *> exprs);