 |
Gorgon Game Engine
|
Go to the documentation of this file.
9 #include "../Containers/Collection.h"
10 #include "../Containers/Hashmap.h"
49 explicit VirtualMachine(
bool automaticreset=
true, std::ostream &out=std::cout, std::istream &in=std::cin);
65 void Run(std::shared_ptr<ScopeInstance> scope);
69 void Run(
unsigned executiontarget);
94 void Jump(
unsigned long line);
105 Data FindSymbol(
const std::string &original,
bool reference=
false,
bool allownull=
false);
110 if(!activevms.Exists(std::this_thread::get_id())) {
111 throw std::runtime_error(
"No active VMs for this thread.");
114 return activevms[std::this_thread::get_id()];
119 return activevms.Exists(std::this_thread::get_id());
139 void SetOutput(std::ostream &out,
bool deleteonchange=
false);
156 if(deleteoutonchange)
delete output;
157 deleteoutonchange=
false;
170 activevms.Add(std::this_thread::get_id(),
this);
176 return (
unsigned)scopeinstances.size();
181 return *scopeinstances.back();
186 return scopeinstances.front()->GetMarkerForNext();
197 if(scopeinstances.size()==0) {
198 throw std::runtime_error(
"No scope instance to return from.");
200 scopeinstances.back()->ReturnValue=value;
201 returnimmediately=
true;
226 Data callfunction(
const Function *fn,
bool method,
const std::vector<Value> ¶ms);
228 Data getvalue(
const Value &val,
bool reference=
false);
229 void functioncall(
const Instruction *inst,
bool memberonly,
bool method);
230 void activatescopeinstance(std::shared_ptr<ScopeInstance> instance);
236 std::string alllibnames;
238 bool returnimmediately=
false;
241 std::function<
Data(
char, std::string)> spechandler;
250 std::vector<std::shared_ptr<ScopeInstance>> scopeinstances;
258 std::ostream *output;
261 std::ostream *defoutput;
262 std::istream *definput;
264 bool deleteoutonchange=
false;
266 std::vector<Data> temporaries;
268 std::shared_ptr<ScopeInstance> toplevel;
Data FindSymbol(const std::string &original, bool reference=false, bool allownull=false)
Finds the given symbol and resolves its value.
Definition: VirtualMachine.cpp:197
Library Math("Math", "Maths library.")
Definition: VirtualMachine.h:41
static bool Exists()
Returns the current VM for this thread.
Definition: VirtualMachine.h:118
unsigned GetScopeInstanceCount() const
Returns the number of active execution scopes.
Definition: VirtualMachine.h:175
virtual std::string ToString(const Data &) const =0
Converts a data of this type to string.
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
void Reset()
Resets any runtime information that this VM has.
constexpr Type Data
Data resource.
Definition: GID.h:164
@ FunctionCall
Marks instruction as a regular function call.
const InstanceMemberList & InstanceMembers
Definition: Reflection.h:1410
This is an instantiation of a scope.
Definition: Scope.h:223
void SetLiteral(const Scripting::Type *type, Any value)
Definition: Instruction.h:95
void Register(const Data &data)
Registers a new object of reference counting, this will set reference count to one.
Definition: Runtime.h:24
void LongJump(SourceMarker marker)
Changes current executing line.
This class is a reference based hashmap.
Definition: Hashmap.h:35
This class allows references to be counted and destroyed properly.
Definition: Runtime.h:19
std::enable_if< std::is_pointer< T_ >::value, T_ >::type ReferenceValue() const
Returns the value of this data in the requested format.
Definition: Data.h:112
A new scope is created automatically when a new input source or a function like construct is created.
Definition: Scope.h:62
This class can hold any other information providing type erasure.
Definition: Any.h:32
ReferenceCounter References
This system allows objects of automatic lifetime.
Definition: VirtualMachine.h:222
std::enable_if<!std::is_pointer< T_ >::value, const typename std::remove_reference< T_ >::type & >::type GetValue() const
Returns the value of this data in the requested format.
Definition: Data.h:57
Data describes a piece of data.
Definition: Data.h:22
constexpr RGBA Black
Definition: Color.h:653
uintptr_t GetSource() const
Definition: Scope.h:44
const Containers::Hashmap< std::string, const Type, nullptr, std::map, String::CaseInsensitiveLess > & InheritedSymbols
Inherited symbols.
Definition: Reflection.h:1408
void SetOutput(std::ostream &out, bool deleteonchange=false)
Redirects the output stream to the given stream.
Definition: VirtualMachine.cpp:50
const StaticMemberList & Members
List of static members.
Definition: Reflection.h:1148
void RemoveLibrary(const Library &library)
Removes a library.
Definition: VirtualMachine.cpp:40
void AddLibrary(const Library &library)
Includes a new library to be used in this virtual machine.
Definition: VirtualMachine.cpp:30
bool IsVariableSet(const std::string &name)
Definition: VirtualMachine.cpp:401
void SetReferenceable(const Data &value)
Sets the data contained in this variable.
Definition: Runtime.cpp:15
const Scripting::Type & InstanceMember()
Variable GetVariable(const std::string &name)
Definition: VirtualMachine.cpp:284
@ Assignment
Marks instruction as an assignment.
static VirtualMachine & Get()
Returns the current VM for this thread.
Definition: VirtualMachine.h:109
bool IsReferenceType() const
Returns whether this type is a reference type.
Definition: Reflection.h:1327
void ASSERT_FALSE(const std::string &message, int skip=1, int depth=4)
Definition: Assert.h:192
const Scripting::Type & Namespace()
Definition: Reflection.h:315
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Library Keywords
Definition: VirtualMachine.h:41
void AttachCommandConsole()
Creates a new InputSource using a console input provider.
This class uniquely represents a source code line.
Definition: Scope.h:30
This file contains classes that supports functions that are defined in runtime.
VirtualMachine(bool automaticreset=true, std::ostream &out=std::cout, std::istream &in=std::cin)
Default constructor.
Definition: VirtualMachine.cpp:17
void Set(Any value)
Sets the data contained in this variable without changing its type.
Definition: Runtime.h:169
This class stores information about types.
Definition: Reflection.h:1165
Type * ParameterTemplateType()
Definition: Runtime.cpp:8
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
This class represents a library.
Definition: Reflection.h:1596
SymbolType type
Type of the symbol.
Definition: VirtualMachine.h:32
void SetSpecialIdentifierHandler(std::function< Data(char, std::string)> handler)
Sets the handler for special identifiers.
Definition: VirtualMachine.h:207
std::string Name
Used for variables and constants.
Definition: Instruction.h:84
Namespace contains other static members as members.
Definition: Reflection.h:1073
virtual void Assign(Data &l, const Data &r) const =0
Assigns the value of the second parameter to the first, reference types can ignore this function.
void Activate()
Activate this VM for this thread.
Definition: VirtualMachine.h:169
bool IsReference() const
Returns if this data contains a reference.
Definition: Data.cpp:212
This is the base class for all static members.
Definition: Reflection.h:365
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
static Data Invalid()
Constructs an invalid data object.
Definition: Data.h:27
Collection is a container for reference typed objects.
Definition: Collection.h:21
Data Literal
Used for literal values.
Definition: Instruction.h:87
std::string ToLower(std::string str)
Converts the given string to lowercase.
Definition: String.h:416
std::vector< Any > OptionList
Definition: Reflection.h:114
Represents a symbol, can be a variable, type, function or constant.
Definition: VirtualMachine.h:26
const Scripting::Type & Library()
void UsingNamespace(const Namespace &name)
Imports symbols of a namespace to the list of global symbols.
Definition: VirtualMachine.cpp:1432
@ ReferenceTag
Marks the object as a reference.
Definition: Reflection.h:61
void SetInput(std::istream &in)
Redirects input stream to the given stream.
Definition: VirtualMachine.cpp:57
~VirtualMachine()
Definition: VirtualMachine.h:51
@ Function
Function, functions can also be represented as data members.
Definition: Reflection.h:385
std::string Extract(std::string &original, const std::string &marker, bool trim=false)
Extracts the part of the string up to the given marker.
Definition: String.h:779
bool DetachCommandConsole()
If there is an attached command console, this function detaches that console, stops execution and ret...
This class contains a parsed value.
Definition: Instruction.h:82
Data FindSymbol(const std::string &name, bool reference)
Tries to find the given symbol (including variables)
Definition: Scope.h:364
Byte Result
Used for temporary results.
Definition: Instruction.h:93
Represents a function overload.
Definition: Reflection.h:588
void ResetInput()
Resets the input stream to default stream that is given in the constructor.
Definition: VirtualMachine.h:163
@ ConstTag
Marks a parameter or a function constant.
Definition: Reflection.h:95
void init_builtin()
Definition: Builtin.cpp:113
std::string Name() const
Return the name of a known GID.
Definition: GID.h:283
Any GetData() const
Returns the data contained in this data element.
Definition: Data.h:133
const Type & GetType(const std::string &name) const
Convenience function, returns the type with the given name.
Definition: Reflection.cpp:395
unsigned long GetLine() const
Definition: Scope.h:46
void SetVariable(const std::string &name, Data data, bool ref=false)
Definition: VirtualMachine.cpp:328
This file contains classes that stores runtime and end programmer defined objects.
@ MethodCall
Marks this instruction as a method call.
void Return(Data value=Data::Invalid())
Returns from the currently running script and sets return data to the given value.
Definition: VirtualMachine.h:196
@ Literal
This is a literal value.
const Containers::Collection< const Library > & Libraries
Allows read-only access to libraries.
Definition: VirtualMachine.h:219
Type
Definition: Shader.h:14
void * Pointer() const
Returns the pointer without type information.
Definition: Any.h:332
void ResetOutput()
Resets the output stream to default stream that is given in the constructor.
Definition: VirtualMachine.h:155
Any object
The object.
Definition: VirtualMachine.h:35
void Run()
This method starts the virtual machine.
Definition: VirtualMachine.cpp:95
Library Reflection
Definition: Builtin.cpp:17
SourceMarker GetMarkerForNext() const
Returns the code marker for the next line.
Definition: VirtualMachine.h:185
Data GetReturnValue() const
Returns the data returned from the last executed script.
Definition: VirtualMachine.h:191
Library & FilesystemLib()
Definition: Filesystem.cpp:49
Library Integrals
This library requires Initialize to be called.
Definition: VirtualMachine.h:41
bool IsConstant() const
Returns if this data is constant.
Definition: Data.h:160
@ Identifier
Marks this value as an identifier, either a constant or a variable.
void Start(InputProvider &input)
This method starts the virtual machine with the given input source.
Definition: VirtualMachine.cpp:61
Exceptions This file contains string related exceptions.
std::istream & GetInput() const
Returns the input stream.
Definition: VirtualMachine.h:150
@ Unknown
This value is not valid.
@ Variable
This is a variable.
void Jump(unsigned long line)
Changes the current executing line.
void UnsetVariable(const std::string &name)
Definition: VirtualMachine.cpp:395
const Scripting::Type & Function()
Definition: Reflection.h:589
ValueType Type
Type of this value.
Definition: Instruction.h:90
bool ExecuteStatement(const std::string &code, InputProvider::Dialect dialect=InputProvider::Programming)
Executes a single statement in this virtual machine.
Definition: VirtualMachine.cpp:1413
@ Jump
Unconditionally jumps by the given offset. Offset should be in JumpOffset field.
Represents a function.
Definition: Reflection.h:557
std::ostream & GetOutput() const
Returns the output stream.
Definition: VirtualMachine.h:145
const Scripting::Type & Type()
Definition: Reflection.h:316
MorphType CanMorphTo(const Type &type) const
Check if it is possible to morph this type to the other.
Definition: Reflection.cpp:214
void Begin(InputProvider &input)
This method begins a new execution scope without starting execution.
Definition: VirtualMachine.cpp:67
Variable * getvarref(const std::string &var)
Internal, returns pointer to the variable. Can return nullptr. Only searches in VM variables.
Definition: VirtualMachine.cpp:320
ScopeInstance & CurrentScopeInstance() const
Returns the current exection scope.
Definition: VirtualMachine.h:180
bool IsValid() const
Returns if the data is in a valid state.
Definition: Data.h:152
A single instruction.
Definition: Instruction.h:150
void fixparameter(Data ¶m, const Type &pdef, bool ref, const std::string &error)
Definition: VirtualMachine.cpp:176
void Increase(const Data &data)
Increases the reference count of the given object. If it is not registered, this request is ignored.
Definition: Runtime.h:47
This class defines a virtual environment for scripts to run.
Definition: VirtualMachine.h:45
Iterator Find(const K_ &key)
Finds the given key in the hashmap and returns iterator for it.
Definition: Hashmap.h:402
Data ExecuteFunction(const Function *fn, const std::vector< Data > ¶ms, bool method)
Executes a function in the current scope.
Definition: VirtualMachine.cpp:1421
Data MorphTo(const Type &type, Data source, bool allowtypecast=true) const
Morphs the given data into the target type.
Definition: Reflection.cpp:123
std::string namespc
Namespace that this symbol is in. For variables, namespc could be local.
Definition: VirtualMachine.h:29
This class represents a variable. It contains the data and the name of the variable.
Definition: Runtime.h:146
void CompileCurrent()
Commands virtual machine to compile current execution scope.
Definition: VirtualMachine.cpp:415
std::string GetName() const
Returns the name of this member.
Definition: Reflection.h:325