use smart pointers
This commit is contained in:
parent
1f4228c038
commit
8d3d1e8787
2 changed files with 18 additions and 22 deletions
|
|
@ -1,8 +1,10 @@
|
|||
#include "parser.hpp"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "tokens.hpp"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
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<Expression> left;
|
||||
unique_ptr<Token> op;
|
||||
unique_ptr<Expression> right;
|
||||
|
||||
public:
|
||||
ExprType get_type() override;
|
||||
|
|
@ -30,7 +31,7 @@ public:
|
|||
|
||||
/// An expression between parentheses
|
||||
class Grouping : public Expression {
|
||||
Expression *expr;
|
||||
unique_ptr<Expression> 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<Token> op;
|
||||
unique_ptr<Expression> right;
|
||||
|
||||
public:
|
||||
ExprType get_type() override;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue