12#ifndef COMPILER_MACROS_PROCESSOR_HPP
13#define COMPILER_MACROS_PROCESSOR_HPP
19#include <unordered_map>
53 [[nodiscard]]
const Node&
ast()
const noexcept override;
135 void processNode(
Node& node,
unsigned depth,
bool is_processing_namespace =
false);
155 static void checkMacroArgCountEq(
const Node& node, std::size_t expected,
const std::string& name,
const std::string& kind =
"");
165 static void checkMacroArgCountGe(
const Node& node, std::size_t expected,
const std::string& name,
const std::string& kind =
"");
Defines tools to handle macro definitions.
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass (take in an AST, output an AST)
A class that applies macros in a Node.
The class handling the macros definitions and calls, given an AST.
Node evaluate(Node &node, unsigned depth, bool is_not_body=false)
Evaluate only the macros.
void registerFuncDef(const Node &node)
Registers a function definition node.
const Node & ast() const noexcept override
Return the modified AST.
std::shared_ptr< MacroExecutor > m_conditional_executor
bool applyMacro(Node &node, unsigned depth)
Apply a macro on a given node.
static bool isBeginNode(const Node &node)
Check if a given node is a list node, and starts with a Begin.
static void removeBegin(Node &node, std::size_t i)
Remove a begin block added by a macro.
std::optional< Node > lookupDefinedFunction(const std::string &name) const
Return std::nullopt if the function isn't registered, otherwise return its node.
const Node * findNearestMacro(const std::string &name) const
Find the nearest macro matching a given name.
Node m_ast
The modified AST.
static bool isTruthy(const Node &node)
Check if a node can be evaluated to true.
static void throwMacroProcessingError(const std::string &message, const Node &node)
Throw a macro processing error.
std::unordered_map< std::string, Node > m_defined_functions
bool isConstEval(const Node &node) const
Check if a node can be evaluated at compile time.
std::vector< std::shared_ptr< MacroExecutor > > m_executors
void process(const Node &ast) override
Send the complete AST and work on it.
MacroProcessor(unsigned debug) noexcept
Construct a new Macro Processor object.
void processNode(Node &node, unsigned depth, bool is_processing_namespace=false)
Register macros in scopes and apply them as needed.
std::vector< MacroScope > m_macros
Handling macros in a scope fashion.
static void checkMacroArgCountGe(const Node &node, std::size_t expected, const std::string &name, const std::string &kind="")
Check if the given node has at least the provided argument count, otherwise throws an error.
static void checkMacroArgCountEq(const Node &node, std::size_t expected, const std::string &name, const std::string &kind="")
Check if the given node has exactly the provided argument count, otherwise throws an error.
void handleMacroNode(Node &node)
Registers macros based on their type, expand conditional macros.
void deleteNearestMacro(const std::string &name)
Find the nearest macro matching a given name and delete it.
A node of an Abstract Syntax Tree for ArkScript.
An interface to describe compiler passes.