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.3
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
20#include <Ark/VM/Value.hpp>
22#include <Ark/Exceptions.hpp>
23
24namespace Ark
25{
26 /**
27 * @brief Ark state to handle the dirty job of loading and compiling ArkScript code
28 *
29 */
31 {
32 public:
33 /**
34 * @brief Construct a new State object
35 *
36 * @param libpath a list of search paths for the std library
37 */
38 explicit State(const std::vector<std::filesystem::path>& libpath = {}) noexcept;
39
40 /**
41 * @brief Feed the state by giving it the path to an existing bytecode file
42 *
43 * @param bytecode_filename
44 * @return true on success
45 * @return false on failure
46 */
47 bool feed(const std::string& bytecode_filename);
48
49 /**
50 * @brief Feed the state with ArkScript bytecode
51 *
52 * @param bytecode
53 * @return true on success
54 * @return false on failure
55 */
56 bool feed(const bytecode_t& bytecode);
57
58 /**
59 * @brief Compile a file, and use the resulting bytecode
60 *
61 * @param filename path to an ArkScript code file
62 * @return true on success
63 * @return false on failure
64 */
65 bool doFile(const std::string& filename);
66
67 /**
68 * @brief Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode
69 *
70 * @param code the ArkScript code
71 * @return true on success
72 * @return false on failure
73 */
74 bool doString(const std::string& code);
75
76 /**
77 * @brief Register a function in the virtual machine
78 *
79 * @param name the name of the function in ArkScript
80 * @param function the code of the function
81 */
82 void loadFunction(const std::string& name, Value::ProcType function) noexcept;
83
84 /**
85 * @brief Set the script arguments in sys:args
86 *
87 * @param args
88 */
89 void setArgs(const std::vector<std::string>& args) noexcept;
90
91 /**
92 * @brief Set the debug level
93 *
94 * @param level between 0 (nothing) and 3 (maximum verbosity)
95 */
96 void setDebug(unsigned level) noexcept;
97
98 /**
99 * @brief Set the std search paths
100 *
101 * @param libenv the list of std search paths to set
102 */
103 void setLibDirs(const std::vector<std::filesystem::path>& libenv) noexcept;
104
105 /**
106 * @brief Reset State (all member variables related to execution)
107 *
108 */
109 void reset() noexcept;
110
111 friend class VM;
112 friend class internal::Closure;
113 friend class Repl;
114
115 private:
116 static bool checkMagic(const bytecode_t& bytecode);
117
118 /**
119 * @brief Called to configure the state (set the bytecode, debug level, call the compiler...)
120 *
121 */
122 void configure();
123
124 /**
125 * @brief Reads and compiles code of file
126 *
127 * @param file the path of file code to compile
128 * @param output set path of .arkc file
129 * @return true on success
130 * @return false on failure and raise an exception
131 */
132 bool compile(const std::string& file, const std::string& output);
133
134 static void throwStateError(const std::string& message)
135 {
136 throw Error("StateError: " + message);
137 }
138
140
142 std::vector<std::filesystem::path> m_libenv;
143 std::string m_filename;
144
145 // related to the bytecode
146 std::vector<std::string> m_symbols;
147 std::vector<Value> m_constants;
148 std::vector<bytecode_t> m_pages;
149
150 // related to the execution
151 std::unordered_map<std::string, Value> m_binded;
152 };
153}
154
155#endif
Common code for the compiler.
ArkScript homemade exceptions.
#define ARK_API
Definition: Module.hpp:28
Ark state to handle the dirty job of loading and compiling ArkScript code.
Definition: State.hpp:31
std::vector< std::filesystem::path > m_libenv
Definition: State.hpp:142
bytecode_t m_bytecode
Definition: State.hpp:141
std::string m_filename
Definition: State.hpp:143
std::vector< Value > m_constants
Definition: State.hpp:147
static void throwStateError(const std::string &message)
Definition: State.hpp:134
std::unordered_map< std::string, Value > m_binded
Definition: State.hpp:151
std::vector< std::string > m_symbols
Definition: State.hpp:146
std::vector< bytecode_t > m_pages
Definition: State.hpp:148
unsigned m_debug_level
Definition: State.hpp:139
The ArkScript virtual machine, executing ArkScript bytecode.
Definition: VM.hpp:41
Value(*)(std::vector< Value > &, VM *) ProcType
Definition: Value.hpp:63
Closure management.
Definition: Closure.hpp:37
Definition: Builtins.hpp:21
std::vector< uint8_t > bytecode_t
Definition: Common.hpp:21