Gorgon Game Engine
AST.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include "../../Containers/Collection.h"
6 #include "../Data.h"
7 #include "../Instruction.h"
8 
9 
10 namespace Gorgon { namespace Scripting { namespace Compilers {
11 
12  class Base;
13 
19  class ASTNode {
20  public:
21 
23  enum NodeType {
26 
31 
35 
38 
41 
44 
47 
51 
55 
58 
61 
64 
67  };
68 
70  ASTNode(NodeType type): Type(type) { }
71 
74  ASTNode *newnode=new ASTNode(Type);
75  newnode->Start=Start;
76  newnode->Text=Text;
77  newnode->Leaves=Leaves.Duplicate();
78 
79  return newnode;
80  }
81 
84  Leaves.Destroy();
85  }
86 
89 
91  int Start = -1;
92 
94  int Line = -1;
95 
97  std::string Text;
98 
101 
104 
105 
106  };
107 
112  class ASTCompiler {
113  public:
114 
117  ASTCompiler(std::vector<Instruction> &list, Base &parser) : parser(parser), list(&list) { }
118 
122  unsigned Compile(ASTNode *tree);
123 
127  bool IsReady() const { return waitingcount==0; }
128 
129  void Finalize() {
130  if(scopes.size()) {
131  throw FlowException(scope::keywordnames[scopes.back().type]+" scope is not closed.");
132  }
133  }
134 
135  private:
136  bool compilekeyword(ASTNode *tree, Byte &tempind);
137  void release(int start, int except=-1);
138  void release(int start, Value except);
139 
140 
141  struct scope {
142  enum scopetype {
143  unknown,
144  ifkeyword,
145  whilekeyword,
146  functionkeyword,
147  methodkeyword,
148  forkeyword
149  } type;
150 
151  static const std::string keywordnames[];
152 
153  scope(scopetype type, int index) : type(type) { indices.push_back(index); }
154 
155  scope(scopetype type) : type(type) { }
156 
157  scope(scopetype type, const Data &data) : type(type), data(data) { }
158 
159  bool passed=0;
160  Data data;
161  std::vector<int> indices;
162  std::vector<int> indices2;
163 
164  int state=0;
165  int clear=-1;
166  };
167 
168  std::vector<scope> scopes;
169  std::vector<std::vector<Instruction>*> redirects;
170 
171  //temporaries start from 1
172  int indstart=1;
173  int waitingcount = 0;
174  std::vector<Instruction> *list;
175  Base &parser;
176  };
177 
184  void ASTToSVG(const std::string &line, ASTNode &tree, const std::vector<std::string> &compiled={}, bool show=false);
185 
187  void PrintAST(ASTNode &tree);
188 } } }
Gorgon::Scripting::Compilers::ASTToSVG
void ASTToSVG(const std::string &line, ASTNode &tree, const std::vector< std::string > &compiled={}, bool show=false)
Converts given AST to an SVG file.
Definition: AST.cpp:88
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::Resource::GID::Data
constexpr Type Data
Data resource.
Definition: GID.h:164
Gorgon::Scripting::InstructionType::FunctionCall
@ FunctionCall
Marks instruction as a regular function call.
Gorgon::Scripting::ReferenceCounter::Register
void Register(const Data &data)
Registers a new object of reference counting, this will set reference count to one.
Definition: Runtime.h:24
Gorgon::String::Replace
std::string Replace(std::string str, const std::string &find, const std::string &replace)
String replace that does not use regex.
Definition: String.h:349
Gorgon::Scripting::Compilers::ASTNode::Text
std::string Text
Textual data held by this node.
Definition: AST.h:97
Gorgon::Scripting::Compilers::ASTNode::Operator
@ Operator
This node represents an operator. All operators in GorgonScript are left associative and binary.
Definition: AST.h:46
Gorgon::OS::Open
bool Open(const std::string &file)
Opens the given file with the related application.
Definition: Linux.cpp:268
Gorgon::Scripting::Value::SetStringLiteral
void SetStringLiteral(const std::string &value)
Definition: Instruction.h:105
Gorgon::Scripting::Compilers::ASTNode::Start
int Start
Starting character of the node. Used for error locating.
Definition: AST.h:91
Gorgon::Scripting::VirtualMachine::References
ReferenceCounter References
This system allows objects of automatic lifetime.
Definition: VirtualMachine.h:222
Gorgon::Scripting::Compilers::PrintAST
void PrintAST(ASTNode &tree)
Recursively prints an AST.
Definition: AST.cpp:120
Gorgon::Scripting::Compilers::ASTNode::Identifier
@ Identifier
This node is an identifier.
Definition: AST.h:40
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
AST.h
Gorgon::Scripting::Compilers::ASTNode::Literal
@ Literal
This node represents a literal. Literal member of ASTNode should be set.
Definition: AST.h:25
Gorgon::OS::Start
bool Start(const std::string &name, const std::vector< std::string > &args=std::vector< std::string >())
Starts the given application.
Definition: Linux.cpp:116
Gorgon::Scripting::Compilers::ASTCompiler::Compile
unsigned Compile(ASTNode *tree)
This function compiles given abstract syntax tree, returns the number of instructions generated.
Definition: AST.cpp:355
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::Compilers::Base
The base class for compilers.
Definition: Compilers.h:21
Gorgon::Scripting::InstructionType::Assignment
@ Assignment
Marks instruction as an assignment.
Gorgon::Scripting::VirtualMachine::Get
static VirtualMachine & Get()
Returns the current VM for this thread.
Definition: VirtualMachine.h:109
Gorgon::Scripting::Compilers::ASTNode::Keyword
@ Keyword
This node is a keyword call.
Definition: AST.h:57
Gorgon::Scripting::Compilers::ASTNode::FunctionCall
@ FunctionCall
This node represents a function call.
Definition: AST.h:30
Gorgon::Scripting::Compilers::ASTCompiler::IsReady
bool IsReady() const
If this function returns true, it is ok to use instructions from the list.
Definition: AST.h:127
Gorgon::Scripting::Compilers::ASTCompiler::Finalize
void Finalize()
Definition: AST.h:129
Gorgon::Utils::ASSERT_FALSE
void ASSERT_FALSE(const std::string &message, int skip=1, int depth=4)
Definition: Assert.h:192
Gorgon::Scripting::Data::GetType
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Gorgon::Scripting::Compilers::ASTNode
Represents a node in abstract syntax tree.
Definition: AST.h:19
Gorgon::Scripting::Compilers::ASTNode::~ASTNode
~ASTNode()
Destroying a node destroys its children.
Definition: AST.h:83
Gorgon::Scripting::Compilers::ASTNode::Line
int Line
Starting line of this ASTNode.
Definition: AST.h:94
Gorgon::Scripting::Compilers::ASTNode::ASTNode
ASTNode(NodeType type)
Constructor requires the node type.
Definition: AST.h:70
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::Compilers::ASTNode::Duplicate
ASTNode * Duplicate()
Duplicates this node.
Definition: AST.h:73
Gorgon::Scripting::Value::Name
std::string Name
Used for variables and constants.
Definition: Instruction.h:84
Gorgon::Scripting::Compilers::ASTNode::Variable
@ Variable
This node represents a variable identifier.
Definition: AST.h:43
Gorgon::Scripting::Compilers::ASTNode::MethodCall
@ MethodCall
Same as function call, this just calls method variant if it exists, if not it will print out return v...
Definition: AST.h:34
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Scripting::Compilers::ASTCompiler
ASTCompiler stores states for AST compiler.
Definition: AST.h:112
Gorgon::Scripting::Data::Invalid
static Data Invalid()
Constructs an invalid data object.
Definition: Data.h:27
Gorgon::Containers::Collection
Collection is a container for reference typed objects.
Definition: Collection.h:21
Gorgon::Scripting::Value::Literal
Data Literal
Used for literal values.
Definition: Instruction.h:87
Gorgon::String::ToLower
std::string ToLower(std::string str)
Converts the given string to lowercase.
Definition: String.h:416
Gorgon::Scripting::Compilers::ASTNode::Leaves
Containers::Collection< ASTNode > Leaves
Leaves of this node.
Definition: AST.h:103
Gorgon::Scripting::Compilers::ASTNode::Member
@ Member
This node represents a membership. Membership should be parsed as left associative.
Definition: AST.h:37
Gorgon::Scripting::Instruction::Type
InstructionType Type
Type of the instruction.
Definition: Instruction.h:159
Gorgon::Scripting::Compilers::ASTNode::LiteralValue
Data LiteralValue
If node type is literal, this value will be used.
Definition: AST.h:100
Gorgon::Scripting::Value
This class contains a parsed value.
Definition: Instruction.h:82
Gorgon::Scripting::Compilers::ASTNode::Assignment
@ Assignment
This node is an assignment. This node should be top level.
Definition: AST.h:60
Gorgon::Scripting::Value::Result
Byte Result
Used for temporary results.
Definition: Instruction.h:93
Gorgon::Scripting::Compilers::ASTNode::Type
NodeType Type
Type of the node.
Definition: AST.h:88
Gorgon::Scripting::Compilers::dottree
std::string dottree(ASTNode &tree, int &ind)
Definition: AST.cpp:15
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::Compilers::ASTNode::Index
@ Index
This node represents an indexing operation.
Definition: AST.h:50
Gorgon::Scripting::Compilers::ASTCompiler::ASTCompiler
ASTCompiler(std::vector< Instruction > &list, Base &parser)
AST compiler requires a vector of instructions.
Definition: AST.h:117
Gorgon::Scripting::ValueType::Literal
@ Literal
This is a literal value.
Gorgon::Scripting::Compilers::ASTNode::Empty
@ Empty
This node is empty, possibly a placeholder for an identifier.
Definition: AST.h:63
Gorgon::Scripting::Compilers::ASTNode::List
@ List
List of expressions to be compiled.
Definition: AST.h:66
Gorgon::Scripting::MappedReferenceType
This class allows embedded types to become scripting types that are passed around as references.
Definition: Embedding.h:1406
Gorgon::Scripting::ValueType::Identifier
@ Identifier
Marks this value as an identifier, either a constant or a variable.
Gorgon::Scripting::InstructionType::Unknown
@ Unknown
This value is not valid.
Gorgon::Scripting::Compilers::compilevalue
Value compilevalue(ASTNode &tree, std::vector< Instruction > *list, Byte &tempind, bool generateoutput=true)
Definition: AST.cpp:157
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::Compilers::ASTNode::Construct
@ Construct
This node is a constructor node.
Definition: AST.h:54
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::Compilers::instructionlisttype
MappedReferenceType< std::vector< Instruction >, &ToEmptyString > instructionlisttype("#instructionlist", "")
Gorgon::Scripting::Instruction
A single instruction.
Definition: Instruction.h:150
Gorgon::Scripting::Compilers::ASTNode::NodeType
NodeType
Node type.
Definition: AST.h:23
Gorgon::Scripting::ToEmptyString
std::string ToEmptyString(const T_ &)
Definition: Embedding.h:30
Gorgon::Scripting::Member::GetName
std::string GetName() const
Returns the name of this member.
Definition: Reflection.h:325