ArkScript
A small, fast, functional and scripting language for video games
Closure.cpp
Go to the documentation of this file.
2
3#include <Ark/VM/Scope.hpp>
4#include <Ark/VM/Value.hpp>
5#include <Ark/VM/VM.hpp>
6
7#include <ranges>
8
9namespace Ark::internal
10{
11 Closure::Closure(const Scope& scope, const PageAddr_t pa) noexcept :
12 m_scope(std::make_shared<Scope>(scope)),
13 m_page_addr(pa)
14 {}
15
16 Closure::Closure(const std::shared_ptr<Scope>& scope_ptr, const PageAddr_t pa) noexcept :
17 m_scope(scope_ptr),
18 m_page_addr(pa)
19 {}
20
21 bool Closure::hasFieldEndingWith(const std::string& end, VM& vm) const
22 {
23 for (const auto id : std::ranges::views::keys(m_scope->m_data))
24 {
25 if (end.ends_with(":" + vm.m_state.m_symbols[id]))
26 return true;
27 }
28 return false;
29 }
30
31 std::string Closure::toString(VM& vm) const noexcept
32 {
33 std::string out = "(";
34 for (std::size_t i = 0, end = m_scope->m_data.size(); i < end; ++i)
35 {
36 if (i != 0)
37 out += ' ';
38
39 out += '.' + vm.m_state.m_symbols[m_scope->m_data[i].first] + '=';
40 out += m_scope->m_data[i].second.toString(vm);
41 }
42 return out + ")";
43 }
44
45 bool operator==(const Closure& A, const Closure& B) noexcept
46 {
47 // they do not come from the same closure builder
48 if (A.m_page_addr != B.m_page_addr)
49 return false;
50
51 return *A.m_scope == *B.m_scope;
52 }
53}
Subtype of the value type, handling closures.
The virtual machine scope system.
The ArkScript virtual machine.
std::vector< std::string > m_symbols
Definition State.hpp:149
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:44
State & m_state
Definition VM.hpp:157
Closure management.
Definition Closure.hpp:37
bool hasFieldEndingWith(const std::string &end, VM &vm) const
Used when generating error messages in the VM, to see if a symbol might have been wrongly fully quali...
Definition Closure.cpp:21
std::string toString(VM &vm) const noexcept
Print the closure to a string.
Definition Closure.cpp:31
Closure(const Scope &scope, PageAddr_t pa) noexcept
Construct a new Closure object.
Definition Closure.cpp:11
std::shared_ptr< Scope > m_scope
Definition Closure.hpp:84
A class to handle the VM scope more efficiently.
Definition Scope.hpp:28
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21
uint16_t PageAddr_t
Definition Closure.hpp:30