ArkScript
A small, fast, functional and scripting language for video games
Scope.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 * @version 0.2
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef ARK_VM_SCOPE_HPP
13#define ARK_VM_SCOPE_HPP
14
15#include <vector>
16#include <cinttypes>
17
18#include <Ark/Platform.hpp>
19#include <Ark/VM/Value.hpp>
20
21namespace Ark::internal
22{
23 /**
24 * @brief A class to handle the VM scope more efficiently
25 *
26 */
27 class Scope
28 {
29 public:
30 /**
31 * @brief Construct a new Scope object
32 *
33 */
34 Scope() noexcept;
35
36 /**
37 * @brief Merge values from this scope as refs in the other scope
38 * @details This scope must be kept alive for the ref to be used
39 * @param other
40 */
41 void mergeRefInto(Scope& other);
42
43 /**
44 * @brief Put a value in the scope
45 *
46 * @param id The symbol id of the variable
47 * @param val The value linked to the symbol
48 */
49 void push_back(uint16_t id, Value&& val) noexcept;
50
51 /**
52 * @brief Put a value in the scope
53 *
54 * @param id The symbol id of the variable
55 * @param val The value linked to the symbol
56 */
57 void push_back(uint16_t id, const Value& val) noexcept;
58
59 /**
60 * @brief Check if the scope has a specific symbol in memory
61 *
62 * @param id The id of the symbol
63 * @return true On success
64 * @return false Otherwise
65 */
66 bool has(uint16_t id) noexcept;
67
68 /**
69 * @brief Get a value from its symbol id
70 *
71 * @param id_to_look_for
72 * @return Value* Returns nullptr if the value can not be found
73 */
74 Value* operator[](uint16_t id_to_look_for) noexcept;
75
76 /**
77 * @brief Get a value from its symbol id
78 *
79 * @param id_to_look_for
80 * @return const Value* Returns nullptr if the value can not be found
81 */
82 const Value* operator[](uint16_t id_to_look_for) const noexcept;
83
84 /**
85 * @brief Get the id of a variable based on its value ; used for debug only
86 *
87 * @param val
88 * @return uint16_t
89 */
90 [[nodiscard]] uint16_t idFromValue(const Value& val) const noexcept;
91
92 /**
93 * @brief Return the size of the scope
94 *
95 * @return const std::size_t
96 */
97 [[nodiscard]] std::size_t size() const noexcept;
98
99 friend ARK_API bool operator==(const Scope& A, const Scope& B) noexcept;
100
101 friend class Ark::VM;
103
104 private:
105 std::vector<std::pair<uint16_t, Value>> m_data;
106 uint16_t m_min_id; ///< Minimum stored ID, used for a basic bloom filter
107 uint16_t m_max_id; ///< Maximum stored ID, used for a basic bloom filter
108 };
109}
110
111#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:44
Closure management.
Definition Closure.hpp:37
A class to handle the VM scope more efficiently.
Definition Scope.hpp:28
uint16_t m_max_id
Maximum stored ID, used for a basic bloom filter.
Definition Scope.hpp:107
void mergeRefInto(Scope &other)
Merge values from this scope as refs in the other scope.
Definition Scope.cpp:13
bool has(uint16_t id) noexcept
Check if the scope has a specific symbol in memory.
Definition Scope.cpp:44
std::vector< std::pair< uint16_t, Value > > m_data
Definition Scope.hpp:105
uint16_t m_min_id
Minimum stored ID, used for a basic bloom filter.
Definition Scope.hpp:106
void push_back(uint16_t id, Value &&val) noexcept
Put a value in the scope.
Definition Scope.cpp:24
uint16_t idFromValue(const Value &val) const noexcept
Get the id of a variable based on its value ; used for debug only.
Definition Scope.cpp:69
Value * operator[](uint16_t id_to_look_for) noexcept
Get a value from its symbol id.
Definition Scope.cpp:49
std::size_t size() const noexcept
Return the size of the scope.
Definition Scope.cpp:79
Scope() noexcept
Construct a new Scope object.
Definition Scope.cpp:7
friend ARK_API bool operator==(const Scope &A, const Scope &B) noexcept
Definition Scope.cpp:84