ArkScript
A small, fast, functional and scripting language for video games
Closure.hpp
Go to the documentation of this file.
1/**
2 * @file Closure.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Subtype of the value type, handling closures
5 * @version 2.0
6 * @date 2024-04-21
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef VM_VALUE_CLOSURE_HPP
13#define VM_VALUE_CLOSURE_HPP
14
15#include <memory>
16#include <iostream>
17#include <string>
18
19#include <Ark/Platform.hpp>
20
21namespace Ark
22{
23 class VM;
24}
25
26namespace Ark::internal
27{
28 class Scope;
29
30 using PageAddr_t = uint16_t;
31
32 /**
33 * @brief Closure management
34 *
35 */
36 class Closure
37 {
38 public:
39 /**
40 * @brief Construct a new Closure object
41 *
42 * @param scope the scope of the function turned into a closure
43 * @param pa the current page address of the function turned into a closure
44 */
45 Closure(const Scope& scope, PageAddr_t pa) noexcept;
46
47 /**
48 * @brief Construct a new Closure object
49 * @param scope_ptr a shared pointer to the scope of the function turned into a closure
50 * @param pa the current page address of the function turned into a closure
51 */
52 Closure(const std::shared_ptr<Scope>& scope_ptr, PageAddr_t pa) noexcept;
53
54 [[nodiscard]] const Scope& scope() const noexcept { return *m_scope; }
55 [[nodiscard]] Scope& refScope() const noexcept { return *m_scope; }
56 [[nodiscard]] const std::shared_ptr<Scope>& scopePtr() const { return m_scope; }
57
58 /**
59 *
60 * @return PageAddr_t the bytecode page address this closure refers to
61 */
62 [[nodiscard]] PageAddr_t pageAddr() const { return m_page_addr; }
63
64 /**
65 * @brief Used when generating error messages in the VM, to see if a symbol might have been wrongly fully qualified
66 *
67 * @param end
68 * @param vm
69 * @return true if the closure has a field which is the end of 'end'
70 */
71 [[nodiscard]] bool hasFieldEndingWith(const std::string& end, VM& vm) const;
72
73 /**
74 * @brief Print the closure to a string
75 *
76 * @param vm
77 */
78 std::string toString(VM& vm) const noexcept;
79
80 friend ARK_API bool operator==(const Closure& A, const Closure& B) noexcept;
81 friend ARK_API_INLINE bool operator<(const Closure& A, const Closure& B) noexcept;
82
83 private:
84 std::shared_ptr<Scope> m_scope;
85 // keep track of the code page number, in case we need it later
87 };
88
89 inline bool operator<(const Closure& A, const Closure& B) noexcept
90 {
91 return A.m_page_addr < B.m_page_addr;
92 }
93}
94
95#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
#define ARK_API_INLINE
Definition Platform.hpp:51
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:44
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
const std::shared_ptr< Scope > & scopePtr() const
Definition Closure.hpp:56
std::string toString(VM &vm) const noexcept
Print the closure to a string.
Definition Closure.cpp:31
PageAddr_t pageAddr() const
Definition Closure.hpp:62
PageAddr_t m_page_addr
Definition Closure.hpp:86
friend ARK_API_INLINE bool operator<(const Closure &A, const Closure &B) noexcept
Definition Closure.hpp:89
friend ARK_API bool operator==(const Closure &A, const Closure &B) noexcept
Definition Closure.cpp:45
Scope & refScope() const noexcept
Definition Closure.hpp:55
std::shared_ptr< Scope > m_scope
Definition Closure.hpp:84
const Scope & scope() const noexcept
Definition Closure.hpp:54
A class to handle the VM scope more efficiently.
Definition Scope.hpp:28
bool operator<(const Namespace &, const Namespace &)
Definition Namespace.hpp:27
uint16_t PageAddr_t
Definition Closure.hpp:30