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

#include <LocalsLocator.hpp>

Collaboration diagram for Ark::internal::LocalsLocator:
[legend]

Classes

struct  Scope
 

Public Types

enum class  ScopeType { Default , Function , Closure }
 

Public Member Functions

 LocalsLocator ()
 Create a new LocalsLocator to track the position of variables in the scope stack.
 
void addLocal (const std::string &name)
 Register a local in the current scope, triggered by a STORE instruction. If the local already exists, it won't be added.
 
std::optional< std::size_t > lookupLastScopeByName (const std::string &name)
 Search for a local in the current scope. Returns std::nullopt in case of closure scopes or if the variable is in a parent scope.
 
void createScope (ScopeType type=ScopeType::Default)
 Create a new scope.
 
void deleteScope ()
 Delete the last scope.
 
void saveScopeLengthForBranch ()
 Save the current scope length before entering a branch, so that we can ignore variable definitions inside the branch and generate valid indices.
 
void dropVarsForBranch ()
 Drop potentially defined variables in the last saved branch.
 

Private Attributes

std::vector< Scopem_scopes
 
std::vector< std::size_t > m_drop_for_conds
 Needed to drop variables inside if/else branches since they don't have their own scope.
 

Detailed Description

Definition at line 20 of file LocalsLocator.hpp.

Member Enumeration Documentation

◆ ScopeType

Enumerator
Default 
Function 
Closure 

Definition at line 23 of file LocalsLocator.hpp.

Constructor & Destructor Documentation

◆ LocalsLocator()

Ark::internal::LocalsLocator::LocalsLocator ( )

Create a new LocalsLocator to track the position of variables in the scope stack.

Definition at line 7 of file LocalsLocator.cpp.

References m_scopes.

Member Function Documentation

◆ addLocal()

void Ark::internal::LocalsLocator::addLocal ( const std::string & name)

Register a local in the current scope, triggered by a STORE instruction. If the local already exists, it won't be added.

Parameters
namelocal's name

Definition at line 13 of file LocalsLocator.cpp.

References m_scopes.

Referenced by Ark::internal::ASTLowerer::compileFunction(), and Ark::internal::ASTLowerer::compileLetMutSet().

◆ createScope()

void Ark::internal::LocalsLocator::createScope ( ScopeType type = ScopeType::Default)

Create a new scope.

Parameters
typescope type, default ScopeType::Default

Definition at line 34 of file LocalsLocator.cpp.

References m_scopes.

Referenced by Ark::internal::ASTLowerer::compileFunction(), and Ark::internal::ASTLowerer::compileWhile().

◆ deleteScope()

void Ark::internal::LocalsLocator::deleteScope ( )

Delete the last scope.

Definition at line 41 of file LocalsLocator.cpp.

References m_scopes.

Referenced by Ark::internal::ASTLowerer::compileFunction(), and Ark::internal::ASTLowerer::compileWhile().

◆ dropVarsForBranch()

void Ark::internal::LocalsLocator::dropVarsForBranch ( )

Drop potentially defined variables in the last saved branch.

Definition at line 51 of file LocalsLocator.cpp.

References m_drop_for_conds, and m_scopes.

Referenced by Ark::internal::ASTLowerer::compileIf().

◆ lookupLastScopeByName()

std::optional< std::size_t > Ark::internal::LocalsLocator::lookupLastScopeByName ( const std::string & name)

Search for a local in the current scope. Returns std::nullopt in case of closure scopes or if the variable is in a parent scope.

Parameters
namelocal's name
Returns
std::optional<std::size_t>

Definition at line 20 of file LocalsLocator.cpp.

References Closure, and m_scopes.

Referenced by Ark::internal::ASTLowerer::compileSymbol().

◆ saveScopeLengthForBranch()

void Ark::internal::LocalsLocator::saveScopeLengthForBranch ( )

Save the current scope length before entering a branch, so that we can ignore variable definitions inside the branch and generate valid indices.

Definition at line 46 of file LocalsLocator.cpp.

References m_drop_for_conds, and m_scopes.

Referenced by Ark::internal::ASTLowerer::compileIf().

Member Data Documentation

◆ m_drop_for_conds

std::vector<std::size_t> Ark::internal::LocalsLocator::m_drop_for_conds
private

Needed to drop variables inside if/else branches since they don't have their own scope.

Definition at line 80 of file LocalsLocator.hpp.

Referenced by dropVarsForBranch(), and saveScopeLengthForBranch().

◆ m_scopes

std::vector<Scope> Ark::internal::LocalsLocator::m_scopes
private

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