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 {
9 m_data.reserve(3);
10 }
11
12 void Scope::push_back(uint16_t id, Value&& val) noexcept
13 {
14 m_data.emplace_back(std::move(id), std::move(val));
15 }
16
17 void Scope::push_back(uint16_t id, const Value& val) noexcept
18 {
19 m_data.emplace_back(id, val);
20 }
21
22 bool Scope::has(uint16_t id) noexcept
23 {
24 return operator[](id) != nullptr;
25 }
26
27 Value* Scope::operator[](uint16_t id) noexcept
28 {
29 for (std::size_t i = 0, end = m_data.size(); i < end; ++i)
30 {
31 if (m_data[i].first == id)
32 return &m_data[i].second;
33 }
34 return nullptr;
35 }
36
37 uint16_t Scope::idFromValue(const Value& val) const noexcept
38 {
39 for (std::size_t i = 0, end = m_data.size(); i < end; ++i)
40 {
41 if (m_data[i].second == val)
42 return m_data[i].first;
43 }
44 return std::numeric_limits<uint16_t>::max();
45 }
46
47 std::size_t Scope::size() const noexcept
48 {
49 return m_data.size();
50 }
51}
The virtual machine scope system.
Value * operator[](uint16_t id) noexcept
Get a value from its symbol id.
Definition: Scope.cpp:27
bool has(uint16_t id) noexcept
Check if the scope has a specific symbol in memory.
Definition: Scope.cpp:22
std::vector< std::pair< uint16_t, Value > > m_data
Definition: Scope.hpp:88
void push_back(uint16_t id, Value &&val) noexcept
Put a value in the scope.
Definition: Scope.cpp:12
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:37
std::size_t size() const noexcept
Return the size of the scope.
Definition: Scope.cpp:47
Scope() noexcept
Construct a new Scope object.
Definition: Scope.cpp:7