TorqueScript  0.2
interpreter.hpp
1 
15 #pragma once
16 
17 #include <vector>
18 #include <memory>
19 #include <unordered_map>
20 
21 #include <torquescript/interpreterconfiguration.hpp>
22 #include <torquescript/function.hpp>
23 #include <torquescript/consoleobject.hpp>
24 #include <torquescript/storedvalue.hpp>
25 #include <torquescript/stringhelpers.hpp>
26 #include <torquescript/stringtable.hpp>
27 #include <torquescript/functionregistry.hpp>
28 #include <torquescript/storedvaluestack.hpp>
29 #include <torquescript/consoleobjectregistry.hpp>
30 
31 #define NAMESPACE_EMPTY ""
32 #define PACKAGE_EMPTY ""
33 
34 namespace TorqueScript
35 {
37  class Compiler;
38  class CodeBlock;
39  class ExecutionState;
40 
46  {
47  public:
53  Interpreter();
54 
60  ~Interpreter();
61 
67  void setGlobal(const std::size_t name, StoredValue value);
68  void setGlobal(const std::string& name, StoredValue value);
69 
77  StoredValue* getGlobal(const std::string& name);
78 
87  StoredValue* getGlobal(const std::size_t name);
88 
90 
96  CodeBlock* compile(const std::string& input);
97  void evaluate(const std::string& input, std::shared_ptr<ExecutionState> state = nullptr);
98  void execute(const std::string& path, std::shared_ptr<ExecutionState> state = nullptr);
99 
104 
109  void addFunction(std::shared_ptr<Function> function);
110  std::shared_ptr<Function> getFunction(const std::string& space, const std::string& name);
111  std::shared_ptr<Function> getFunctionParent(Function* function);
112 
113  FunctionRegistry* findFunctionRegistry(const std::string packageName);
114  void addFunctionRegistry(const std::string& packageName);
115  void activateFunctionRegistry(const std::string& packageName);
116  void deactivateFunctionRegistry(const std::string& packageName);
117  void removeFunctionRegistry(const std::string& packageName);
119 
120  std::shared_ptr<ExecutionState> getExecutionState();
121 
124 
127 
130 
140  std::shared_ptr<ConsoleObject> initializeConsoleObjectTree(ObjectInstantiationDescriptor& descriptor);
141 
142  template <typename classType>
143  void registerConsoleObjectType()
144  {
145  // Ensure descriptors are initialized
146  classType::initializeMemberFields(TypeInformation<classType>::Descriptor);
147  }
148 
149  private:
151  Compiler* mCompiler;
152 
154  std::vector<FunctionRegistry> mFunctionRegistries;
155 
157  std::unordered_map<std::size_t, StoredValue> mGlobalVariables;
158  };
159 }
A CodeBlock defines a piece of executable code generated from a single input (Ie. a file)....
Definition: codeblock.hpp:37
Primary compiler class. This class is an AST visitor that walks the AST tree generated by a AST::ASTB...
Definition: compiler.hpp:35
Definition: consoleobjectregistry.hpp:38
A function is callable subroutine from anywhere in the language, defined by a script....
Definition: function.hpp:40
The interpreter class represents a high level instance of the TorqueScript interpreter....
Definition: interpreter.hpp:46
std::shared_ptr< ConsoleObject > initializeConsoleObjectTree(ObjectInstantiationDescriptor &descriptor)
Initializes the tree of objects described by the provided descriptor.
const InterpreterConfiguration mConfig
The interpreter configuration.
Definition: interpreter.hpp:129
ConsoleObjectRegistry mConsoleObjectRegistry
The ConsoleObjectRegistry associated with this interpreter. It is used to store all ConsoleObject ins...
Definition: interpreter.hpp:123
StoredValue * getGlobal(const std::string &name)
Retrieves a global variable by string.
StoredValue * getGlobal(const std::size_t name)
Retrieves a global variable by string ID.
CodeBlock * compile(const std::string &input)
Ask the interpreter to compile the input string and return the resulting CodeBlock.
StringTable mStringTable
The string table associated with this interpreter.
Definition: interpreter.hpp:126
void addFunction(std::shared_ptr< Function > function)
Registers a new function to the interpreter.
Interpreter(const InterpreterConfiguration &config)
Constructs a new interpreter with the specified configuration.
Storage class used to keep variable values in-memory of arbitrary data types. The data types supporte...
Definition: storedvalue.hpp:80
A string table maintains a mapping of numeric identifiers to their raw string values....
Definition: stringtable.hpp:32
Definition: ast.hpp:28
A FunctionRegistry is used to keep track of registered functions in memory by the packages they are a...
Definition: functionregistry.hpp:33
A structure representing overall interpreter runtime configuration. Some settings can be changed at r...
Definition: interpreterconfiguration.hpp:26
Struct describing a tree of console object initializations.
Definition: executionscope.hpp:48
Definition: consoleobject.hpp:101