Gorgon Game Engine
RuntimeFunction.h
Go to the documentation of this file.
1 
3 
4 #pragma once
5 
6 #include <ostream>
7 #include <string>
8 
9 
10 #include "Reflection.h"
11 #include "Data.h"
12 #include "Input.h"
13 #include "Scope.h"
14 #include "VirtualMachine.h"
15 #include "Types/Array.h"
16 
17 
18 namespace Gorgon { namespace Scripting {
19 
21  public:
23  Scope &parent,
25  bool stretchlast, bool repeatlast,
26  bool accessible, bool constant,
27  bool returnsref, bool returnsconst,
28  bool implicit
29  ) :
30  scope(parent, ""),
34  )
35  { }
36 
38  return scope.GetParent();
39  }
40 
41  void SaveInstruction(Instruction inst, long pline) {
42  scope.SaveInstruction(inst, pline);
43  }
44 
45  void SaveInstructions(const std::vector<Instruction> &instructions) {
46  scope.SaveInstructions(instructions);
47  }
48 
49  std::shared_ptr<ScopeInstance> Instantiate(ScopeInstance &current) const {
50  return scope.Instantiate(current);
51  }
52 
53  virtual Data Call(bool ismethod, const std::vector<Data> &parameters) const override {
54  auto &vm=VirtualMachine::Get();
55 
56  auto scope=Instantiate(vm.CurrentScopeInstance());
57 
58  auto pin=parameters.begin();
59  auto par=this->parameters.begin();
60  if(parent->IsMember()) {
61  //...
62  Utils::NotImplemented("Member functions");
63  }
64 
65  Array *repeater=nullptr;
66  while(par!=this->parameters.end()) {
67  Data v;
68 
69  if(pin==parameters.end()) {
70  ASSERT(par->IsOptional(), "Non-optional parameter is ignored");
71  if(par==this->parameters.end()-1 && repeatlast) {
72  repeater=new Array(par->GetType());
73  vm.References.Register(repeater);
74  v={ArrayType(), repeater};
75  }
76  else {
77  v=par->GetDefaultValue();
78  }
79  }
80  else {
81  if(par==this->parameters.end()-1 && repeatlast) {
82  repeater=new Array(par->GetType());
83  vm.References.Register(repeater);
84  v={ArrayType(), repeater};
85  }
86  else {
87  v=*pin;
88  }
89  pin++;
90  }
91 
92  if(v.GetType()==Types::Variant()) {
93  v=v.GetValue<Data>();
94  }
95  if(v.IsReference() && par->IsConstant()) {
96  v.MakeConstant();
97  }
98 
99  scope->SetVariable(par->GetName(), v);
100  par++;
101  }
102 
103  while(pin!=parameters.end()) {
104  ASSERT(repeatlast, "Extra parameters supplied");
105  ASSERT(repeater, "???");
106 
107  auto v=*pin;
108  if(v.GetType()==Types::Variant()) {
109  v=v.GetValue<Data>();
110  }
111  if(v.IsReference() && par->IsConstant()) {
112  v.MakeConstant();
113  }
114 
115  repeater->PushData(v);
116  pin++;
117  }
118 
119  if(returntype) {
120  scope->SetReturn({returntype, returnsconst, returnsref});
121  }
122 
123  vm.Run(scope);
124  auto ret=scope->ReturnValue;
125 
126  if(ismethod && ret.IsValid()) {
127  vm.GetOutput()<<ret<<std::endl<<std::endl;
128 
129  return Data::Invalid();
130  }
131 
132  return ret;
133  }
134 
135  protected:
136 
137  virtual void dochecks(bool ismethod) override {
138  scope.SetName(parent->GetName());
139  }
140 
141  private:
142  mutable Scope scope;
143  };
144 
145 } }
Gorgon::Scripting::Scope::SetVariable
void SetVariable(const std::string &name, const Data &data)
Definition: Scope.h:151
Gorgon::Scripting::RuntimeOverload::Instantiate
std::shared_ptr< ScopeInstance > Instantiate(ScopeInstance &current) const
Definition: RuntimeFunction.h:49
Scope.h
Gorgon::Scripting::RuntimeOverload::SaveInstruction
void SaveInstruction(Instruction inst, long pline)
Definition: RuntimeFunction.h:41
Gorgon::Scripting::RuntimeOverload::GetParentScope
Scope & GetParentScope()
Definition: RuntimeFunction.h:37
Gorgon::Scripting::ScopeInstance
This is an instantiation of a scope.
Definition: Scope.h:223
Gorgon::Scripting::Function::Overload::Overload
Overload(const Type *returntype, ParameterList parameters, P_ ...tags)
Regular constructor that can take as many tags as needed.
Definition: Reflection.h:593
Gorgon::Scripting::Scope
A new scope is created automatically when a new input source or a function like construct is created.
Definition: Scope.h:62
Reflection.h
Gorgon::Scripting::Data::GetValue
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
Gorgon::Scripting::Function::Overload::returntype
const Type * returntype
Return type of this function variant. If nullptr this function does not return a value.
Definition: Reflection.h:739
Gorgon::Scripting::Function::Overload::implicit
bool implicit
If the parent function is constructor, marks this variant as an implicit type conversion.
Definition: Reflection.h:766
Gorgon::Scripting::Data
Data describes a piece of data.
Definition: Data.h:22
Gorgon::Scripting::ParameterList
std::vector< Parameter > ParameterList
Definition: Reflection.h:308
Gorgon::Scripting::RuntimeOverload::SaveInstructions
void SaveInstructions(const std::vector< Instruction > &instructions)
Definition: RuntimeFunction.h:45
Gorgon::Scripting::Scope::SaveInstruction
void SaveInstruction(Instruction inst, long pline)
Saves an instruction to the scope.
Definition: Scope.h:102
Gorgon::Scripting::Scope::Instantiate
std::shared_ptr< ScopeInstance > Instantiate()
Definition: Scope.cpp:80
Gorgon::Scripting::Function::IsMember
bool IsMember() const
Returns if this function is a member function of a type.
Definition: Reflection.h:863
Gorgon::Scripting::Function::Overload::accessible
bool accessible
Only meaningful in class member functions.
Definition: Reflection.h:751
Gorgon::Scripting::VirtualMachine::Get
static VirtualMachine & Get()
Returns the current VM for this thread.
Definition: VirtualMachine.h:109
Gorgon::Scripting::Data::GetType
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Gorgon::Scripting::Scope::SetName
void SetName(const std::string &name)
In rare cases where scope name cannot be determined at the construction, this function can be used to...
Definition: Scope.h:179
Gorgon::Scripting::Data::MakeConstant
void MakeConstant()
Makes this data a constant.
Definition: Data.cpp:199
Gorgon::Scripting::Type
This class stores information about types.
Definition: Reflection.h:1165
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Scripting::RuntimeOverload::dochecks
virtual void dochecks(bool ismethod) override
This function should perform validity checks on the variant.
Definition: RuntimeFunction.h:137
Gorgon::Scripting::Data::IsReference
bool IsReference() const
Returns if this data contains a reference.
Definition: Data.cpp:212
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Scripting::Data::Invalid
static Data Invalid()
Constructs an invalid data object.
Definition: Data.h:27
Gorgon::Scripting::Function::Overload::parent
Function * parent
The parent function of this variant.
Definition: Reflection.h:763
Gorgon::Scripting::Array
Definition: Array.h:14
Gorgon::Scripting::Array::PushData
void PushData(Data elm)
Definition: Array.h:52
VirtualMachine.h
Gorgon::Scripting::Function::Overload
Represents a function overload.
Definition: Reflection.h:588
Gorgon::Scripting::Function::Overload::stretchlast
bool stretchlast
If true, in console dialect, spaces in the last parameter are not treated as parameter separator as i...
Definition: Reflection.h:743
Gorgon::Scripting::Function::Overload::repeatlast
bool repeatlast
If true last parameter can be specified any number of times.
Definition: Reflection.h:747
Gorgon::Scripting::Scope::SaveInstructions
void SaveInstructions(const std::vector< Instruction > &insts)
Saves a list of instructions to this scope.
Definition: Scope.h:107
Gorgon::Scripting::Function::Overload::parameters
ParameterList parameters
Modifiable parameters of this overload.
Definition: Reflection.h:736
Input.h
Gorgon::Scripting::RuntimeOverload::RuntimeOverload
RuntimeOverload(Scope &parent, const Type *returntype, ParameterList parameters, bool stretchlast, bool repeatlast, bool accessible, bool constant, bool returnsref, bool returnsconst, bool implicit)
Definition: RuntimeFunction.h:22
Gorgon::Scripting::ArrayType
Type * ArrayType()
Definition: Array.cpp:74
Gorgon::Scripting::Function::Overload::returnsref
bool returnsref
This function variant returns a reference.
Definition: Reflection.h:757
Gorgon::Scripting::RuntimeOverload
Definition: RuntimeFunction.h:20
Data.h
Array.h
Gorgon::Scripting::Function::Overload::returnsconst
bool returnsconst
This function variant returns a constant, useful with references.
Definition: Reflection.h:760
Gorgon::Scripting::Function::Overload::constant
bool constant
Makes this function constant. Only works on member functions.
Definition: Reflection.h:754
Gorgon::Scripting::RuntimeOverload::Call
virtual Data Call(bool ismethod, const std::vector< Data > &parameters) const override
Class the stub for this function.
Definition: RuntimeFunction.h:53
Gorgon::Scripting::Function
Represents a function.
Definition: Reflection.h:557
Gorgon::Scripting::Instruction
A single instruction.
Definition: Instruction.h:150
Gorgon::Utils::NotImplemented
void NotImplemented(const std::string &what="This feature")
Definition: Assert.h:187
Gorgon::Scripting::Scope::GetParent
Scope & GetParent() const
Definition: Scope.h:138
Gorgon::Scripting::Member::GetName
std::string GetName() const
Returns the name of this member.
Definition: Reflection.h:325