Skip to content

Commit

Permalink
refactor(macro processor): renamed registerMacro to handleMacroNode, …
Browse files Browse the repository at this point in the history
…since it also has to handle conditional macros for consistency
  • Loading branch information
SuperFola committed Nov 23, 2024
1 parent 985b111 commit 108c941
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions include/Ark/Compiler/Macros/Executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ namespace Ark::internal
[[nodiscard]] const Node* findNearestMacro(const std::string& name) const;

/**
* @brief Registers macros based on their type
* @brief Registers macros based on their type, expand conditional macros
* @details Validate macros and register them by their name
* Proxy function for MacroProcessor::registerMacro
* Proxy function for MacroProcessor::handleMacroNode
*
* @param node A node of type Macro
*/
void registerMacro(Node& node) const;
void handleMacroNode(Node& node) const;

/**
* @brief Check if a node can be evaluated to true
Expand Down
5 changes: 3 additions & 2 deletions include/Ark/Compiler/Macros/Processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Ark::internal
private:
Node m_ast; ///< The modified AST
std::vector<MacroScope> m_macros; ///< Handling macros in a scope fashion
std::shared_ptr<MacroExecutor> m_conditional_executor;
std::vector<std::shared_ptr<MacroExecutor>> m_executors;
std::unordered_map<std::string, Node> m_defined_functions;

Expand Down Expand Up @@ -117,12 +118,12 @@ namespace Ark::internal
[[nodiscard]] bool isConstEval(const Node& node) const;

/**
* @brief Registers macros based on their type
* @brief Registers macros based on their type, expand conditional macros
* @details Validate macros and register them by their name
*
* @param node A node of type Macro
*/
void registerMacro(Node& node);
void handleMacroNode(Node& node);

/**
* @brief Registers a function definition node
Expand Down
4 changes: 2 additions & 2 deletions src/arkreactor/Compiler/Macros/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace Ark::internal
return m_processor->findNearestMacro(name);
}

void MacroExecutor::registerMacro(Node& node) const
void MacroExecutor::handleMacroNode(Node& node) const
{
m_processor->registerMacro(node);
m_processor->handleMacroNode(node);
}

bool MacroExecutor::isTruthy(const Node& node) const
Expand Down
2 changes: 1 addition & 1 deletion src/arkreactor/Compiler/Macros/Executors/Conditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Ark::internal
}

if (node.nodeType() == NodeType::Macro)
registerMacro(node);
handleMacroNode(node);

return true;
}
Expand Down
14 changes: 7 additions & 7 deletions src/arkreactor/Compiler/Macros/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ namespace Ark::internal
Pass("MacroProcessor", debug)
{
// create executors pipeline
m_conditional_executor = std::make_shared<ConditionalExecutor>(this);
m_executors.emplace_back(std::make_shared<SymbolExecutor>(this));
m_executors.emplace_back(std::make_shared<ConditionalExecutor>(this));
m_executors.emplace_back(m_conditional_executor);
m_executors.emplace_back(std::make_shared<FunctionExecutor>(this));
}

Expand All @@ -46,7 +47,7 @@ namespace Ark::internal
return m_ast;
}

void MacroProcessor::registerMacro(Node& node)
void MacroProcessor::handleMacroNode(Node& node)
{
// a macro needs at least 2 nodes, name + value is the minimal form
// this is guaranted by the parser
Expand All @@ -66,6 +67,9 @@ namespace Ark::internal
assert(node.list()[1].nodeType() == NodeType::List && "Invalid macro argument's list");
m_macros.back().add(first_node.string(), node);
}
// in case we had a conditional, we need to evaluate and expand it
else if (m_conditional_executor->canHandle(node))
m_conditional_executor->applyMacro(node, 0);
}

// todo find a better way to do this
Expand Down Expand Up @@ -122,17 +126,13 @@ namespace Ark::internal
m_macros.emplace_back(depth);
}

registerMacro(node.list()[pos]);
// in case we had a conditional, we need to evaluate it
applyMacro(node.list()[pos], 0);

handleMacroNode(node.list()[pos]);
added_begin = isBeginNode(node.list()[pos]) && !had_begin;
}
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)
Expand Down

0 comments on commit 108c941

Please sign in to comment.