ArkScript
A small, fast, functional and scripting language for video games
State.hpp
Go to the documentation of this file.
1/**
2 * @file State.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief State used by the virtual machine: it loads the bytecode, can compile it if needed, load C++ functions...
5 * @version 0.4
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef ARK_VM_STATE_HPP
13#define ARK_VM_STATE_HPP
14
15#include <string>
16#include <vector>
17#include <unordered_map>
18#include <filesystem>
19#include <Ark/Constants.hpp>
20
21#include <Ark/VM/Value.hpp>
23#include <Ark/Exceptions.hpp>
24
25namespace Ark
26{
27 /**
28 * @brief Ark state to handle the dirty job of loading and compiling ArkScript code
29 *
30 */
32 {
33 public:
34 /**
35 * @brief Construct a new State object
36 *
37 * @param libenv a list of search paths for the std library
38 */
39 explicit State(const std::vector<std::filesystem::path>& libenv = {}) noexcept;
40
41 /**
42 * @brief Feed the state by giving it the path to an existing bytecode file
43 *
44 * @param bytecode_filename
45 * @return true on success
46 * @return false on failure
47 */
48 bool feed(const std::string& bytecode_filename);
49
50 /**
51 * @brief Feed the state with ArkScript bytecode
52 *
53 * @param bytecode
54 * @return true on success
55 * @return false on failure
56 */
57 bool feed(const bytecode_t& bytecode);
58
59 /**
60 * @brief Compile a file, and use the resulting bytecode
61 *
62 * @param file path to an ArkScript code file
63 * @param features compiler features to enable/disable
64 * @return true on success
65 * @return false on failure
66 */
67 bool doFile(const std::string& file, uint16_t features = DefaultFeatures);
68
69 /**
70 * @brief Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode
71 *
72 * @param code the ArkScript code
73 * @param features compiler features to enable/disable
74 * @return true on success
75 * @return false on failure
76 */
77 bool doString(const std::string& code, uint16_t features = DefaultFeatures);
78
79 /**
80 * @brief Register a function in the virtual machine
81 *
82 * @param name the name of the function in ArkScript
83 * @param function the code of the function
84 */
85 void loadFunction(const std::string& name, Value::ProcType function) noexcept;
86
87 /**
88 * @brief Set the script arguments in sys:args
89 *
90 * @param args
91 */
92 void setArgs(const std::vector<std::string>& args) noexcept;
93
94 /**
95 * @brief Set the debug level
96 *
97 * @param level between 0 (nothing) and 3 (maximum verbosity)
98 */
99 void setDebug(unsigned level) noexcept;
100
101 /**
102 * @brief Set the std search paths
103 *
104 * @param libenv the list of std search paths to set
105 */
106 void setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept;
107
108 /**
109 * @brief Reset State (all member variables related to execution)
110 *
111 */
112 void reset() noexcept;
113
114 friend class VM;
115 friend class internal::Closure;
116 friend class Repl;
117
118 private:
119 /**
120 * @brief Called to configure the state (set the bytecode, debug level, call the compiler...)
121 *
122 * @param bcr reference to a pre-fed bytecode reader
123 */
124 void configure(const BytecodeReader& bcr);
125
126 /**
127 * @brief Reads and compiles code of file
128 *
129 * @param file the path of file code to compile
130 * @param output set path of .arkc file
131 * @param features compiler features to enable/disable
132 * @return true on success
133 * @return false on failure and raise an exception
134 */
135 bool compile(const std::string& file, const std::string& output, uint16_t features) const;
136
137 static void throwStateError(const std::string& message)
138 {
139 throw Error("StateError: " + message);
140 }
141
143
145 std::vector<std::filesystem::path> m_libenv;
146 std::string m_filename;
147
148 // related to the bytecode
149 std::vector<std::string> m_symbols;
150 std::vector<Value> m_constants;
151 std::vector<bytecode_t> m_pages;
152
153 // related to the execution
154 std::unordered_map<std::string, Value> m_binded;
155 };
156}
157
158#endif
Common code for the compiler.
Constants used by ArkScript.
ArkScript homemade exceptions.
#define ARK_API
Definition Module.hpp:28
This class is just a helper to.
Ark state to handle the dirty job of loading and compiling ArkScript code.
Definition State.hpp:32
std::vector< std::filesystem::path > m_libenv
Definition State.hpp:145
bytecode_t m_bytecode
Definition State.hpp:144
std::string m_filename
Definition State.hpp:146
std::vector< Value > m_constants
Definition State.hpp:150
static void throwStateError(const std::string &message)
Definition State.hpp:137
std::unordered_map< std::string, Value > m_binded
Definition State.hpp:154
std::vector< std::string > m_symbols
Definition State.hpp:149
std::vector< bytecode_t > m_pages
Definition State.hpp:151
unsigned m_debug_level
Definition State.hpp:142
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:44
Value(*)(std::vector< Value > &, VM *) ProcType
Definition Value.hpp:62
Closure management.
Definition Closure.hpp:37
constexpr uint16_t DefaultFeatures
Definition Constants.hpp:60
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22