ArkScript
A small, fast, functional and scripting language for video games
Ark::internal::MacroProcessor Class Referencefinal

The class handling the macros definitions and calls, given an AST. More...

#include <Processor.hpp>

Inheritance diagram for Ark::internal::MacroProcessor:
[legend]
Collaboration diagram for Ark::internal::MacroProcessor:
[legend]

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 Nodeast () const noexcept override
 Return the modified AST.
 
- Public Member Functions inherited from Ark::internal::Pass
 Pass (std::string name, unsigned debug_level)
 Construct a new Pass object.
 
virtual ~Pass ()=default
 

Private Member Functions

std::optional< NodelookupDefinedFunction (const std::string &name) const
 Return std::nullopt if the function isn't registered, otherwise return its node.
 
const NodefindNearestMacro (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< MacroScopem_macros
 Handling macros in a scope fashion.
 
std::shared_ptr< MacroExecutorm_conditional_executor
 
std::vector< std::shared_ptr< MacroExecutor > > m_executors
 
std::unordered_map< std::string, Nodem_defined_functions
 

Friends

class MacroExecutor
 

Additional Inherited Members

- Protected Attributes inherited from Ark::internal::Pass
Logger m_logger
 

Detailed Description

The class handling the macros definitions and calls, given an AST.

Definition at line 31 of file Processor.hpp.

Constructor & Destructor Documentation

◆ MacroProcessor()

Ark::internal::MacroProcessor::MacroProcessor ( unsigned debug)
explicitnoexcept

Construct a new Macro Processor object.

Parameters
debugthe debug level

Definition at line 20 of file Processor.cpp.

Member Function Documentation

◆ applyMacro()

bool Ark::internal::MacroProcessor::applyMacro ( Node & node,
unsigned depth )
private

Apply a macro on a given node.

Parameters
node
depth
Returns
true if a macro was applied
false

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().

◆ ast()

const Node & Ark::internal::MacroProcessor::ast ( ) const
nodiscardoverridevirtualnoexcept

Return the modified AST.

Returns
Node&

Implements Ark::internal::Pass.

Definition at line 45 of file Processor.cpp.

References m_ast.

Referenced by Ark::Welder::computeAST(), evaluate(), and process().

◆ checkMacroArgCountEq()

void Ark::internal::MacroProcessor::checkMacroArgCountEq ( const Node & node,
std::size_t expected,
const std::string & name,
const std::string & kind = "" )
staticprivate

Check if the given node has exactly the provided argument count, otherwise throws an error.

Parameters
nodea list node with a macro application, eg (= a b)
expectedexpected argument count, not counting the macro
namethe name of the macro being applied
kindthe 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().

◆ checkMacroArgCountGe()

void Ark::internal::MacroProcessor::checkMacroArgCountGe ( const Node & node,
std::size_t expected,
const std::string & name,
const std::string & kind = "" )
staticprivate

Check if the given node has at least the provided argument count, otherwise throws an error.

Parameters
nodea list node with a macro application, eg (= a b)
expectedexpected argument count, not counting the macro
namethe name of the macro being applied
kindthe 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().

◆ deleteNearestMacro()

void Ark::internal::MacroProcessor::deleteNearestMacro ( const std::string & name)
private

Find the nearest macro matching a given name and delete it.

Parameters
name

Definition at line 646 of file Processor.cpp.

References m_macros.

Referenced by evaluate().

◆ evaluate()

Node Ark::internal::MacroProcessor::evaluate ( Node & node,
unsigned depth,
bool is_not_body = false )
private

Evaluate only the macros.

Parameters
node
depth
is_not_bodytrue if the method is run on a non-body code (eg a condition of an if-macro)
Returns
Node

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().

◆ findNearestMacro()

const Node * Ark::internal::MacroProcessor::findNearestMacro ( const std::string & name) const
nodiscardprivate

Find the nearest macro matching a given name.

Parameters
name
Returns
const Node* nullptr if no macro was found

Definition at line 633 of file Processor.cpp.

References m_macros.

Referenced by evaluate(), Ark::internal::MacroExecutor::findNearestMacro(), and isConstEval().

◆ handleMacroNode()

void Ark::internal::MacroProcessor::handleMacroNode ( Node & node)
private

Registers macros based on their type, expand conditional macros.

Validate macros and register them by their name

Parameters
nodeA 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().

◆ isBeginNode()

bool Ark::internal::MacroProcessor::isBeginNode ( const Node & node)
staticprivate

Check if a given node is a list node, and starts with a Begin.

Parameters
node
Returns
true if it starts with a Begin
false

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().

◆ isConstEval()

◆ isTruthy()

bool Ark::internal::MacroProcessor::isTruthy ( const Node & node)
staticprivate

◆ lookupDefinedFunction()

std::optional< Node > Ark::internal::MacroProcessor::lookupDefinedFunction ( const std::string & name) const
nodiscardprivate

Return std::nullopt if the function isn't registered, otherwise return its node.

Parameters
namefunction name
Returns
std::optional<Node>

Definition at line 626 of file Processor.cpp.

References m_defined_functions.

Referenced by evaluate().

◆ process()

void Ark::internal::MacroProcessor::process ( const Node & ast)
overridevirtual

◆ processNode()

void Ark::internal::MacroProcessor::processNode ( Node & node,
unsigned depth,
bool is_processing_namespace = false )
private

◆ registerFuncDef()

◆ removeBegin()

void Ark::internal::MacroProcessor::removeBegin ( Node & node,
std::size_t i )
staticprivate

◆ throwMacroProcessingError()

void Ark::internal::MacroProcessor::throwMacroProcessingError ( const std::string & message,
const Node & node )
staticprivate

Friends And Related Symbol Documentation

◆ MacroExecutor

friend class MacroExecutor
friend

Definition at line 55 of file Processor.hpp.

Member Data Documentation

◆ m_ast

Node Ark::internal::MacroProcessor::m_ast
private

The modified AST.

Definition at line 58 of file Processor.hpp.

Referenced by ast(), and process().

◆ m_conditional_executor

std::shared_ptr<MacroExecutor> Ark::internal::MacroProcessor::m_conditional_executor
private

Definition at line 60 of file Processor.hpp.

Referenced by handleMacroNode().

◆ m_defined_functions

std::unordered_map<std::string, Node> Ark::internal::MacroProcessor::m_defined_functions
private

Definition at line 62 of file Processor.hpp.

Referenced by lookupDefinedFunction(), and registerFuncDef().

◆ m_executors

std::vector<std::shared_ptr<MacroExecutor> > Ark::internal::MacroProcessor::m_executors
private

Definition at line 61 of file Processor.hpp.

Referenced by applyMacro().

◆ m_macros

std::vector<MacroScope> Ark::internal::MacroProcessor::m_macros
private

Handling macros in a scope fashion.

Definition at line 59 of file Processor.hpp.

Referenced by deleteNearestMacro(), findNearestMacro(), handleMacroNode(), and processNode().


The documentation for this class was generated from the following files: