ArkScript
A small, lisp-inspired, functional scripting language
State.hpp
Go to the documentation of this file.
1/**
2 * @file State.hpp
3 * @author Alexandre 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-2025
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
20#include <Ark/VM/Value.hpp>
23#include <Ark/Exceptions.hpp>
25
26namespace Ark
27{
28 /**
29 * @brief Ark state to handle the dirty job of loading and compiling ArkScript code
30 *
31 */
33 {
34 public:
35 /**
36 * @brief Construct a new State object
37 *
38 * @param libenv a list of search paths for the std library
39 */
40 explicit State(const std::vector<std::filesystem::path>& libenv = {}) noexcept;
41
42 /**
43 * @brief Feed the state by giving it the path to an existing bytecode file
44 *
45 * @param bytecode_filename
46 * @return true on success
47 * @return false on failure
48 */
49 bool feed(const std::string& bytecode_filename);
50
51 /**
52 * @brief Feed the state with ArkScript bytecode
53 *
54 * @param bytecode
55 * @return true on success
56 * @return false on failure
57 */
58 bool feed(const bytecode_t& bytecode);
59
60 /**
61 * @brief Compile a file, and use the resulting bytecode
62 *
63 * @param file path to an ArkScript code file
64 * @param features compiler features to enable/disable
65 * @return true on success
66 * @return false on failure
67 */
68 bool doFile(const std::string& file, uint16_t features = DefaultFeatures);
69
70 /**
71 * @brief Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode
72 *
73 * @param code the ArkScript code
74 * @param features compiler features to enable/disable
75 * @return true on success
76 * @return false on failure
77 */
78 bool doString(const std::string& code, uint16_t features = DefaultFeatures);
79
80 /**
81 * @brief Register a function in the virtual machine
82 *
83 * @param name the name of the function in ArkScript
84 * @param function the code of the function
85 */
86 void loadFunction(const std::string& name, Procedure::CallbackType&& function) noexcept;
87
88 /**
89 * @brief Set the script arguments in sys:args
90 *
91 * @param args
92 */
93 void setArgs(const std::vector<std::string>& args) noexcept;
94
95 /**
96 * @brief Set the debug level
97 *
98 * @param level between 0 (nothing) and 3 (maximum verbosity)
99 */
100 void setDebug(unsigned level) noexcept;
101
102 /**
103 * @brief Set the std search paths
104 *
105 * @param libenv the list of std search paths to set
106 */
107 void setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept;
108
109 /**
110 * @brief Reset State (all member variables related to execution)
111 *
112 */
113 void reset() noexcept;
114
115 friend class VM;
116 friend class internal::Closure;
117 friend class Repl;
118
119 private:
120 /**
121 * @brief Called to configure the state (set the bytecode, debug level, call the compiler...)
122 *
123 * @param bcr reference to a pre-fed bytecode reader
124 */
125 void configure(const BytecodeReader& bcr);
126
127 /**
128 * @brief Reads and compiles code of file
129 *
130 * @param file the path of file code to compile
131 * @param output set path of .arkc file
132 * @param features compiler features to enable/disable
133 * @return true on success
134 * @return false on failure and raise an exception
135 */
136 [[nodiscard]] bool compile(const std::string& file, const std::string& output, uint16_t features) const;
137
138 static void throwStateError(const std::string& message)
139 {
140 throw Error("StateError: " + message);
141 }
142
144
146 std::vector<std::filesystem::path> m_libenv;
147 std::string m_filename;
148
149 // related to the bytecode
150 std::vector<std::string> m_symbols;
151 std::vector<Value> m_constants;
152 std::vector<std::string> m_filenames;
153 std::vector<internal::InstLoc> m_inst_locations;
154 std::size_t m_max_page_size;
156
157 // related to the execution
158 std::unordered_map<std::string, Value> m_binded; ///< Values binded to the State, to be used by the VM
159
160 /**
161 * @brief Get an instruction in a given page, with a given instruction pointer
162 *
163 * @param pp page pointer
164 * @param ip instruction pointer
165 * @return uint8_t instruction
166 */
167 [[nodiscard]] inline constexpr uint8_t inst(const std::size_t pp, const std::size_t ip) const noexcept
168 {
169 return m_code[pp * m_max_page_size + ip];
170 }
171 };
172}
173
174#endif
Common code for the compiler.
Constants used by ArkScript.
ArkScript homemade exceptions.
#define ARK_API
Definition Module.hpp:28
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:33
std::vector< std::filesystem::path > m_libenv
Definition State.hpp:146
bytecode_t m_bytecode
Definition State.hpp:145
std::string m_filename
Definition State.hpp:147
std::vector< Value > m_constants
Definition State.hpp:151
static void throwStateError(const std::string &message)
Definition State.hpp:138
std::vector< internal::InstLoc > m_inst_locations
Definition State.hpp:153
std::vector< std::string > m_filenames
Definition State.hpp:152
std::unordered_map< std::string, Value > m_binded
Values binded to the State, to be used by the VM.
Definition State.hpp:158
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:167
std::vector< std::string > m_symbols
Definition State.hpp:150
bytecode_t m_code
Definition State.hpp:155
std::size_t m_max_page_size
Definition State.hpp:154
unsigned m_debug_level
Definition State.hpp:143
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
Closure management.
Definition Closure.hpp:36
constexpr uint16_t DefaultFeatures
Definition Constants.hpp:59
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22