ArkScript
A small, fast, functional and scripting language for video games
Scope.cpp
Go to the documentation of this file.
1#include <Ark/VM/Scope.hpp>
2
3#include <limits>
4
5namespace Ark::internal
6{
7 Scope::Scope() noexcept :
8 m_min_id(std::numeric_limits<uint16_t>::max()), m_max_id(0)
9 {
10 m_data.reserve(3);
11 }
12
14 {
15 for (auto& [id, val] : m_data)
16 {
17 if (val.valueType() == ValueType::Reference)
18 other.push_back(id, val);
19 else
20 other.push_back(id, Value(&val));
21 }
22 }
23
24 void Scope::push_back(uint16_t id, Value&& val) noexcept
25 {
26 if (id < m_min_id)
27 m_min_id = id;
28 if (id > m_max_id)
29 m_max_id = id;
30
31 m_data.emplace_back(id, std::move(val));
32 }
33
34 void Scope::push_back(uint16_t id, const Value& val) noexcept
35 {
36 if (id < m_min_id)
37 m_min_id = id;
38 if (id > m_max_id)
39 m_max_id = id;
40
41 m_data.emplace_back(id, val);
42 }
43
44 bool Scope::has(const uint16_t id) noexcept
45 {
46 return m_min_id <= id && id <= m_max_id && operator[](id) != nullptr;
47 }
48
49 Value* Scope::operator[](const uint16_t id_to_look_for) noexcept
50 {
51 for (auto& [id, value] : m_data)
52 {
53 if (id == id_to_look_for)
54 return &value;
55 }
56 return nullptr;
57 }
58
59 const Value* Scope::operator[](const uint16_t id_to_look_for) const noexcept
60 {
61 for (const auto& [id, value] : m_data)
62 {
63 if (id == id_to_look_for)
64 return &value;
65 }
66 return nullptr;
67 }
68
69 uint16_t Scope::idFromValue(const Value& val) const noexcept
70 {
71 for (const auto& [id, data] : m_data)
72 {
73 if (data == val)
74 return id;
75 }
76 return std::numeric_limits<uint16_t>::max();
77 }
78
79 std::size_t Scope::size() const noexcept
80 {
81 return m_data.size();
82 }
83
84 bool operator==(const Scope& A, const Scope& B) noexcept
85 {
86 const std::size_t size = A.size();
87 if (size != B.size())
88 return false;
89 if (A.m_min_id != B.m_min_id || A.m_max_id != B.m_max_id)
90 return false;
91
92 // assuming we have the same closure page address, the element should be in the same order
93 for (std::size_t i = 0; i < size; ++i)
94 {
95 if (A.m_data[i] != B.m_data[i])
96 return false;
97 }
98
99 return true;
100 }
101}
The virtual machine scope system.
A class to handle the VM scope more efficiently.
Definition Scope.hpp:28
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
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
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21