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

The ArkScript AST optimizer. More...

#include <Optimizer.hpp>

+ Collaboration diagram for Ark::internal::Optimizer:

Public Member Functions

 Optimizer (uint16_t options) noexcept
 Construct a new Optimizer. More...
 
void feed (const Node &ast)
 Send the AST to the optimizer, then run the different optimization strategies on it. More...
 
const Nodeast () const noexcept
 Returns the modified AST. More...
 

Private Member Functions

void throwOptimizerError (const std::string &message, const Node &node)
 Generate a fancy error message. More...
 
void remove_unused ()
 Iterate over the AST and remove unused top level functions and constants. More...
 
void runOnGlobalScopeVars (Node &node, const std::function< void(Node &, Node &, int)> &func)
 Run a given functor on the global scope symbols. More...
 
void countOccurences (Node &node)
 Count the occurences of each symbol in the AST, recursively. More...
 

Private Attributes

Node m_ast
 
uint16_t m_options
 
std::unordered_map< std::string, unsigned > m_sym_appearances
 

Detailed Description

The ArkScript AST optimizer.

Definition at line 31 of file Optimizer.hpp.

Constructor & Destructor Documentation

◆ Optimizer()

Ark::internal::Optimizer::Optimizer ( uint16_t  options)
explicitnoexcept

Construct a new Optimizer.

Definition at line 5 of file Optimizer.cpp.

Member Function Documentation

◆ ast()

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

Returns the modified AST.

Returns
const Node&

Definition at line 17 of file Optimizer.cpp.

References m_ast.

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

◆ countOccurences()

void Ark::internal::Optimizer::countOccurences ( Node node)
private

◆ feed()

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

Send the AST to the optimizer, then run the different optimization strategies on it.

Parameters
ast

Definition at line 9 of file Optimizer.cpp.

References ast(), Ark::FeatureRemoveUnusedVars, m_ast, m_options, and remove_unused().

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

◆ remove_unused()

void Ark::internal::Optimizer::remove_unused ( )
private

Iterate over the AST and remove unused top level functions and constants.

Definition at line 27 of file Optimizer.cpp.

References Ark::internal::Node::constList(), countOccurences(), Ark::internal::Node::list(), Ark::internal::List, m_ast, m_sym_appearances, Ark::internal::Node::nodeType(), and runOnGlobalScopeVars().

Referenced by feed().

◆ runOnGlobalScopeVars()

void Ark::internal::Optimizer::runOnGlobalScopeVars ( Node node,
const std::function< void(Node &, Node &, int)> &  func 
)
private

Run a given functor on the global scope symbols.

Parameters
node
func

Definition at line 47 of file Optimizer.cpp.

References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Keyword, Ark::internal::Let, Ark::internal::Node::list(), and Ark::internal::Mut.

Referenced by remove_unused().

◆ throwOptimizerError()

void Ark::internal::Optimizer::throwOptimizerError ( const std::string &  message,
const Node node 
)
private

Generate a fancy error message.

Parameters
message
node

Definition at line 22 of file Optimizer.cpp.

References Ark::internal::makeNodeBasedErrorCtx().

Member Data Documentation

◆ m_ast

Node Ark::internal::Optimizer::m_ast
private

Definition at line 55 of file Optimizer.hpp.

Referenced by ast(), feed(), and remove_unused().

◆ m_options

uint16_t Ark::internal::Optimizer::m_options
private

Definition at line 56 of file Optimizer.hpp.

Referenced by feed().

◆ m_sym_appearances

std::unordered_map<std::string, unsigned> Ark::internal::Optimizer::m_sym_appearances
private

Definition at line 57 of file Optimizer.hpp.

Referenced by countOccurences(), and remove_unused().


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