Skip to content

Commit

Permalink
AST: destroy the AST after compiling it
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Aug 6, 2024
1 parent c8e8bb2 commit b06d71d
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
64 changes: 64 additions & 0 deletions include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ struct BinaryExpression final : public AbstractSyntaxTree {
AbstractSyntaxTree *right{};
Token op;
BinaryExpression(AbstractSyntaxTree *left, AbstractSyntaxTree *right, Token op);
~BinaryExpression() override {
delete left;
delete right;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -63,6 +67,9 @@ struct UnaryExpression final : public AbstractSyntaxTree {
AbstractSyntaxTree *expression;
Token op;
UnaryExpression(AbstractSyntaxTree *expression, Token op, Side side);
~UnaryExpression() override {
delete expression;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -74,13 +81,21 @@ struct Declaration final : public AbstractSyntaxTree {
Declaration(AbstractSyntaxTree *type, Node identifier, AbstractSyntaxTree *value);
Declaration(AbstractSyntaxTree *type, Node identifier);
Declaration(Node identifier, AbstractSyntaxTree *value);
~Declaration() override {
if (type.has_value()) delete type.value();
if (value.has_value()) delete value.value();
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};

struct ScopedBody : public AbstractSyntaxTree {
std::vector<AbstractSyntaxTree *> body;
explicit ScopedBody(const std::vector<AbstractSyntaxTree *> &body);
~ScopedBody() override {
for (auto &node : body)
delete node;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -89,12 +104,20 @@ struct FunctionDeclaration : public AbstractSyntaxTree {
AbstractSyntaxTree *returnType;
std::vector<Declaration *> arguments;
FunctionDeclaration(AbstractSyntaxTree *returnType, const std::vector<Declaration *> &arguments);
~FunctionDeclaration() override {
delete returnType;
for (auto &arg : arguments)
delete arg;
}
bool operator==(const AbstractSyntaxTree &other) const override;
};

struct ReturnStatement : public AbstractSyntaxTree {
AbstractSyntaxTree *expression;
explicit ReturnStatement(AbstractSyntaxTree *expression);
~ReturnStatement() override {
delete expression;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -103,13 +126,21 @@ struct TypeCast : public AbstractSyntaxTree {
AbstractSyntaxTree *expression;
AbstractSyntaxTree *type;
TypeCast(AbstractSyntaxTree *expression, AbstractSyntaxTree *type);
~TypeCast() override {
delete expression;
delete type;
}
bool operator==(const AbstractSyntaxTree &other) const override;
};

struct FunctionCall : public AbstractSyntaxTree {
Node identifier;
std::vector<AbstractSyntaxTree *> arguments;
FunctionCall(Node identifier, const std::vector<AbstractSyntaxTree *> &arguments);
~FunctionCall() override {
for (auto &arg : arguments)
delete arg;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -120,6 +151,11 @@ struct IfStatement : public AbstractSyntaxTree {
std::optional<AbstractSyntaxTree *> elseBody;
IfStatement(AbstractSyntaxTree *condition, AbstractSyntaxTree *thenBody, AbstractSyntaxTree *elseBody);
IfStatement(AbstractSyntaxTree *condition, AbstractSyntaxTree *thenBody);
~IfStatement() override {
delete condition;
delete thenBody;
if (elseBody.has_value()) delete elseBody.value();
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -128,6 +164,10 @@ struct WhileStatement : public AbstractSyntaxTree {
AbstractSyntaxTree *condition;
AbstractSyntaxTree *body;
WhileStatement(AbstractSyntaxTree *condition, AbstractSyntaxTree *body);
~WhileStatement() override {
delete condition;
delete body;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -138,27 +178,43 @@ struct ForLoop : public AbstractSyntaxTree {
AbstractSyntaxTree *step;
AbstractSyntaxTree *body;
ForLoop(AbstractSyntaxTree *initialization, AbstractSyntaxTree *condition, AbstractSyntaxTree *step, AbstractSyntaxTree *body);
~ForLoop() override {
delete initialization;
delete condition;
delete step;
delete body;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};

struct List : public AbstractSyntaxTree {
std::vector<AbstractSyntaxTree *> elements;
explicit List(const std::vector<AbstractSyntaxTree *> &elements);
~List() override {
for (auto &element : elements)
delete element;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};

struct ArrayType : public AbstractSyntaxTree {
AbstractSyntaxTree *type;
explicit ArrayType(AbstractSyntaxTree *type);
~ArrayType() override {
delete type;
}
bool operator==(const AbstractSyntaxTree &other) const override;
};

struct ArrayAccess : public AbstractSyntaxTree {
Node identifier;
AbstractSyntaxTree *index;
ArrayAccess(Node identifier, AbstractSyntaxTree *index);
~ArrayAccess() override {
delete index;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -173,6 +229,9 @@ struct ImportStatement : public AbstractSyntaxTree {
struct ExportStatement : public AbstractSyntaxTree {
AbstractSyntaxTree *stm;
explicit ExportStatement(AbstractSyntaxTree *stm);
~ExportStatement() override {
delete stm;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand All @@ -182,6 +241,11 @@ struct TernaryExpression : public AbstractSyntaxTree {
AbstractSyntaxTree *thenCase;
AbstractSyntaxTree *elseCase;
TernaryExpression(AbstractSyntaxTree *condition, AbstractSyntaxTree *thenCase, AbstractSyntaxTree *elseCase);
~TernaryExpression() override {
delete condition;
delete thenCase;
delete elseCase;
}
bool operator==(const AbstractSyntaxTree &other) const override;
void compile(Program &program, Segment &segment) const override;
};
Expand Down
1 change: 1 addition & 0 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ void compile(Program &program, const char *input) {
}
for (auto &node: ast) {
node->compile(program, program.segments[0]);
delete node;
}
program.segments.front().instructions.push_back(
Instruction{
Expand Down
Loading

0 comments on commit b06d71d

Please sign in to comment.