Skip to content

Commit

Permalink
refactor(macro processor): removing the need for a double apply macro…
Browse files Browse the repository at this point in the history
… + recursive apply in MacroProcessor::processNode
  • Loading branch information
SuperFola committed Nov 23, 2024
1 parent 108c941 commit 66b5650
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
9 changes: 9 additions & 0 deletions include/Ark/Compiler/Macros/Executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ namespace Ark::internal
*/
[[nodiscard]] const Node* findNearestMacro(const std::string& name) const;

/**
* @brief Apply a macro on a given node
* @details Proxy function for MacroProcessor::applyMacro
*
* @param node
* @param depth
*/
void applyMacroProxy(Node& node, unsigned depth);

/**
* @brief Registers macros based on their type, expand conditional macros
* @details Validate macros and register them by their name
Expand Down
7 changes: 0 additions & 7 deletions include/Ark/Compiler/Macros/Processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ namespace Ark::internal
*/
void deleteNearestMacro(const std::string& name);

/**
* @brief Recursively apply macros on a given node
*
* @param node
*/
void recurApply(Node& node);

/**
* @brief Check if a given node is a list node, and starts with a Begin
*
Expand Down
5 changes: 5 additions & 0 deletions src/arkreactor/Compiler/Macros/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace Ark::internal
return m_processor->findNearestMacro(name);
}

void MacroExecutor::applyMacroProxy(Node& node, unsigned depth)
{
m_processor->applyMacro(node, depth);
}

void MacroExecutor::handleMacroNode(Node& node) const
{
m_processor->handleMacroNode(node);
Expand Down
1 change: 1 addition & 0 deletions src/arkreactor/Compiler/Macros/Executors/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace Ark::internal
unify(args_applied, temp_body, nullptr);

node.updateValueAndType(evaluate(temp_body, depth + 1, false));
applyMacroProxy(node, depth + 1);
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/arkreactor/Compiler/Macros/Executors/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Ark::internal
{
node.updateValueAndType(macro->constList()[1]);
evaluate(node, depth + 1, false);
applyMacroProxy(node, depth + 1);
return true;
}
}
Expand Down
21 changes: 7 additions & 14 deletions src/arkreactor/Compiler/Macros/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,17 @@ namespace Ark::internal
else // running on non-macros
{
applyMacro(node.list()[pos], 0);
recurApply(node.list()[pos]); // todo remove it
added_begin = isBeginNode(node.list()[pos]) && !had_begin;

if (node.list()[pos].nodeType() == NodeType::Unused)
node.list().erase(node.constList().begin() + static_cast<std::vector<Node>::difference_type>(pos));
else
// Go forward only if it isn't a macro, because we delete macros
// while running on the AST. Also, applying a macro can result in
// nodes being marked unused, and delete them immediately. When
// that happens, we can't increment i, otherwise we delete a node,
// advance, resulting in a node being skipped!
++i;

// process subnodes if any
if (node.nodeType() == NodeType::List && pos < node.constList().size())
Expand All @@ -145,10 +151,6 @@ namespace Ark::internal
// needed if we created a function node from a macro
registerFuncDef(node.list()[pos]);
}

// go forward only if it isn't a macro, because we delete macros
// while running on the AST
++i;
}

if (pos < node.constList().size())
Expand Down Expand Up @@ -584,15 +586,6 @@ namespace Ark::internal
}
}

void MacroProcessor::recurApply(Node& node)
{
if (applyMacro(node, 0) && node.isListLike())
{
for (auto& child : node.list())
recurApply(child);
}
}

bool MacroProcessor::isBeginNode(const Node& node)
{
return node.nodeType() == NodeType::List &&
Expand Down

0 comments on commit 66b5650

Please sign in to comment.