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 (lexplt.dev@gmail.com)
4  * @brief Subtype of the value type, handling closures
5  * @version 0.1
6  * @date 2020-10-27
7  *
8  * @copyright Copyright (c) 2020-2021
9  *
10  */
11 
12 #ifndef VM_VALUE_CLOSURE_HPP
13 #define VM_VALUE_CLOSURE_HPP
14 
15 #include <memory>
16 #include <vector>
17 #include <iostream>
18 
19 #include <Ark/Platform.hpp>
20 
21 namespace Ark
22 {
23  class VM;
24 }
25 
26 namespace Ark::internal
27 {
28  class Scope;
29 
30  /**
31  * @brief Scope handling
32  *
33  * A scope is defined as a shared pointer to a list of local variables
34  * because a Closure could continue to leave when the local variables list
35  * has been closed by the virtual machine
36  */
37  using Scope_t = std::shared_ptr<Scope>;
38  using PageAddr_t = uint16_t;
39 
40  /**
41  * @brief Closure management
42  *
43  */
44  class Closure
45  {
46  public:
47  /**
48  * @brief Construct a new Closure object
49  *
50  */
51  Closure() noexcept;
52 
53  /**
54  * @brief Construct a new Closure object
55  *
56  * @param scope_ptr the scope of the function turned into a closure
57  * @param pa the current page address of the function turned into a closure
58  */
59  Closure(Scope_t&& scope_ptr, PageAddr_t pa) noexcept;
60 
61  /**
62  * @brief Construct a new Closure object
63  *
64  * @param scope_ptr the scope of the function turned into a closure
65  * @param pa the current page address of the function turned into a closure
66  */
67  Closure(const Scope_t& scope_ptr, PageAddr_t pa) noexcept;
68 
69  /**
70  * @brief Return the scope held by the object
71  *
72  * @return const Scope_t&
73  */
74  const Scope_t& scope() const noexcept;
75 
76  /**
77  * @brief Return a reference to the scpoe held by the object
78  *
79  * @return Scope_t&
80  */
81  Scope_t& refScope() noexcept;
82 
83  /**
84  * @brief Return the page address of the object
85  *
86  * @return PageAddr_t
87  */
88  inline PageAddr_t pageAddr() const { return m_page_addr; }
89 
90  /**
91  * @brief Print the closure to a string
92  *
93  * @param os
94  * @param vm
95  */
96  void toString(std::ostream& os, VM& vm) const noexcept;
97 
98  friend ARK_API_INLINE bool operator==(const Closure& A, const Closure& B) noexcept;
99  friend ARK_API_INLINE bool operator<(const Closure& A, const Closure& B) noexcept;
100 
101  private:
103  // keep track of the code page number, in case we need it later
105  };
106 
107  inline bool operator==(const Closure& A, const Closure& B) noexcept
108  {
109  return A.m_scope == B.m_scope;
110  }
111 
112  inline bool operator<(const Closure& A, const Closure& B) noexcept
113  {
114  return A.m_page_addr < B.m_page_addr;
115  }
116 }
117 
118 #endif
ArkScript configuration macros.
#define ARK_API_INLINE
Definition: Platform.hpp:51
The ArkScript virtual machine, executing ArkScript bytecode.
Definition: VM.hpp:47
Closure management.
Definition: Closure.hpp:45
friend ARK_API_INLINE bool operator==(const Closure &A, const Closure &B) noexcept
Definition: Closure.hpp:107
PageAddr_t pageAddr() const
Return the page address of the object.
Definition: Closure.hpp:88
PageAddr_t m_page_addr
Definition: Closure.hpp:104
const Scope_t & scope() const noexcept
Return the scope held by the object.
Definition: Closure.cpp:24
Scope_t & refScope() noexcept
Return a reference to the scpoe held by the object.
Definition: Closure.cpp:29
void toString(std::ostream &os, VM &vm) const noexcept
Print the closure to a string.
Definition: Closure.cpp:34
friend ARK_API_INLINE bool operator<(const Closure &A, const Closure &B) noexcept
Definition: Closure.hpp:112
Closure() noexcept
Construct a new Closure object.
Definition: Closure.cpp:9
std::shared_ptr< Scope > Scope_t
Scope handling.
Definition: Closure.hpp:37
uint16_t PageAddr_t
Definition: Closure.hpp:38
bool operator<(const Closure &A, const Closure &B) noexcept
Definition: Closure.hpp:112
bool operator==(const Closure &A, const Closure &B) noexcept
Definition: Closure.hpp:107
Definition: Builtins.hpp:21