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.1
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2021
9 *
10 */
11
12#ifndef ARK_VM_SCOPE_HPP
13#define ARK_VM_SCOPE_HPP
14
15#include <vector>
16#include <utility>
17#include <cinttypes>
18
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 Put a value in the scope
38 *
39 * @param id The symbol id of the variable
40 * @param val The value linked to the symbol
41 */
42 void push_back(uint16_t id, Value&& val) 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, const Value& val) noexcept;
51
52 /**
53 * @brief Check if the scope has a specific symbol in memory
54 *
55 * @param id The id of the symbol
56 * @return true On success
57 * @return false Otherwise
58 */
59 bool has(uint16_t id) noexcept;
60
61 /**
62 * @brief Get a value from its symbol id
63 *
64 * @param id
65 * @return Value* Returns nullptr if the value can not be found
66 */
67 Value* operator[](uint16_t id) noexcept;
68
69 /**
70 * @brief Get the id of a variable based on its value ; used for debug only
71 *
72 * @param val
73 * @return uint16_t
74 */
75 uint16_t idFromValue(const Value& val) const noexcept;
76
77 /**
78 * @brief Return the size of the scope
79 *
80 * @return const std::size_t
81 */
82 std::size_t size() const noexcept;
83
84 friend class Ark::VM;
86
87 private:
88 std::vector<std::pair<uint16_t, Value>> m_data;
89 };
90}
91
92#endif
The ArkScript virtual machine, executing ArkScript bytecode.
Definition: VM.hpp:48
Closure management.
Definition: Closure.hpp:45
A class to handle the VM scope more efficiently.
Definition: Scope.hpp:28
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