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 
24 namespace Ark
25 {
26  /**
27  * @brief Ark state to handle the dirty job of loading and compiling ArkScript code
28  *
29  */
30  class ARK_API State
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 internal::Closure;
114  friend class Repl;
115 
116  private:
117  /**
118  * @brief Called to configure the state (set the bytecode, debug level, call the compiler...)
119  *
120  */
121  void configure();
122 
123  /**
124  * @brief Reads and compiles code of file
125  *
126  * @param file the path of file code to compile
127  * @param output set path of .arkc file
128  * @return true on success
129  * @return false on failure and raise an exception
130  */
131  bool compile(const std::string& file, const std::string& output);
132 
133  inline void throwStateError(const std::string& message)
134  {
135  throw std::runtime_error("StateError: " + message);
136  }
137 
138  unsigned m_debug_level;
139 
141  std::vector<std::string> m_libenv;
142  std::string m_filename;
143  uint16_t m_options;
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
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:140
std::string m_filename
Definition: State.hpp:142
std::vector< Value > m_constants
Definition: State.hpp:147
void throwStateError(const std::string &message)
Definition: State.hpp:133
uint16_t m_options
Definition: State.hpp:143
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:138
std::vector< std::string > m_libenv
Definition: State.hpp:141
The ArkScript virtual machine, executing ArkScript bytecode.
Definition: VM.hpp:47
Value(*)(std::vector< Value > &, VM *) ProcType
Definition: Value.hpp:74
Closure management.
Definition: Closure.hpp:45
Definition: Builtins.hpp:21
constexpr uint16_t DefaultFeatures
Definition: Constants.hpp:51
std::vector< uint8_t > bytecode_t
Definition: Common.hpp:22