 |
Gorgon Game Engine
|
Go to the documentation of this file.
8 #include "../Scripting.h"
14 namespace Gorgon {
namespace Scripting {
20 Instruction instruction;
39 return (source == other.source ? line<other.line : source<other.source);
42 bool IsValid()
const {
return source!=0; }
46 unsigned long GetLine()
const {
return line; }
49 SourceMarker(
unsigned long line, uintptr_t source) : line(line), source(source) {}
51 unsigned long line = 0;
71 Scope(
Scope &parent,
const std::string &name,
bool terminal=
false);
79 return lines[line].physical;
88 if(!provider)
return false;
95 return (
unsigned)lines.size();
99 std::string
GetName()
const {
return name; }
103 lines.push_back({inst, pline});
109 for(
auto &inst : insts) {
110 lines.push_back({inst, pline});
125 return instances.GetCount()!=0;
129 ASSERT(instances.GetCount(),
"There are no instances available");
131 return *instances.Last();
135 return parent!=
nullptr;
143 auto var=variables.find(name);
145 if( var !=variables.end() )
152 variables[name]={name, data};
156 auto var=variables.find(name);
158 if( var !=variables.end() ) {
159 variables.erase(var);
171 std::vector<Line> temp;
194 Scope *parent=
nullptr;
201 std::map<std::string, Variable, String::CaseInsensitiveLess> variables;
205 std::vector<Line> lines;
227 using variablestoragetype=std::map<std::string, Variable, String::CaseInsensitiveLess>;
228 using symbolstoragetype =std::map<std::string, const StaticMember*, String::CaseInsensitiveLess>;
232 scope.instances.Remove(
this);
248 return {current,
reinterpret_cast<uintptr_t
>(
this)};
254 return {current-1,
reinterpret_cast<uintptr_t
>(
this)};
270 auto varit=variables.find(name);
271 if( varit !=variables.end() )
272 return &varit->second;
282 auto varit=variables.find(name);
283 if( varit !=variables.end() )
284 return &varit->second;
297 variables[name]={name, data};
301 auto var=variables.find(name);
303 if( var !=variables.end() ) {
304 variables.erase(var);
350 if(symbols.count(symbol.
GetName())!=0) {
351 ambiguoussymbols.insert(std::make_pair(symbol.
GetName(), symbols[symbol.
GetName()]->GetQualifiedName()));
352 symbols.erase(symbol.
GetName());
355 else if(ambiguoussymbols.count(symbol.
GetName())!=0) {
359 symbols.insert(std::make_pair(symbol.
GetName(), &symbol));
366 variablestoragetype::iterator it;
367 if( (it=variables.find(name)) != variables.end() ) {
382 if(ambiguoussymbols.count(name)!=0) {
383 throw AmbiguousSymbolException(name, SymbolType::Identifier,
"Could be one of: "+ambiguoussymbols[name]);
387 symbolstoragetype::iterator sym;
388 if( (sym=symbols.find(name)) != symbols.end() ) {
389 return sym->second->Get();
404 std::set<std::string> UsedNamespaces;
407 std::map<std::string, const StaticMember*, String::CaseInsensitiveLess> symbols;
408 std::map<std::string, std::string, String::CaseInsensitiveLess> ambiguoussymbols;
419 std::map<std::string, Variable, String::CaseInsensitiveLess> variables;
421 ScopeInstance *parent =
nullptr;
428 unsigned long current = 0;
void SetVariable(const std::string &name, const Data &data)
Definition: Scope.h:151
long GetPhysicalLine()
Definition: Scope.h:338
void swap(Event< Source_, Args_... > &l, Event< Source_, Args_... > &r)
Swaps two events.
Definition: Event.h:351
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
virtual void Finalize()=0
Finalizes the compilation.
const Instruction * Peek(unsigned long line)
Returns the code at the given line without changing current line.
Definition: Scope.h:330
ScopeInstance & LastInstance() const
Definition: Scope.h:128
void Unload()
Unloads an input source by erasing all current data.
Definition: Scope.h:168
const Instruction * ReadInstruction(unsigned long line)
Reads the instruction in the given line.
Definition: Scope.cpp:5
This is an instantiation of a scope.
Definition: Scope.h:223
const Instruction * Peek()
Returns the code at the current line without incrementing it.
Definition: Scope.h:325
SourceMarker GetMarkerForNext() const
Returns a unique identifier for the next line in source code.
Definition: Scope.h:247
Programming dialect compiler.
Definition: Compilers.h:89
void MoveToEnd()
Definition: Scope.h:334
std::string GetName() const
Returns the name of this scope.
Definition: Scope.h:99
void SetReturn(Return returns)
Sets the return type of this scope instance.
Definition: Scope.h:346
A new scope is created automatically when a new input source or a function like construct is created.
Definition: Scope.h:62
friend class ScopeInstance
Definition: Scope.h:63
~ScopeInstance()
Definition: Scope.h:231
virtual unsigned Compile(const std::string &input, unsigned long pline)=0
Asks the compiler to compile the given input.
bool HasParent() const
Definition: Scope.h:134
Data describes a piece of data.
Definition: Data.h:22
uintptr_t GetSource() const
Definition: Scope.h:44
void SetVariable(const std::string &name, const Data &data)
Definition: Scope.h:296
constexpr Type Line
Definition: GID.h:234
void SaveInstruction(Instruction inst, long pline)
Saves an instruction to the scope.
Definition: Scope.h:102
Scope(InputProvider &provider, const std::string &name, bool terminal=false)
Constructor requires an input provider and a name to define this input source.
Definition: Scope.cpp:62
The base class for compilers.
Definition: Compilers.h:21
std::shared_ptr< ScopeInstance > Instantiate()
Definition: Scope.cpp:80
SourceMarker()
Definition: Scope.h:33
bool IsInteractive() const
Returns if this scope is interactive (i.e. code is entered by user)
Definition: Scope.h:87
Variable * GetVariable(const std::string &name)
Definition: Scope.h:142
bool reference
Marks this return as a reference.
Definition: Scope.h:219
void ASSERT_FALSE(const std::string &message, int skip=1, int depth=4)
Definition: Assert.h:192
SourceMarker GetMarkerForCurrent() const
Returns a unique identifier for the next line in source code.
Definition: Scope.h:253
This class uniquely represents a source code line.
Definition: Scope.h:30
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
friend class Scope
Definition: Scope.h:224
This class stores information about types.
Definition: Reflection.h:1165
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Data GetReference()
Definition: Data.cpp:148
void Jumpto(unsigned long line)
Jumps to the given line, line numbers start at zero.
Definition: Scope.h:236
Data ReturnValue
Current return value of the scope. This value can be modified before actually returning from the scop...
Definition: Scope.h:400
unsigned long GetLineNumber() const
Returns the current executing logical line number.
Definition: Scope.h:241
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
unsigned ReadyInstructionCount() const
Current number of instructions that are prepared.
Definition: Scope.h:94
const Type * type
Return type, nullptr means anything.
Definition: Scope.h:212
void AddSymbol(const StaticMember &symbol)
Adds a symbol to the symbol table of this scope.
Definition: Scope.h:349
Data FindSymbol(const std::string &name, bool reference)
Tries to find the given symbol (including variables)
Definition: Scope.h:364
bool IsTerminal() const
Returns if this scope is terminal scope.
Definition: Scope.h:116
void Compile()
Forces the compilation of entire scope.
Definition: Scope.h:319
void SaveInstructions(const std::vector< Instruction > &insts)
Saves a list of instructions to this scope.
Definition: Scope.h:107
bool UnsetVariable(const std::string &name)
Definition: Scope.h:155
bool HasInstance() const
Definition: Scope.h:124
Variable * GetVariable(const std::string &name)
Definition: Scope.h:281
const Instruction * Get()
Returns the code at the current line and increments the current line.
Definition: Scope.h:258
bool operator<(const SourceMarker &other)
Definition: Scope.h:38
unsigned long GetLine() const
Definition: Scope.h:46
This file contains classes that stores runtime and end programmer defined objects.
Scope & GetScope() const
Returns the scope of this scope instance.
Definition: Scope.h:343
std::vector< Instruction > List
The instructions that are compiled.
Definition: Compilers.h:51
std::string GetName() const
Definition: Scope.h:265
const std::map< std::string, Variable, String::CaseInsensitiveLess > & GetLocalVariables()
Definition: Scope.h:277
SourceMarker & operator=(const SourceMarker &)=default
std::string GetQualifiedName() const
Returns the namespace qualified name of this member.
Definition: Reflection.h:338
SourceMarker(const SourceMarker &)=default
bool IsValid() const
Definition: Scope.h:42
Represents what could be returned from a scope instance.
Definition: Scope.h:209
bool constant
Marked scope can return a constant.
Definition: Scope.h:216
A single instruction.
Definition: Instruction.h:150
bool UnsetVariable(const std::string &name)
Definition: Scope.h:300
Variable * GetLocalVariable(const std::string &name)
Definition: Scope.h:269
This class defines a virtual environment for scripts to run.
Definition: VirtualMachine.h:45
Scope & GetParent() const
Definition: Scope.h:138
long GetPhysicalLine(unsigned long line)
Current physical line.
Definition: Scope.h:77
This class represents a variable. It contains the data and the name of the variable.
Definition: Runtime.h:146
std::string GetName() const
Returns the name of this member.
Definition: Reflection.h:325