ArkScript
A small, lisp-inspired, functional scripting language
ScopeView.hpp
Go to the documentation of this file.
1/**
2 * @file Scope.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
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
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 pushBack(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 pushBack(uint16_t id, const Value& val) noexcept;
59
60 /**
61 * @brief Insert one or more pairs at the beginning of the scope
62 * @details This can ONLY be called on the last known scope, otherwise it will override the data of the next scope!
63 *
64 * @param values
65 */
66 void insertFront(const std::vector<pair_t>& values) noexcept;
67
68 /**
69 * @brief Check if the scope maybe holds a specific symbol in memory
70 *
71 * @param id The id of the symbol
72 * @return true On success
73 * @return false Otherwise
74 */
75 [[nodiscard]] bool maybeHas(uint16_t id) const noexcept;
76
77 /**
78 * @brief Get a value from its symbol id
79 *
80 * @param id_to_look_for
81 * @return Value* Returns nullptr if the value can not be found
82 */
83 [[nodiscard]] Value* operator[](uint16_t id_to_look_for) noexcept;
84
85 /**
86 * @brief Get a value from its symbol id
87 *
88 * @param id_to_look_for
89 * @return const Value* Returns nullptr if the value can not be found
90 */
91 [[nodiscard]] const Value* operator[](uint16_t id_to_look_for) const noexcept;
92
93 /**
94 * @brief Get the id of a variable based on its value ; used for debug only
95 *
96 * @param val
97 * @return uint16_t
98 */
99 [[nodiscard]] uint16_t idFromValue(const Value& val) const noexcept;
100
101 /**
102 * @brief Return the element at index in scope
103 *
104 * @return const pair_t&
105 */
106 [[nodiscard]] inline const pair_t& atPos(const std::size_t i) const noexcept
107 {
108 return m_storage[m_start + i];
109 }
110
111 /**
112 * @brief Return the element at index, starting from the end
113 *
114 * @return const pair_t&
115 */
116 [[nodiscard]] inline pair_t& atPosReverse(const std::size_t i) noexcept
117 {
118 return m_storage[m_start + m_size - 1 - i];
119 }
120
121 /**
122 * @brief Reset size, min and max id for the scope, to signify it's empty
123 */
124 void reset() noexcept;
125
126 /**
127 * @brief Return the size of the scope
128 *
129 * @return const std::size_t
130 */
131 [[nodiscard]] inline std::size_t size() const noexcept
132 {
133 return m_size;
134 }
135
136 /**
137 * @brief Compute the position of the first free slot in the shared storage, after this scope
138 *
139 * @return std::size_t
140 */
141 [[nodiscard]] inline std::size_t storageEnd() const noexcept
142 {
143 return m_start + m_size;
144 }
145
146 friend ARK_API bool operator==(const ScopeView& A, const ScopeView& B) noexcept;
147
148 friend class Ark::VM;
149
150 private:
152 std::size_t m_start;
153 std::size_t m_size;
154 uint16_t m_min_id; ///< Minimum stored ID, used for a basic bloom filter
155 uint16_t m_max_id; ///< Maximum stored ID, used for a basic bloom filter
156 };
157}
158
159#endif
#define ARK_API
Definition Module.hpp:22
ArkScript configuration macros.
Default value type handled by the virtual machine.
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
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.
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
STL namespace.