ArkScript
A small, fast, functional and scripting language for video games
ScopeView.hpp
Go to the documentation of this file.
1/**
2 * @file Scope.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief The virtual machine scope system
5 * @date 2020-10-27
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef ARK_VM_SCOPE_HPP
12#define ARK_VM_SCOPE_HPP
13
14#include <array>
15#include <cinttypes>
16
17#include <Ark/Platform.hpp>
18#include <Ark/VM/Value.hpp>
19
20namespace Ark::internal
21{
22 /**
23 * @brief A class to handle the VM scope more efficiently
24 *
25 */
27 {
28 public:
29 using pair_t = std::pair<uint16_t, Value>;
30
31 /**
32 * @brief Deleted constructor to avoid creating ScopeViews pointing to nothing. Helps catch bugs at compile time
33 */
34 ScopeView() = delete;
35
36 /**
37 * @brief Create a new ScopeView
38 *
39 * @param storage pointer to the shared scope storage
40 * @param start first free starting position
41 */
42 ScopeView(pair_t* storage, std::size_t start) noexcept;
43
44 /**
45 * @brief Put a value in the scope
46 *
47 * @param id The symbol id of the variable
48 * @param val The value linked to the symbol
49 */
50 void push_back(uint16_t id, Value&& val) noexcept;
51
52 /**
53 * @brief Put a value in the scope
54 *
55 * @param id The symbol id of the variable
56 * @param val The value linked to the symbol
57 */
58 void push_back(uint16_t id, const Value& val) noexcept;
59
60 /**
61 * @brief Check if the scope maybe holds a specific symbol in memory
62 *
63 * @param id The id of the symbol
64 * @return true On success
65 * @return false Otherwise
66 */
67 bool maybeHas(uint16_t id) const noexcept;
68
69 /**
70 * @brief Get a value from its symbol id
71 *
72 * @param id_to_look_for
73 * @return Value* Returns nullptr if the value can not be found
74 */
75 Value* operator[](uint16_t id_to_look_for) noexcept;
76
77 /**
78 * @brief Get a value from its symbol id
79 *
80 * @param id_to_look_for
81 * @return const Value* Returns nullptr if the value can not be found
82 */
83 const Value* operator[](uint16_t id_to_look_for) const noexcept;
84
85 /**
86 * @brief Get the id of a variable based on its value ; used for debug only
87 *
88 * @param val
89 * @return uint16_t
90 */
91 [[nodiscard]] uint16_t idFromValue(const Value& val) const noexcept;
92
93 /**
94 * @brief Return the element at index in scope
95 *
96 * @return const pair_t&
97 */
98 [[nodiscard]] inline const pair_t& atPos(const std::size_t i) const noexcept
99 {
100 return m_storage[m_start + i];
101 }
102
103 /**
104 * @brief Return the element at index, starting from the end
105 *
106 * @return const pair_t&
107 */
108 [[nodiscard]] inline pair_t& atPosReverse(const std::size_t i) noexcept
109 {
110 return m_storage[m_start + m_size - 1 - i];
111 }
112
113 /**
114 * @brief Reset size, min and max id for the scope, to signify it's empty
115 */
116 void reset() noexcept;
117
118 /**
119 * @brief Return the size of the scope
120 *
121 * @return const std::size_t
122 */
123 [[nodiscard]] inline std::size_t size() const noexcept
124 {
125 return m_size;
126 }
127
128 /**
129 * @brief Compute the position of the first free slot in the shared storage, after this scope
130 *
131 * @return std::size_t
132 */
133 [[nodiscard]] inline std::size_t storageEnd() const noexcept
134 {
135 return m_start + m_size;
136 }
137
138 friend ARK_API bool operator==(const ScopeView& A, const ScopeView& B) noexcept;
139
140 friend class Ark::VM;
141
142 private:
144 std::size_t m_start;
145 std::size_t m_size;
146 uint16_t m_min_id; ///< Minimum stored ID, used for a basic bloom filter
147 uint16_t m_max_id; ///< Maximum stored ID, used for a basic bloom filter
148 };
149}
150
151#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
Default value type handled by the virtual machine.
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:45
A class to handle the VM scope more efficiently.
Definition ScopeView.hpp:27
ScopeView()=delete
Deleted constructor to avoid creating ScopeViews pointing to nothing. Helps catch bugs at compile tim...
uint16_t m_max_id
Maximum stored ID, used for a basic bloom filter.
std::size_t storageEnd() const noexcept
Compute the position of the first free slot in the shared storage, after this scope.
std::pair< uint16_t, Value > pair_t
Definition ScopeView.hpp:29
const pair_t & atPos(const std::size_t i) const noexcept
Return the element at index in scope.
Definition ScopeView.hpp:98
uint16_t m_min_id
Minimum stored ID, used for a basic bloom filter.
pair_t & atPosReverse(const std::size_t i) noexcept
Return the element at index, starting from the end.
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21