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 (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 * @version 0.2
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2021
9 *
10 */
11
12#ifndef ARK_VM_STATE_HPP
13#define ARK_VM_STATE_HPP
14
15#include <string>
16#include <vector>
17#include <cinttypes>
18#include <unordered_map>
19
20#include <Ark/VM/Value.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 options the options for the virtual machine, compiler, and parser
37 * @param libpath a list of search paths for the std library
38 */
39 State(uint16_t options = DefaultFeatures, const std::vector<std::string>& libpath = {}) 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 filename path to an ArkScript code file
63 * @return true on success
64 * @return false on failure
65 */
66 bool doFile(const std::string& filename);
67
68 /**
69 * @brief Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode
70 *
71 * @param code the ArkScript code
72 * @return true on success
73 * @return false on failure
74 */
75 bool doString(const std::string& code);
76
77 /**
78 * @brief Register a function in the virtual machine
79 *
80 * @param name the name of the function in ArkScript
81 * @param function the code of the function
82 */
83 void loadFunction(const std::string& name, Value::ProcType function) noexcept;
84
85 /**
86 * @brief Set the script arguments in sys:args
87 *
88 * @param args
89 */
90 void setArgs(const std::vector<std::string>& args) noexcept;
91
92 /**
93 * @brief Set the debug level
94 *
95 * @param level between 0 (nothing) and 3 (maximum verbosity)
96 */
97 void setDebug(unsigned level) noexcept;
98
99 /**
100 * @brief Set the std search paths
101 *
102 * @param libenv the list of std search paths to set
103 */
104 void setLibDirs(const std::vector<std::string>& libenv) noexcept;
105
106 /**
107 * @brief Reset State (all member variables related to execution)
108 *
109 */
110 void reset() noexcept;
111
112 friend class VM;
113 friend class Repl;
114
115 private:
116 /**
117 * @brief Called to configure the state (set the bytecode, debug level, call the compiler...)
118 *
119 */
120 void configure();
121
122 /**
123 * @brief Reads and compiles code of file
124 *
125 * @param file the path of file code to compile
126 * @param output set path of .arkc file
127 * @return true on success
128 * @return false on failure and raise an exception
129 */
130 bool compile(const std::string& file, const std::string& output);
131
132 inline void throwStateError(const std::string& message)
133 {
134 throw std::runtime_error("StateError: " + message);
135 }
136
138
140 std::vector<std::string> m_libenv;
141 std::string m_filename;
142 uint16_t m_options;
143
144 // related to the bytecode
145 std::vector<std::string> m_symbols;
146 std::vector<Value> m_constants;
147 std::vector<bytecode_t> m_pages;
148
149 // related to the execution
150 std::unordered_map<std::string, Value> m_binded;
151 };
152}
153
154#endif
A bytecode disassembler for ArkScript.
ArkScript compiler is in charge of transforming the AST into bytecode.
#define ARK_API
Definition: Platform.hpp:41
Ark state to handle the dirty job of loading and compiling ArkScript code.
Definition: State.hpp:31
bytecode_t m_bytecode
Definition: State.hpp:139
std::string m_filename
Definition: State.hpp:141
std::vector< Value > m_constants
Definition: State.hpp:146
void throwStateError(const std::string &message)
Definition: State.hpp:132
uint16_t m_options
Definition: State.hpp:142
std::unordered_map< std::string, Value > m_binded
Definition: State.hpp:150
std::vector< std::string > m_symbols
Definition: State.hpp:145
std::vector< bytecode_t > m_pages
Definition: State.hpp:147
unsigned m_debug_level
Definition: State.hpp:137
std::vector< std::string > m_libenv
Definition: State.hpp:140
The ArkScript virtual machine, executing ArkScript bytecode.
Definition: VM.hpp:46
Value(*)(std::vector< Value > &, VM *) ProcType
Definition: Value.hpp:74
Definition: Builtins.hpp:21
constexpr uint16_t DefaultFeatures
Definition: Constants.hpp:51
std::vector< uint8_t > bytecode_t
Definition: Common.hpp:22