From 8d3d1e878775e1a62c1d75fd8979bcaac9c6c1e1 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Tue, 22 Oct 2024 12:58:27 +0200 Subject: [PATCH] use smart pointers --- src/parser.cpp | 19 +++++++------------ src/parser.hpp | 21 +++++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index f41072e..4217d48 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1,8 +1,10 @@ #include "parser.hpp" +#include +#include using namespace std; -Expression::~Expression() {} +Expression::~Expression() = default; // class Binary ExprType Binary::get_type() { return ExprType::Binary; } @@ -15,11 +17,7 @@ string Binary::as_string() { Binary::Binary(Expression *_left, Token *_operator, Expression *_right) : left(_left), op(_operator), right(_right){}; -Binary::~Binary() { - delete left; - delete right; - delete op; -} +Binary::~Binary() = default; // class Grouping ExprType Grouping::get_type() { return ExprType::Grouping; } @@ -28,7 +26,7 @@ string Grouping::as_string() { return "(" + expr->as_string() + ")"; } Grouping::Grouping(Expression *_expr) : expr(_expr){}; -Grouping::~Grouping() { delete expr; } +Grouping::~Grouping() = default; // class Unary ExprType Unary::get_type() { return ExprType::Unary; } @@ -40,10 +38,7 @@ string Unary::as_string() { Unary::Unary(Token *_operator, Expression *_right) : op(_operator), right(_right){}; -Unary::~Unary() { - delete right; - delete op; -} +Unary::~Unary() = default; // class Literal string Literal::as_string() { @@ -114,7 +109,7 @@ Token *Parser::consume(Token::Type typ, string message) { } runtime_error Parser::error(Token token, string message) { - cout << token.as_string() << " " << message; + std::cout << token.as_string() << " " << message; return runtime_error(message); // TODO no exceptions } diff --git a/src/parser.hpp b/src/parser.hpp index 3fe8ec2..2bd4fd2 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -1,6 +1,7 @@ #pragma once #include "tokens.hpp" +#include #include enum class ExprType { Binary, Grouping, Unary, Literal, None }; @@ -10,16 +11,16 @@ using namespace std; /// Base class for expressions class Expression { public: - virtual ExprType get_type() = 0; // abstract, getter for tyoe - virtual string as_string() = 0; // abstract, string rep for debugging - virtual ~Expression(); // destructor -}; // namespace stdclass Expression + virtual ExprType get_type() = 0; // get the expression type + virtual string as_string() = 0; // get string rep for debugging + virtual ~Expression(); +}; /// An expression with two operands class Binary : public Expression { - Expression *left; - Token *op; - Expression *right; + unique_ptr left; + unique_ptr op; + unique_ptr right; public: ExprType get_type() override; @@ -30,7 +31,7 @@ public: /// An expression between parentheses class Grouping : public Expression { - Expression *expr; + unique_ptr expr; public: ExprType get_type() override; @@ -41,8 +42,8 @@ public: /// An expression with one operand (operator is `-` or `!`) class Unary : public Expression { - Token *op; - Expression *right; + unique_ptr op; + unique_ptr right; public: ExprType get_type() override;