![]() |
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, uint16_t options) noexcept | |
Construct a new Macro Processor object. | |
void | feed (const Node &ast) |
Send the complete AST (after the inclusions and stuff), and work on it. | |
const Node & | ast () const noexcept |
Return the modified AST. | |
Private Member Functions | |
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 | isPredefined (const std::string &symbol) |
Check if a given symbol is a predefined macro or not. | |
void | recurApply (Node &node) |
Recursively apply macros on a given node. | |
bool | hadBegin (const Node &node) |
Check if a given node is a list node, and starts with a Begin. | |
void | removeBegin (Node &node, std::size_t &i) |
Remove a begin block added by a macro. | |
bool | isConstEval (const Node &node) const |
Check if a node can be evaluated at compile time. | |
void | registerMacro (Node &node) |
Registers macros based on their type. | |
void | registerFuncDef (Node &node) |
Registers a function definition node. | |
void | process (Node &node, unsigned depth) |
Register macros in scopes and apply them as needed. | |
bool | applyMacro (Node &node) |
Apply a macro on a given node. | |
void | unify (const std::unordered_map< std::string, Node > &map, Node &target, Node *parent, std::size_t index=0) |
Unify a target node with a given map symbol => replacement node, recursively. | |
Node | evaluate (Node &node, bool is_not_body=false) |
Evaluate only the macros. | |
bool | isTruthy (const Node &node) |
Check if a node can be evaluated to true. | |
void | throwMacroProcessingError (const std::string &message, const Node &node) |
Throw a macro processing error. | |
Private Attributes | |
unsigned | m_debug |
The debug level. | |
uint16_t | m_options |
Node | m_ast |
The modified AST. | |
std::vector< MacroScope > | m_macros |
Handling macros in a scope fashion. | |
MacroExecutorPipeline | m_executor_pipeline |
std::vector< std::string > | m_predefined_macros |
Already existing macros, non-keywords, non-builtins. | |
std::unordered_map< std::string, Node > | m_defined_functions |
Friends | |
class | MacroExecutor |
The class handling the macros definitions and calls, given an AST.
Definition at line 30 of file Processor.hpp.
|
noexcept |
Construct a new Macro Processor object.
debug | the debug level |
options | the options flags |
Definition at line 15 of file Processor.cpp.
|
private |
Apply a macro on a given node.
node |
Definition at line 213 of file Processor.cpp.
References Ark::internal::MacroExecutorPipeline::applyMacro(), and m_executor_pipeline.
Referenced by Ark::internal::MacroExecutor::applyMacroProxy(), evaluate(), process(), recurApply(), and registerMacro().
|
noexcept |
Return the modified AST.
Definition at line 46 of file Processor.cpp.
References m_ast.
Referenced by feed(), Ark::Compiler::feed(), and Ark::JsonCompiler::feed().
|
private |
Find the nearest macro matching a given name and delete it.
name |
Definition at line 516 of file Processor.cpp.
References m_macros.
Referenced by registerMacro().
Evaluate only the macros.
node | |
is_not_body | true if the method is run on a non-body code (eg a condition of an if-macro) |
Definition at line 245 of file Processor.cpp.
References applyMacro(), Ark::internal::Node::constList(), evaluate(), findNearestMacro(), Ark::internal::Fun, GEN_COMPARATOR, GEN_OP, Ark::internal::Node::getFalseNode(), Ark::internal::Node::getListNode(), Ark::internal::Node::getNilNode(), Ark::internal::Node::getTrueNode(), isConstEval(), isTruthy(), Ark::internal::Keyword, Ark::internal::Node::list(), Ark::internal::List, m_defined_functions, Ark::internal::Node::nodeType(), Ark::internal::nodeTypes, Ark::internal::Node::number(), Ark::internal::Number, Ark::internal::Node::push_back(), Ark::internal::Node::setNodeType(), Ark::internal::Node::setString(), Ark::internal::Node::string(), Ark::internal::String, Ark::internal::Symbol, throwMacroProcessingError(), Ark::internal::typeToString(), and Ark::internal::Unused.
Referenced by Ark::internal::MacroExecutor::evaluate(), and evaluate().
void Ark::internal::MacroProcessor::feed | ( | const Node & | ast | ) |
Send the complete AST (after the inclusions and stuff), and work on it.
ast |
Definition at line 30 of file Processor.cpp.
References ast(), m_ast, m_debug, and process().
Referenced by Ark::Compiler::feed(), and Ark::JsonCompiler::feed().
|
private |
Find the nearest macro matching a given name.
name |
Definition at line 503 of file Processor.cpp.
References m_macros.
Referenced by evaluate(), Ark::internal::MacroExecutor::findNearestMacro(), and isConstEval().
|
private |
Check if a given node is a list node, and starts with a Begin.
node |
Definition at line 552 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 process().
|
private |
Check if a node can be evaluated at compile time.
node |
Definition at line 580 of file Processor.cpp.
References Ark::internal::Builtins::builtins, Ark::internal::Capture, Ark::internal::Closure, Ark::internal::Node::constList(), findNearestMacro(), Ark::internal::GetField, Ark::internal::Keyword, Ark::internal::List, Ark::internal::Macro, Ark::internal::Node::nodeType(), Ark::internal::Number, Ark::internal::operators, Ark::internal::Spread, Ark::internal::Node::string(), Ark::internal::String, Ark::internal::Symbol, and Ark::internal::Unused.
Referenced by evaluate().
|
private |
Check if a given symbol is a predefined macro or not.
symbol |
Definition at line 531 of file Processor.cpp.
References m_predefined_macros.
Referenced by Ark::internal::MacroExecutor::isPredefined().
|
private |
Check if a node can be evaluated to true.
node |
Definition at line 485 of file Processor.cpp.
References Ark::internal::Node::nodeType(), Ark::internal::Node::number(), Ark::internal::Number, Ark::internal::Spread, Ark::internal::Node::string(), Ark::internal::String, Ark::internal::Symbol, and throwMacroProcessingError().
Referenced by evaluate(), and Ark::internal::MacroExecutor::isTruthy().
|
private |
Register macros in scopes and apply them as needed.
node | node on which to operate |
depth |
Definition at line 133 of file Processor.cpp.
References applyMacro(), Ark::internal::Node::constList(), hadBegin(), Ark::internal::Node::list(), Ark::internal::List, m_macros, Ark::internal::Macro, Ark::internal::Node::nodeType(), process(), recurApply(), registerFuncDef(), registerMacro(), removeBegin(), and Ark::internal::Unused.
|
private |
Recursively apply macros on a given node.
node |
Definition at line 541 of file Processor.cpp.
References applyMacro(), Ark::internal::Node::list(), Ark::internal::List, Ark::internal::Node::nodeType(), and recurApply().
Referenced by process(), and recurApply().
|
private |
Registers a function definition node.
node |
Definition at line 115 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(), and Ark::internal::Set.
Referenced by process().
|
private |
Registers macros based on their type.
Validate macros and register them by their name
node | A node of type Macro |
Definition at line 51 of file Processor.cpp.
References applyMacro(), Ark::internal::Node::constList(), deleteNearestMacro(), Ark::internal::If, Ark::internal::Node::keyword(), Ark::internal::Keyword, Ark::internal::Node::list(), Ark::internal::List, m_macros, Ark::internal::Node::nodeType(), Ark::internal::Spread, Ark::internal::Node::string(), Ark::internal::Symbol, and throwMacroProcessingError().
Referenced by process(), and Ark::internal::MacroExecutor::registerMacro().
|
private |
Remove a begin block added by a macro.
node | |
i |
Definition at line 560 of file Processor.cpp.
References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Node::keyword(), Ark::internal::Keyword, Ark::internal::Node::list(), Ark::internal::List, and Ark::internal::Node::nodeType().
Referenced by process().
|
private |
Throw a macro processing error.
message | the error |
node | the node in which there is an error |
Definition at line 621 of file Processor.cpp.
References Ark::internal::makeNodeBasedErrorCtx().
Referenced by evaluate(), isTruthy(), registerMacro(), Ark::internal::MacroExecutor::throwMacroProcessingError(), and unify().
|
private |
Unify a target node with a given map symbol => replacement node, recursively.
map | |
target | |
parent | |
index | position of target inside parent->list() |
Definition at line 218 of file Processor.cpp.
References Ark::internal::Node::list(), Ark::internal::List, Ark::internal::Macro, Ark::internal::Node::nodeType(), Ark::internal::Node::setNodeType(), Ark::internal::Spread, Ark::internal::Node::string(), Ark::internal::Symbol, throwMacroProcessingError(), and unify().
Referenced by Ark::internal::ConditionalExecutor::MacroExecutor(), and unify().
|
friend |
Definition at line 55 of file Processor.hpp.
|
private |
|
private |
|
private |
Definition at line 64 of file Processor.hpp.
Referenced by evaluate(), and registerFuncDef().
|
private |
Definition at line 62 of file Processor.hpp.
Referenced by applyMacro().
|
private |
Handling macros in a scope fashion.
Definition at line 61 of file Processor.hpp.
Referenced by deleteNearestMacro(), findNearestMacro(), process(), and registerMacro().
|
private |
Definition at line 59 of file Processor.hpp.
|
private |
Already existing macros, non-keywords, non-builtins.
Definition at line 63 of file Processor.hpp.
Referenced by isPredefined().