Gorgon Game Engine
Instruction.h
Go to the documentation of this file.
1 #pragma once
2 #include "../Scripting.h"
3 
4 namespace Gorgon { namespace Scripting {
5 
7  enum class InstructionType {
9  Unknown,
10 
14 
17  MemberFunctionCall,
18 
20  MethodCall,
21 
24  MemberToTemp,
25 
28  MemberToVar,
29 
32  MemberAssignment,
33 
36  MemberMethodCall,
37 
39  Assignment,
40 
42  RemoveTemp,
43 
45  SaveToTemp,
46 
48  Jump,
49 
51  JumpFalse,
52 
54  JumpTrue,
55 
58  };
59 
61  enum class ValueType {
64  Temp,
65 
67  Literal,
68 
70  Variable,
71 
73  Identifier,
74 
76  None,
77  };
78 
82  struct Value {
84  std::string Name;
85 
88 
91 
94 
95  void SetLiteral(const Scripting::Type *type, Any value) {
97  Literal={type, value};
98  }
99 
100  void SetLiteral(const Data &value) {
102  Literal=value;
103  }
104 
105  void SetStringLiteral(const std::string &value) {
107  Literal={Types::String(), {value}};
108  }
109 
110  void SetVariable(const std::string &name) {
112  Name=name;
113  }
114 
115  void SetIdentifier(const std::string &name) {
117  Name=name;
118  }
119 
120  void SetTemp(Byte index) {
121  Type=ValueType::Temp;
122  Result=index;
123  }
124 
125  std::string ToString() const {
126  if(Type==ValueType::Temp) {
127  return String::From(Result);
128  }
129  else if(Type==ValueType::Literal) {
130  return Literal.ToString();
131  }
132  else {
133  return Name;
134  }
135  }
136  };
137 
150  struct Instruction {
152 
153  Instruction(const Instruction &inst) :
154  Type(inst.Type), Name(inst.Name), RHS(inst.RHS), Parameters(inst.Parameters), JumpOffset(inst.JumpOffset),
155  Reference(inst.Reference)
156  { }
157 
160 
163 
166 
168  std::vector<Value> Parameters;
169 
170  bool Reference=false;
171 
172  union {
175  int JumpOffset=0;
176  };
177  };
178 } }
Gorgon::Scripting::Instruction::JumpOffset
int JumpOffset
Definition: Instruction.h:175
Gorgon::String::From
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
Gorgon::Scripting::InstructionType::FunctionCall
@ FunctionCall
Marks instruction as a regular function call.
Gorgon::Scripting::Value::SetLiteral
void SetLiteral(const Scripting::Type *type, Any value)
Definition: Instruction.h:95
Gorgon::Scripting::Value::SetStringLiteral
void SetStringLiteral(const std::string &value)
Definition: Instruction.h:105
Gorgon::Any
This class can hold any other information providing type erasure.
Definition: Any.h:32
Gorgon::Scripting::Instruction::Reference
bool Reference
Definition: Instruction.h:170
Gorgon::Scripting::Data
Data describes a piece of data.
Definition: Data.h:22
Gorgon::Scripting::Data::ToString
std::string ToString() const
Definition: Data.cpp:90
Gorgon::Scripting::Value::SetLiteral
void SetLiteral(const Data &value)
Definition: Instruction.h:100
Gorgon::Scripting::Instruction::RHS
Value RHS
The value that will be assigned to the variable.
Definition: Instruction.h:165
Gorgon::Scripting::Instruction::Name
Value Name
Name of the function or variable.
Definition: Instruction.h:162
Gorgon::Scripting::InstructionType::Assignment
@ Assignment
Marks instruction as an assignment.
Gorgon::Scripting::InstructionType
InstructionType
Describes the type of an instruction.
Definition: Instruction.h:7
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::Value::Name
std::string Name
Used for variables and constants.
Definition: Instruction.h:84
Gorgon::Scripting::Data::Invalid
static Data Invalid()
Constructs an invalid data object.
Definition: Data.h:27
Gorgon::Scripting::Value::Literal
Data Literal
Used for literal values.
Definition: Instruction.h:87
Gorgon::Scripting::Value
This class contains a parsed value.
Definition: Instruction.h:82
Gorgon::Scripting::Value::Result
Byte Result
Used for temporary results.
Definition: Instruction.h:93
Gorgon::Scripting::Value::ToString
std::string ToString() const
Definition: Instruction.h:125
Gorgon::Scripting::Value::SetTemp
void SetTemp(Byte index)
Definition: Instruction.h:120
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::Scripting::InstructionType::MethodCall
@ MethodCall
Marks this instruction as a method call.
Gorgon::Scripting::ValueType::Literal
@ Literal
This is a literal value.
Gorgon::Scripting::Value::SetIdentifier
void SetIdentifier(const std::string &name)
Definition: Instruction.h:115
Gorgon::Scripting::ValueType::Identifier
@ Identifier
Marks this value as an identifier, either a constant or a variable.
Gorgon::Scripting::Value::SetVariable
void SetVariable(const std::string &name)
Definition: Instruction.h:110
Gorgon::Scripting::InstructionType::Unknown
@ Unknown
This value is not valid.
Gorgon::Scripting::ValueType::Variable
@ Variable
This is a variable.
Gorgon::Scripting::Instruction::Store
Byte Store
Whether to store the result of the function.
Definition: Instruction.h:174
Gorgon::Scripting::Value::Type
ValueType Type
Type of this value.
Definition: Instruction.h:90
Gorgon::Scripting::InstructionType::Jump
@ Jump
Unconditionally jumps by the given offset. Offset should be in JumpOffset field.
Gorgon::Scripting::Instruction::Parameters
std::vector< Value > Parameters
Parameters of the function.
Definition: Instruction.h:168
Gorgon::Scripting::ValueType::None
@ None
For error checking purposes.
Gorgon::Scripting::ValueType
ValueType
Possible value types.
Definition: Instruction.h:61
Gorgon::Scripting::Instruction
A single instruction.
Definition: Instruction.h:150
Gorgon::Scripting::Instruction::Instruction
Instruction()
Definition: Instruction.h:151
Gorgon::Scripting::Instruction::Instruction
Instruction(const Instruction &inst)
Definition: Instruction.h:153