ArkScript
A small, fast, functional and scripting language for video games
Closure.cpp
Go to the documentation of this file.
2
4#include <Ark/VM/VM.hpp>
5
6#include <ranges>
7
8namespace Ark::internal
9{
10 Closure::Closure(const ClosureScope& scope, const PageAddr_t pa) noexcept :
11 m_scope(std::make_shared<ClosureScope>(scope)),
12 m_page_addr(pa)
13 {}
14
15 Closure::Closure(const std::shared_ptr<ClosureScope>& scope_ptr, const PageAddr_t pa) noexcept :
16 m_scope(scope_ptr),
17 m_page_addr(pa)
18 {}
19
20 bool Closure::hasFieldEndingWith(const std::string& end, const VM& vm) const
21 {
22 return std::ranges::any_of(std::ranges::views::keys(m_scope->m_data), [&vm, &end](const auto& id) {
23 return end.ends_with(":" + vm.m_state.m_symbols[id]);
24 });
25 }
26
27 std::string Closure::toString(VM& vm) const noexcept
28 {
29 std::string out = "(";
30 for (std::size_t i = 0, end = m_scope->m_data.size(); i < end; ++i)
31 {
32 if (i != 0)
33 out += ' ';
34
35 out += '.' + vm.m_state.m_symbols[m_scope->m_data[i].first] + '=';
36 out += m_scope->m_data[i].second.toString(vm);
37 }
38 return out + ")";
39 }
40
41 bool operator==(const Closure& A, const Closure& B) noexcept
42 {
43 // they do not come from the same closure builder
44 if (A.m_page_addr != B.m_page_addr)
45 return false;
46
47 return *A.m_scope == *B.m_scope;
48 }
49}
Subtype of the value type, handling closures.
Subtype of the value type, handling closures.
The ArkScript virtual machine.
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:45
A class to store fields captured by a closure.
Closure management.
Definition Closure.hpp:36
std::string toString(VM &vm) const noexcept
Print the closure to a string.
Definition Closure.cpp:27
bool hasFieldEndingWith(const std::string &end, const 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:20
std::shared_ptr< ClosureScope > m_scope
Definition Closure.hpp:83
Closure(const ClosureScope &scope, PageAddr_t pa) noexcept
Construct a new Closure object.
Definition Closure.cpp:10
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21
uint16_t PageAddr_t
Definition Closure.hpp:27