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.
 
void feed (const Node &ast)
 Send the complete AST (after the inclusions and stuff), and work on it.
 
const Nodeast () const noexcept
 Return the modified AST.
 

Private Member Functions

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 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< MacroScopem_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, Nodem_defined_functions
 

Friends

class MacroExecutor
 

Detailed Description

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

Definition at line 30 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(), Ark::Compiler::feed(), and Ark::JsonCompiler::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 516 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(), and Ark::JsonCompiler::feed().

◆ findNearestMacro()

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

Find the nearest macro matching a given name.

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

Definition at line 503 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 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().

◆ 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 531 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 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.

Referenced by feed(), and process().

◆ recurApply()

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

Recursively apply macros on a given node.

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

◆ 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 621 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, throwMacroProcessingError(), and unify().

Referenced by Ark::internal::ConditionalExecutor::MacroExecutor(), and unify().

Friends And Related Function 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 60 of file Processor.hpp.

Referenced by ast(), and feed().

◆ m_debug

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

The debug level.

Definition at line 58 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 64 of file Processor.hpp.

Referenced by evaluate(), and registerFuncDef().

◆ m_executor_pipeline

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

Definition at line 62 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 61 of file Processor.hpp.

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

◆ m_options

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

Definition at line 59 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 63 of file Processor.hpp.

Referenced by isPredefined().


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