use smart pointers

This commit is contained in:
Shautvast 2024-10-22 12:58:27 +02:00
parent 1f4228c038
commit 8d3d1e8787
2 changed files with 18 additions and 22 deletions

View file

@ -1,8 +1,10 @@
#include "parser.hpp" #include "parser.hpp"
#include <iostream>
#include <memory>
using namespace std; using namespace std;
Expression::~Expression() {} Expression::~Expression() = default;
// class Binary // class Binary
ExprType Binary::get_type() { return ExprType::Binary; } ExprType Binary::get_type() { return ExprType::Binary; }
@ -15,11 +17,7 @@ string Binary::as_string() {
Binary::Binary(Expression *_left, Token *_operator, Expression *_right) Binary::Binary(Expression *_left, Token *_operator, Expression *_right)
: left(_left), op(_operator), right(_right){}; : left(_left), op(_operator), right(_right){};
Binary::~Binary() { Binary::~Binary() = default;
delete left;
delete right;
delete op;
}
// class Grouping // class Grouping
ExprType Grouping::get_type() { return ExprType::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(Expression *_expr) : expr(_expr){};
Grouping::~Grouping() { delete expr; } Grouping::~Grouping() = default;
// class Unary // class Unary
ExprType Unary::get_type() { return ExprType::Unary; } ExprType Unary::get_type() { return ExprType::Unary; }
@ -40,10 +38,7 @@ string Unary::as_string() {
Unary::Unary(Token *_operator, Expression *_right) Unary::Unary(Token *_operator, Expression *_right)
: op(_operator), right(_right){}; : op(_operator), right(_right){};
Unary::~Unary() { Unary::~Unary() = default;
delete right;
delete op;
}
// class Literal // class Literal
string Literal::as_string() { string Literal::as_string() {
@ -114,7 +109,7 @@ Token *Parser::consume(Token::Type typ, string message) {
} }
runtime_error Parser::error(Token token, 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 return runtime_error(message); // TODO no exceptions
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "tokens.hpp" #include "tokens.hpp"
#include <memory>
#include <vector> #include <vector>
enum class ExprType { Binary, Grouping, Unary, Literal, None }; enum class ExprType { Binary, Grouping, Unary, Literal, None };
@ -10,16 +11,16 @@ using namespace std;
/// Base class for expressions /// Base class for expressions
class Expression { class Expression {
public: public:
virtual ExprType get_type() = 0; // abstract, getter for tyoe virtual ExprType get_type() = 0; // get the expression type
virtual string as_string() = 0; // abstract, string rep for debugging virtual string as_string() = 0; // get string rep for debugging
virtual ~Expression(); // destructor virtual ~Expression();
}; // namespace stdclass Expression };
/// An expression with two operands /// An expression with two operands
class Binary : public Expression { class Binary : public Expression {
Expression *left; unique_ptr<Expression> left;
Token *op; unique_ptr<Token> op;
Expression *right; unique_ptr<Expression> right;
public: public:
ExprType get_type() override; ExprType get_type() override;
@ -30,7 +31,7 @@ public:
/// An expression between parentheses /// An expression between parentheses
class Grouping : public Expression { class Grouping : public Expression {
Expression *expr; unique_ptr<Expression> expr;
public: public:
ExprType get_type() override; ExprType get_type() override;
@ -41,8 +42,8 @@ public:
/// An expression with one operand (operator is `-` or `!`) /// An expression with one operand (operator is `-` or `!`)
class Unary : public Expression { class Unary : public Expression {
Token *op; unique_ptr<Token> op;
Expression *right; unique_ptr<Expression> right;
public: public:
ExprType get_type() override; ExprType get_type() override;