Skip to content
Snippets Groups Projects
Commit 9fac6605 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add ASTNodeDeclarationCleaner

This removes declaration instructions. These instructions have been used to
build symbol tables are then useless in the AST.

Other instructions could probably be removed but it is future work and probably
not crucial optimization.
parent 2605cd62
No related branches found
No related tags found
1 merge request!37Feature/language
#include <ASTNodeDeclarationCleaner.hpp>
#include <PEGGrammar.hpp>
#include <PugsAssert.hpp>
#include <stack>
void
ASTNodeDeclarationCleaner::_removeDeclarationNode(ASTNode& n)
{
std::stack<size_t> declaration_ids;
for (size_t i_child = 0; i_child < n.children.size(); ++i_child) {
if (n.children[i_child]->is<language::declaration>()) {
declaration_ids.push(i_child);
}
}
while (declaration_ids.size() > 0) {
size_t i_removed = declaration_ids.top();
declaration_ids.pop();
for (size_t i = i_removed; i + 1 < n.children.size(); ++i) {
n.children[i] = std::move(n.children[i + 1]);
}
n.children.pop_back();
}
for (auto& child : n.children) {
this->_removeDeclarationNode(*child);
}
}
ASTNodeDeclarationCleaner::ASTNodeDeclarationCleaner(ASTNode& n)
{
Assert(n.is_root());
this->_removeDeclarationNode(n);
}
#ifndef AST_NODE_DECLARATION_CLEANER_HPP
#define AST_NODE_DECLARATION_CLEANER_HPP
#include <ASTNode.hpp>
class ASTNodeDeclarationCleaner
{
private:
void _removeDeclarationNode(ASTNode& node);
public:
ASTNodeDeclarationCleaner(ASTNode& root_node);
};
#endif // AST_NODE_DECLARATION_CLEANER_HPP
......@@ -13,6 +13,7 @@ add_library(
ASTNodeDataTypeBuilder.cpp
ASTNodeDataTypeChecker.cpp
ASTNodeDeclarationToAffectationConverter.cpp
ASTNodeDeclarationCleaner.cpp
ASTNodeExpressionBuilder.cpp
ASTNodeIncDecExpressionBuilder.cpp
ASTNodeJumpPlacementChecker.cpp
......
......@@ -29,6 +29,7 @@
#include <ASTSymbolInitializationChecker.hpp>
#include <ASTSymbolTableBuilder.hpp>
#include <ASTNodeDeclarationCleaner.hpp>
#include <ASTNodeDeclarationToAffectationConverter.hpp>
#include <ASTDotPrinter.hpp>
......@@ -72,6 +73,7 @@ parser(const std::string& filename)
// optimizations
ASTNodeDeclarationToAffectationConverter{*root_node};
ASTNodeDeclarationCleaner{*root_node};
language::build_node_type(*root_node);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment