ArkScript
A small, fast, functional and scripting language for video games
LocalsLocator.hpp
Go to the documentation of this file.
1/**
2 * @file LocalsLocator.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Track locals at compile
5 * @date 2025-03-20
6 *
7 * @copyright Copyright (c) 2025
8 *
9 */
10
11#ifndef ARK_COMPILER_LOWERER_LOCALSLOCATOR_HPP
12#define ARK_COMPILER_LOWERER_LOCALSLOCATOR_HPP
13
14#include <vector>
15#include <string>
16#include <optional>
17
18namespace Ark::internal
19{
21 {
22 public:
23 enum class ScopeType
24 {
25 Default,
28 };
29
30 /**
31 * @brief Create a new LocalsLocator to track the position of variables in the scope stack
32 */
34
35 /**
36 * @brief Register a local in the current scope, triggered by a STORE instruction. If the local already exists, it won't be added.
37 *
38 * @param name local's name
39 */
40 void addLocal(const std::string& name);
41
42 /**
43 * @brief 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.
44 *
45 * @param name local's name
46 * @return std::optional<std::size_t>
47 */
48 std::optional<std::size_t> lookupLastScopeByName(const std::string& name);
49
50 /**
51 * @brief Create a new scope
52 *
53 * @param type scope type, default `ScopeType::Default`
54 */
56
57 /**
58 * @brief Delete the last scope
59 */
60 void deleteScope();
61
62 /**
63 * @brief Save the current scope length before entering a branch, so that we can ignore variable definitions inside the branch and generate valid indices
64 */
66
67 /**
68 * @brief Drop potentially defined variables in the last saved branch
69 */
70 void dropVarsForBranch();
71
72 private:
73 struct Scope
74 {
75 std::vector<std::string> data;
77 };
78
79 std::vector<Scope> m_scopes;
80 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
81 };
82}
83
84#endif // ARK_COMPILER_LOWERER_LOCALSLOCATOR_HPP
Closure management.
Definition Closure.hpp:36
void saveScopeLengthForBranch()
Save the current scope length before entering a branch, so that we can ignore variable definitions in...
std::vector< Scope > m_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.
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 var...
void dropVarsForBranch()
Drop potentially defined variables in the last saved branch.
void deleteScope()
Delete the last scope.
void addLocal(const std::string &name)
Register a local in the current scope, triggered by a STORE instruction. If the local already exists,...
LocalsLocator()
Create a new LocalsLocator to track the position of variables in the scope stack.
void createScope(ScopeType type=ScopeType::Default)
Create a new scope.
std::vector< std::string > data