ArkScript
A small, lisp-inspired, functional scripting language
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.
 
bool dropVarsForBranch ()
 Drop potentially defined variables in the last saved branch.
 
void markLastLocalAsUnreachable ()
 Mark the last variable of a scope as unreachable, blocking lookupLastScopeByName(..) from returning a value past that point.
 

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 44 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 51 of file LocalsLocator.cpp.

References m_scopes.

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

◆ dropVarsForBranch()

bool Ark::internal::LocalsLocator::dropVarsForBranch ( )
nodiscard

Drop potentially defined variables in the last saved branch.

Returns
true if vars were dropped, meaning that we created at least one variable in a branch
false if nothing was dropped

Definition at line 61 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 24 of file LocalsLocator.cpp.

References Closure, and m_scopes.

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

◆ markLastLocalAsUnreachable()

void Ark::internal::LocalsLocator::markLastLocalAsUnreachable ( )

Mark the last variable of a scope as unreachable, blocking lookupLastScopeByName(..) from returning a value past that point.

Definition at line 77 of file LocalsLocator.cpp.

References m_scopes.

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

◆ 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 56 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 94 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: