ArkScript
A small, lisp-inspired, functional scripting language
State.hpp
Go to the documentation of this file.
1/**
2 * @file State.hpp
3 * @author Lexy Plateau (lexplt.dev@gmail.com)
4 * @brief State used by the virtual machine: it loads the bytecode, can compile it if needed, load C++ functions...
5 * @date 2020-10-27
6 *
7 * @copyright Copyright (c) 2020-2026
8 *
9 */
10
11#ifndef ARK_VM_STATE_HPP
12#define ARK_VM_STATE_HPP
13
14#include <string>
15#include <vector>
16#include <unordered_map>
17#include <filesystem>
18#include <Ark/Constants.hpp>
19
25
26namespace Ark
27{
28 namespace internal
29 {
30 class Debugger;
31 }
32
33 /**
34 * @brief Ark state to handle the dirty job of loading and compiling ArkScript code
35 *
36 */
38 {
39 public:
40 /**
41 * @brief Construct a new State object
42 *
43 * @param libenv a list of search paths for the std library
44 */
45 explicit State(const std::vector<std::filesystem::path>& libenv = {}) noexcept;
46
47 /**
48 * @brief Feed the state by giving it the path to an existing bytecode file
49 *
50 * @param bytecode_filename
51 * @param fail_with_exception
52 * @return true on success
53 * @return false on failure
54 */
55 bool feed(const std::string& bytecode_filename, bool fail_with_exception = false);
56
57 /**
58 * @brief Feed the state with ArkScript bytecode
59 *
60 * @param bytecode
61 * @param fail_with_exception
62 * @return true on success
63 * @return false on failure
64 */
65 bool feed(const bytecode_t& bytecode, bool fail_with_exception = false);
66
67 /**
68 * @brief Compile a file, and use the resulting bytecode
69 *
70 * @param file_path path to an ArkScript code file
71 * @param features compiler features to enable/disable
72 * @param stream optional output stream for the logger
73 * @return true on success
74 * @return false on failure
75 */
76 bool doFile(const std::string& file_path, uint16_t features = DefaultFeatures, std::ostream* stream = nullptr);
77
78 /**
79 * @brief Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode
80 *
81 * @param code the ArkScript code
82 * @param features compiler features to enable/disable
83 * @param stream optional output stream for the logger
84 * @return true on success
85 * @return false on failure
86 */
87 bool doString(const std::string& code, uint16_t features = DefaultFeatures, std::ostream* stream = nullptr);
88
89 /**
90 * @brief Register a function in the virtual machine
91 *
92 * @param name the name of the function in ArkScript
93 * @param function the code of the function
94 */
95 void loadFunction(const std::string& name, Procedure::CallbackType&& function) noexcept;
96
97 /**
98 * @brief Set the script arguments in sys:args
99 *
100 * @param args
101 */
102 void setArgs(const std::vector<std::string>& args) noexcept;
103
104 /**
105 * @brief Set the debug level
106 *
107 * @param level between 0 (nothing) and 3 (maximum verbosity)
108 */
109 void setDebug(unsigned level) noexcept;
110
111 /**
112 * @brief Set the std search paths
113 *
114 * @param libenv the list of std search paths to set
115 */
116 void setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept;
117
118 /**
119 * @brief Reset State (all member variables related to execution)
120 *
121 */
122 void reset() noexcept;
123
124 /**
125 * @brief Used by the debugger to add code to the VM at runtime
126 *
127 * @param pages
128 * @param symbols
129 * @param constants
130 */
131 void extendBytecode(const std::vector<bytecode_t>& pages, const std::vector<std::string>& symbols, const std::vector<Value>& constants);
132
133 friend class VM;
134 friend class Repl;
135 friend class internal::Closure;
136 friend class internal::Debugger;
137
138 private:
139 /**
140 * @brief Called to configure the state (set the bytecode, debug level, call the compiler...)
141 *
142 * @param bcr reference to a pre-fed bytecode reader
143 */
144 void configure(const BytecodeReader& bcr);
145
146 /**
147 * @brief Reads and compiles code of file
148 *
149 * @param file the path of file code to compile
150 * @param output set path of .arkc file
151 * @param stream output stream for the logger
152 * @return true on success
153 * @return false on failure and raise an exception
154 */
155 [[nodiscard]] bool compile(const std::string& file, const std::string& output, std::ostream* stream = nullptr);
156
157 static void throwStateError(const std::string& message)
158 {
159 throw Error("StateError: " + message);
160 }
161
163 uint16_t m_features;
164
166 std::vector<std::filesystem::path> m_libenv;
167 std::string m_filename;
168
169 // related to the bytecode
170 std::vector<std::string> m_symbols;
171 std::vector<Value> m_constants;
172 std::vector<std::string> m_filenames;
173 std::vector<internal::InstLoc> m_inst_locations;
174 std::vector<bytecode_t> m_pages;
175 std::size_t m_max_page_size;
177
178 // related to the execution
179 std::unordered_map<std::string, Value> m_bound; ///< Values bound to the State, to be used by the VM
180
181 void addPagesToContiguousBytecode(const std::vector<bytecode_t>& pages, std::size_t start);
182
183 /**
184 * @brief Compute the maximum length of the given code pages
185 *
186 * @param pages
187 * @return std::size_t
188 */
189 static std::size_t maxPageSize(const std::vector<bytecode_t>& pages);
190
191 /**
192 * @brief Get an instruction in a given page, with a given instruction pointer
193 *
194 * @param pp page pointer
195 * @param ip instruction pointer
196 * @return uint8_t instruction
197 */
198 [[nodiscard]] inline constexpr uint8_t inst(const std::size_t pp, const std::size_t ip) const noexcept
199 {
200 return m_code[pp * m_max_page_size + ip];
201 }
202 };
203}
204
205#endif
Common code for the compiler.
Constants used by ArkScript.
ArkScript homemade exceptions.
#define ARK_API
Definition Module.hpp:22
Wrapper object for user-defined functions.
Default value type handled by the virtual machine.
This class is just a helper to.
std::function< Value(std::vector< Value > &, VM *)> CallbackType
Definition Procedure.hpp:29
Ark state to handle the dirty job of loading and compiling ArkScript code.
Definition State.hpp:38
std::vector< std::filesystem::path > m_libenv
Definition State.hpp:166
uint16_t m_features
Definition State.hpp:163
std::unordered_map< std::string, Value > m_bound
Values bound to the State, to be used by the VM.
Definition State.hpp:179
bytecode_t m_bytecode
Definition State.hpp:165
std::string m_filename
Definition State.hpp:167
std::vector< Value > m_constants
Definition State.hpp:171
static void throwStateError(const std::string &message)
Definition State.hpp:157
std::vector< internal::InstLoc > m_inst_locations
Definition State.hpp:173
std::vector< std::string > m_filenames
Definition State.hpp:172
constexpr uint8_t inst(const std::size_t pp, const std::size_t ip) const noexcept
Get an instruction in a given page, with a given instruction pointer.
Definition State.hpp:198
std::vector< std::string > m_symbols
Definition State.hpp:170
bytecode_t m_code
Definition State.hpp:176
std::size_t m_max_page_size
Definition State.hpp:175
std::vector< bytecode_t > m_pages
Definition State.hpp:174
unsigned m_debug_level
Definition State.hpp:162
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:48
Closure management.
Definition Closure.hpp:35
constexpr uint16_t DefaultFeatures
Definition Constants.hpp:67
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22