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