![]() |
ArkScript
A small, fast, functional and scripting language for video games
|
The class handling the macros definitions and calls, given an AST. More...
#include <Processor.hpp>
Public Member Functions | |
MacroProcessor (unsigned debug) noexcept | |
Construct a new Macro Processor object. | |
void | process (const Node &ast) override |
Send the complete AST and work on it. | |
const Node & | ast () const noexcept override |
Return the modified AST. | |
![]() | |
Pass (std::string name, unsigned debug_level) | |
Construct a new Pass object. | |
virtual | ~Pass ()=default |
Private Member Functions | |
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. | |
void | deleteNearestMacro (const std::string &name) |
Find the nearest macro matching a given name and delete it. | |
bool | isConstEval (const Node &node) const |
Check if a node can be evaluated at compile time. | |
void | handleMacroNode (Node &node) |
Registers macros based on their type, expand conditional macros. | |
void | registerFuncDef (const Node &node) |
Registers a function definition node. | |
void | processNode (Node &node, unsigned depth, bool is_processing_namespace=false) |
Register macros in scopes and apply them as needed. | |
bool | applyMacro (Node &node, unsigned depth) |
Apply a macro on a given node. | |
Node | evaluate (Node &node, unsigned depth, bool is_not_body=false) |
Evaluate only the macros. | |
Static Private Member Functions | |
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. | |
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. | |
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 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. | |
Private Attributes | |
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 |
Friends | |
class | MacroExecutor |
Additional Inherited Members | |
![]() | |
Logger | m_logger |
The class handling the macros definitions and calls, given an AST.
Definition at line 31 of file Processor.hpp.
|
explicitnoexcept |
Construct a new Macro Processor object.
debug | the debug level |
Definition at line 20 of file Processor.cpp.
|
private |
Apply a macro on a given node.
node | |
depth |
Definition at line 187 of file Processor.cpp.
References m_executors, Ark::MaxMacroProcessingDepth, and throwMacroProcessingError().
Referenced by Ark::internal::MacroExecutor::applyMacroProxy(), evaluate(), handleMacroNode(), and processNode().
|
nodiscardoverridevirtualnoexcept |
Return the modified AST.
Implements Ark::internal::Pass.
Definition at line 45 of file Processor.cpp.
References m_ast.
Referenced by Ark::Welder::computeAST(), evaluate(), and process().
|
staticprivate |
Check if the given node has exactly the provided argument count, otherwise throws an error.
node | a list node with a macro application, eg (= a b) |
expected | expected argument count, not counting the macro |
name | the name of the macro being applied |
kind | the macro kind, empty by default (eg "operator", "condition") |
Definition at line 207 of file Processor.cpp.
References Ark::internal::Node::constList(), and throwMacroProcessingError().
Referenced by evaluate().
|
staticprivate |
Check if the given node has at least the provided argument count, otherwise throws an error.
node | a list node with a macro application, eg (= a b) |
expected | expected argument count, not counting the macro |
name | the name of the macro being applied |
kind | the macro kind, empty by default (eg "operator", "condition") |
Definition at line 221 of file Processor.cpp.
References Ark::internal::Node::constList(), and throwMacroProcessingError().
Referenced by evaluate().
|
private |
Find the nearest macro matching a given name and delete it.
name |
Definition at line 646 of file Processor.cpp.
References m_macros.
Referenced by evaluate().
|
private |
Evaluate only the macros.
node | |
depth | |
is_not_body | true if the method is run on a non-body code (eg a condition of an if-macro) |
Definition at line 235 of file Processor.cpp.
References Ark::internal::Language::And, applyMacro(), Ark::internal::Language::Argcount, ast(), checkMacroArgCountEq(), checkMacroArgCountGe(), Ark::internal::Node::col(), Ark::internal::Node::constList(), deleteNearestMacro(), evaluate(), Ark::internal::Node::filename(), findNearestMacro(), Ark::internal::Fun, Ark::internal::getFalseNode(), Ark::internal::getListNode(), Ark::internal::getNilNode(), Ark::internal::getTrueNode(), isConstEval(), isTruthy(), Ark::internal::Keyword, Ark::internal::Node::line(), Ark::internal::List, Ark::internal::Node::list(), lookupDefinedFunction(), Ark::internal::Node::nodeType(), Ark::internal::Number, Ark::internal::Node::number(), Ark::internal::Language::Or, Ark::internal::Language::Paste, Ark::internal::Node::push_back(), Ark::internal::Language::Repr, Ark::internal::Node::repr(), Ark::internal::Node::setFilename(), Ark::internal::Node::setNodeType(), Ark::internal::Node::setPos(), Ark::internal::Node::setString(), Ark::internal::Spread, Ark::internal::String, Ark::internal::Node::string(), Ark::internal::Symbol, Ark::internal::Language::Symcat, throwMacroProcessingError(), Ark::internal::typeToString(), Ark::internal::Language::Undef, Ark::internal::Unused, and Ark::internal::Node::updateValueAndType().
Referenced by Ark::internal::MacroExecutor::evaluate(), evaluate(), and handleMacroNode().
|
nodiscardprivate |
Find the nearest macro matching a given name.
name |
Definition at line 633 of file Processor.cpp.
References m_macros.
Referenced by evaluate(), Ark::internal::MacroExecutor::findNearestMacro(), and isConstEval().
|
private |
Registers macros based on their type, expand conditional macros.
Validate macros and register them by their name
node | A node of type Macro |
Definition at line 50 of file Processor.cpp.
References applyMacro(), Ark::internal::Node::constList(), evaluate(), Ark::internal::List, Ark::internal::Node::list(), m_conditional_executor, m_macros, Ark::internal::Node::nodeType(), Ark::internal::Node::string(), and Ark::internal::Symbol.
Referenced by Ark::internal::MacroExecutor::handleMacroNode(), and processNode().
|
staticprivate |
Check if a given node is a list node, and starts with a Begin.
node |
Definition at line 661 of file Processor.cpp.
References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Keyword, Ark::internal::List, and Ark::internal::Node::nodeType().
Referenced by processNode().
|
nodiscardprivate |
Check if a node can be evaluated at compile time.
node |
Definition at line 690 of file Processor.cpp.
References Ark::internal::Builtins::builtins, Ark::internal::Capture, Ark::internal::Node::constList(), Ark::internal::Field, findNearestMacro(), Ark::internal::Keyword, Ark::internal::List, Ark::internal::Macro, Ark::internal::Namespace, Ark::internal::Node::nodeType(), Ark::internal::Number, Ark::internal::Language::operators, Ark::internal::Spread, Ark::internal::String, Ark::internal::Node::string(), Ark::internal::Symbol, and Ark::internal::Unused.
Referenced by evaluate().
|
staticprivate |
Check if a node can be evaluated to true.
node |
Definition at line 610 of file Processor.cpp.
References Ark::internal::Node::nodeType(), Ark::internal::Number, Ark::internal::Node::number(), Ark::internal::Spread, Ark::internal::String, Ark::internal::Node::string(), Ark::internal::Symbol, and throwMacroProcessingError().
Referenced by evaluate(), and Ark::internal::MacroExecutor::isTruthy().
|
nodiscardprivate |
Return std::nullopt if the function isn't registered, otherwise return its node.
name | function name |
Definition at line 626 of file Processor.cpp.
References m_defined_functions.
Referenced by evaluate().
|
overridevirtual |
Send the complete AST and work on it.
ast |
Implements Ark::internal::Pass.
Definition at line 30 of file Processor.cpp.
References ast(), Ark::internal::Logger::debug(), Ark::internal::Node::debugPrint(), m_ast, Ark::internal::Pass::m_logger, processNode(), Ark::internal::Logger::shouldTrace(), Ark::internal::Logger::trace(), Ark::internal::Logger::traceEnd(), and Ark::internal::Logger::traceStart().
Referenced by Ark::Welder::computeAST().
|
private |
Register macros in scopes and apply them as needed.
node | node on which to operate |
depth | |
is_processing_namespace |
Definition at line 102 of file Processor.cpp.
References applyMacro(), Ark::internal::Node::arkNamespace(), Ark::internal::Namespace::ast, Ark::internal::Node::constList(), handleMacroNode(), isBeginNode(), Ark::internal::List, Ark::internal::Node::list(), m_macros, Ark::internal::Macro, Ark::MaxMacroProcessingDepth, Ark::internal::Namespace, Ark::internal::Node::nodeType(), processNode(), registerFuncDef(), removeBegin(), throwMacroProcessingError(), and Ark::internal::Unused.
Referenced by process(), and processNode().
|
private |
Registers a function definition node.
node |
Definition at line 78 of file Processor.cpp.
References Ark::internal::Node::constList(), Ark::internal::Fun, Ark::internal::Keyword, Ark::internal::Let, Ark::internal::List, m_defined_functions, Ark::internal::Mut, Ark::internal::Node::nodeType(), Ark::internal::Set, Ark::internal::Node::string(), Ark::internal::Symbol, throwMacroProcessingError(), and Ark::internal::typeToString().
Referenced by processNode().
|
staticprivate |
Remove a begin block added by a macro.
node | |
i |
Definition at line 669 of file Processor.cpp.
References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Node::isListLike(), Ark::internal::Keyword, Ark::internal::Node::keyword(), Ark::internal::List, Ark::internal::Node::list(), and Ark::internal::Node::nodeType().
Referenced by processNode().
|
staticprivate |
Throw a macro processing error.
message | the error |
node | the node in which there is an error |
Definition at line 731 of file Processor.cpp.
References Ark::internal::Node::col(), Ark::internal::Node::filename(), Ark::internal::Node::line(), and Ark::internal::Node::repr().
Referenced by applyMacro(), checkMacroArgCountEq(), checkMacroArgCountGe(), evaluate(), isTruthy(), Ark::internal::ConditionalExecutor::MacroExecutor(), processNode(), and registerFuncDef().
|
friend |
Definition at line 55 of file Processor.hpp.
|
private |
|
private |
Definition at line 60 of file Processor.hpp.
Referenced by handleMacroNode().
|
private |
Definition at line 62 of file Processor.hpp.
Referenced by lookupDefinedFunction(), and registerFuncDef().
|
private |
Definition at line 61 of file Processor.hpp.
Referenced by applyMacro().
|
private |
Handling macros in a scope fashion.
Definition at line 59 of file Processor.hpp.
Referenced by deleteNearestMacro(), findNearestMacro(), handleMacroNode(), and processNode().