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

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

#include <Processor.hpp>

+ Collaboration diagram for Ark::internal::MacroProcessor:

Public Member Functions

 MacroProcessor (unsigned debug, uint16_t options) noexcept
 Construct a new Macro Processor object. More...
 
void feed (const Node &ast)
 Send the complete AST (after the inclusions and stuff), and work on it. More...
 
const Nodeast () const noexcept
 Return the modified AST. More...
 

Private Member Functions

NodefindNearestMacro (const std::string &name)
 Find the nearest macro matching a given name. More...
 
void deleteNearestMacro (const std::string &name)
 Find the nearest macro matching a given name and delete it. More...
 
bool isPredefined (const std::string &symbol)
 Check if a given symbol is a predefined macro or not. More...
 
void recurApply (Node &node)
 Recursively apply macros on a given node. More...
 
bool hadBegin (const Node &node)
 Check if a given node is a list node, and starts with a Begin. More...
 
void removeBegin (Node &node, std::size_t &i)
 Remove a begin block added by a macro. More...
 
bool isConstEval (const Node &node) const
 Check if a node can be evaluated at compile time. More...
 
void registerMacro (Node &node)
 Registers macros based on their type. More...
 
void registerFuncDef (Node &node)
 Registers a function definition node. More...
 
void process (Node &node, unsigned depth)
 Register macros in scopes and apply them as needed. More...
 
bool applyMacro (Node &node)
 Apply a macro on a given node. More...
 
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. More...
 
Node evaluate (Node &node, bool is_not_body=false)
 Evaluate only the macros. More...
 
bool isTruthy (const Node &node)
 Check if a node can be evaluated to true. More...
 
void throwMacroProcessingError (const std::string &message, const Node &node)
 Throw a macro processing error. More...
 

Private Attributes

unsigned m_debug
 The debug level. More...
 
uint16_t m_options
 
Node m_ast
 The modified AST. More...
 
std::vector< std::unordered_map< std::string, Node > > m_macros
 Handling macros in a scope fashion. More...
 
MacroExecutorPipeline m_executor_pipeline
 
std::vector< std::string > m_predefined_macros
 Already existing macros, non-keywords, non-builtins. More...
 
std::unordered_map< std::string, Nodem_defined_functions
 

Friends

class MacroExecutor
 

Detailed Description

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

Definition at line 29 of file Processor.hpp.

Constructor & Destructor Documentation

◆ MacroProcessor()

Ark::internal::MacroProcessor::MacroProcessor ( unsigned  debug,
uint16_t  options 
)
noexcept

Construct a new Macro Processor object.

Parameters
debugthe debug level
optionsthe options flags

Definition at line 15 of file Processor.cpp.

Member Function Documentation

◆ applyMacro()

bool Ark::internal::MacroProcessor::applyMacro ( Node node)
private

Apply a macro on a given node.

Parameters
node
Returns
true if a macro was applied
false

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

◆ ast()

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

Return the modified AST.

Returns
Node&

Definition at line 46 of file Processor.cpp.

References m_ast.

Referenced by feed(), and Ark::Compiler::feed().

◆ 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 515 of file Processor.cpp.

References m_macros.

Referenced by registerMacro().

◆ evaluate()

◆ feed()

void Ark::internal::MacroProcessor::feed ( const Node ast)

Send the complete AST (after the inclusions and stuff), and work on it.

Parameters
ast

Definition at line 30 of file Processor.cpp.

References ast(), m_ast, m_debug, and process().

Referenced by Ark::Compiler::feed().

◆ findNearestMacro()

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

Find the nearest macro matching a given name.

Parameters
name
Returns
Node* nullptr if no macro was found

Definition at line 499 of file Processor.cpp.

References m_macros.

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

◆ hadBegin()

bool Ark::internal::MacroProcessor::hadBegin ( const Node node)
private

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

◆ isConstEval()

◆ isPredefined()

bool Ark::internal::MacroProcessor::isPredefined ( const std::string &  symbol)
private

Check if a given symbol is a predefined macro or not.

Parameters
symbol
Returns
true
false

Definition at line 534 of file Processor.cpp.

References m_predefined_macros.

Referenced by Ark::internal::MacroExecutor::isPredefined().

◆ isTruthy()

bool Ark::internal::MacroProcessor::isTruthy ( const Node node)
private

◆ process()

void Ark::internal::MacroProcessor::process ( Node node,
unsigned  depth 
)
private

Register macros in scopes and apply them as needed.

Parameters
nodenode on which to operate
depth

Definition at line 132 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(), recurApply(), registerFuncDef(), registerMacro(), removeBegin(), and Ark::internal::Unused.

Referenced by feed().

◆ recurApply()

void Ark::internal::MacroProcessor::recurApply ( Node node)
private

Recursively apply macros on a given node.

Parameters
node

Definition at line 544 of file Processor.cpp.

References applyMacro(), Ark::internal::Node::list(), Ark::internal::List, and Ark::internal::Node::nodeType().

Referenced by process().

◆ registerFuncDef()

void Ark::internal::MacroProcessor::registerFuncDef ( Node node)
private

◆ registerMacro()

void Ark::internal::MacroProcessor::registerMacro ( Node node)
private

◆ removeBegin()

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

◆ throwMacroProcessingError()

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

Throw a macro processing error.

Parameters
messagethe error
nodethe node in which there is an error

Definition at line 624 of file Processor.cpp.

References Ark::internal::makeNodeBasedErrorCtx().

Referenced by evaluate(), isTruthy(), registerMacro(), Ark::internal::MacroExecutor::throwMacroProcessingError(), and unify().

◆ unify()

void Ark::internal::MacroProcessor::unify ( const std::unordered_map< std::string, Node > &  map,
Node target,
Node parent,
std::size_t  index = 0 
)
private

Unify a target node with a given map symbol => replacement node, recursively.

Parameters
map
target
parent
indexposition 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, and throwMacroProcessingError().

Referenced by Ark::internal::ConditionalExecutor::MacroExecutor().

Friends And Related Function Documentation

◆ MacroExecutor

friend class MacroExecutor
friend

Definition at line 54 of file Processor.hpp.

Member Data Documentation

◆ m_ast

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

The modified AST.

Definition at line 59 of file Processor.hpp.

Referenced by ast(), and feed().

◆ m_debug

unsigned Ark::internal::MacroProcessor::m_debug
private

The debug level.

Definition at line 57 of file Processor.hpp.

Referenced by feed().

◆ m_defined_functions

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

Definition at line 63 of file Processor.hpp.

Referenced by evaluate(), and registerFuncDef().

◆ m_executor_pipeline

MacroExecutorPipeline Ark::internal::MacroProcessor::m_executor_pipeline
private

Definition at line 61 of file Processor.hpp.

Referenced by applyMacro().

◆ m_macros

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

Handling macros in a scope fashion.

Definition at line 60 of file Processor.hpp.

Referenced by deleteNearestMacro(), findNearestMacro(), process(), and registerMacro().

◆ m_options

uint16_t Ark::internal::MacroProcessor::m_options
private

Definition at line 58 of file Processor.hpp.

◆ m_predefined_macros

std::vector<std::string> Ark::internal::MacroProcessor::m_predefined_macros
private

Already existing macros, non-keywords, non-builtins.

Definition at line 62 of file Processor.hpp.

Referenced by isPredefined().


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