Gorgon Game Engine
Compilers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <string>
5 #include <vector>
6 
7 #include "Data.h"
8 #include "Instruction.h"
9 #include "Compilers/AST.h"
10 #include "../Types.h"
11 
12 namespace Gorgon { namespace Scripting {
13 
14  class Scope;
15  class InputSource;
16 
18 namespace Compilers {
19 
21  class Base {
22  public:
24 
25  virtual ~Base() {}
26 
32  virtual unsigned Compile(const std::string &input, unsigned long pline) = 0;
33 
36  virtual void Finalize() = 0;
37 
38  virtual Base &Duplicate(Scope *scope) const = 0;
39 
41  bool HasScope() const {
42  return scope!=nullptr;
43  }
44 
46  Scope &GetScope() const {
47  return *scope;
48  }
49 
51  std::vector<Instruction> List;
52 
53  protected:
56  };
57 
59  class Intermediate : public Base {
60  public:
61  Intermediate(Scope *scope=nullptr) : Base(scope) { }
62 
63  virtual unsigned Compile(const std::string &input, unsigned long pline) override;
64  virtual void Finalize() override { }
65 
66  virtual Intermediate &Duplicate(Scope *scope) const {
67  return *new Intermediate(scope);
68  }
69 
70  private:
71  void storedfn(const std::string &input, int &ch);
72 
73  void fncall(const std::string &input, int &ch, bool allowmethod=true);
74 
75  void varassign(const std::string &input, int &ch);
76 
77  void memberassign(const std::string &input, int &ch);
78 
79  Value parsevalue(const std::string &input, int &ch);
80 
81  unsigned long parsetemporary(const std::string &input, int &ch);
82 
83  void eatwhite(const std::string &input, int &ch);
84 
85  void jinst(std::string input, int &ch);
86  };
87 
89  class Programming : public Base {
90  public:
91  Programming(Scope *scope=nullptr) : Base(scope), compiler(List, *this) { }
92 
93  virtual unsigned Compile(const std::string &input, unsigned long pline) override;
94 
95  virtual void Finalize() override;
96 
97  virtual Programming &Duplicate(Scope *scope) const override {
98  return *new Programming(scope);
99  }
100 
101  private:
102  ASTNode *parse(const std::string &input);
103  void extractline(std::string &input, std::string &prepared);
104 
105  void transformpos(int ch, int &oline, int &och);
106 
107  ASTCompiler compiler;
108  std::string left;
109  struct charmarker {
110  int pos;
111  int chr;
112  int line;
113  int off;
114  };
115  std::vector<charmarker> linemarkers;
116  int waiting=0;
117  int plinespassed=0; //physical lines that are passed while trying to find the end of the logical line
118  };
119 
121  std::string Disassemble(const Instruction *);
122 
124  inline std::string Disassemble(const Instruction &inst) { return Disassemble(&inst); }
125 
127  void Disassemble(InputSource &source, std::ostream &out);
128 
129 } } }
Gorgon::Scripting::Type::Parse
virtual Data Parse(const std::string &) const =0
Parses a string into this data. This function is allowed to throw.
Gorgon::Scripting::Math
Library Math("Math", "Maths library.")
Definition: VirtualMachine.h:41
Gorgon::Scripting::ByteToString
std::string ByteToString(const Gorgon::Byte &b)
Definition: Builtin.cpp:109
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::Compilers::Intermediate::Compile
virtual unsigned Compile(const std::string &input, unsigned long pline) override
Asks the compiler to compile the given input.
Definition: IL.cpp:161
Gorgon::Scripting::Compilers::Base::Finalize
virtual void Finalize()=0
Finalizes the compilation.
Gorgon::Resource::GID::Data
constexpr Type Data
Data resource.
Definition: GID.h:164
Gorgon::Scripting::Namespace::AddMembers
virtual void AddMembers(std::initializer_list< StaticMember * > newmembers)
Adds a list of members to this namespace.
Definition: Reflection.h:1104
Gorgon::Scripting::Types::Constant
const Scripting::Type & Constant()
Gorgon::Scripting::Compilers::Programming
Programming dialect compiler.
Definition: Compilers.h:89
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::Containers::Hashmap::GetCount
long GetCount() const
Returns the number of elements in the map.
Definition: Hashmap.h:375
Gorgon::Scripting::FunctionType
Type * FunctionType()
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
Gorgon::Scripting::Compilers::Base::scope
Scope * scope
The current scope the compiler is compiling, can be nullptr.
Definition: Compilers.h:55
Reflection.h
Gorgon::Scripting::Compilers::Base::Compile
virtual unsigned Compile(const std::string &input, unsigned long pline)=0
Asks the compiler to compile the given input.
Gorgon::Scripting::Data
Data describes a piece of data.
Definition: Data.h:22
AST.h
Embedding.h
Gorgon::Scripting::ParameterList
std::vector< Parameter > ParameterList
Definition: Reflection.h:308
Gorgon::Scripting::VirtualMachine::SetOutput
void SetOutput(std::ostream &out, bool deleteonchange=false)
Redirects the output stream to the given stream.
Definition: VirtualMachine.cpp:50
Gorgon::Scripting::Namespace::Members
const StaticMemberList & Members
List of static members.
Definition: Reflection.h:1148
Gorgon::Float
float Float
Represents floating point data type.
Definition: Types.h:16
Gorgon::Scripting::Compilers::Intermediate::Duplicate
virtual Intermediate & Duplicate(Scope *scope) const
Definition: Compilers.h:66
Gorgon::Scripting::Compilers::Base
The base class for compilers.
Definition: Compilers.h:21
Gorgon::Scripting::Compilers::Base::GetScope
Scope & GetScope() const
Returns the scope that compiler will compile.
Definition: Compilers.h:46
Gorgon::Scripting::Compilers::Intermediate::Finalize
virtual void Finalize() override
Finalizes the compilation.
Definition: Compilers.h:64
Gorgon::Scripting::VirtualMachine::Get
static VirtualMachine & Get()
Returns the current VM for this thread.
Definition: VirtualMachine.h:109
Gorgon::Scripting::MapFunctionToInstanceMember
InstanceMember * MapFunctionToInstanceMember(F_ reader, const std::string &name, const std::string &help, const Type *membertype, const Type *parenttype)
This function will map a const data returning function to a read-only, non-ref, const instance member...
Definition: Embedding.h:1147
Gorgon::Animation::log
Utils::Logger log
Definition: Animation.cpp:11
Gorgon::Scripting::Types::Namespace
const Scripting::Type & Namespace()
Definition: Reflection.h:315
Gorgon::Scripting::Data::GetType
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Gorgon::Scripting::Keywords
Library Keywords
Definition: VirtualMachine.h:41
Gorgon::Scripting::Compilers::ASTNode
Represents a node in abstract syntax tree.
Definition: AST.h:19
Gorgon::Scripting::MappedOperator
This class makes working with operators easier.
Definition: Embedding.h:635
Gorgon::Graphics::internal::isspace
bool isspace(Glyph g)
Definition: Font.cpp:96
Gorgon::Scripting::Compilers::Intermediate::Intermediate
Intermediate(Scope *scope=nullptr)
Definition: Compilers.h:61
Gorgon::String::ToUpper
std::string ToUpper(std::string str)
Converts the given string to uppercase.
Definition: String.h:426
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::Library
This class represents a library.
Definition: Reflection.h:1596
Gorgon::Char
uint32_t Char
Definition: Types.h:46
Gorgon::Scripting::Compilers::Programming::Finalize
virtual void Finalize() override
Finalizes the compilation.
Gorgon::Scripting::Compilers::Disassemble
std::string Disassemble(const Instruction *)
Disassembles the given instruction.
Definition: Generator.cpp:55
Gorgon::Scripting::RepeatTag
@ RepeatTag
Marks an object as repeatable.
Definition: Reflection.h:73
Gorgon::Scripting::Compilers::Programming::Compile
virtual unsigned Compile(const std::string &input, unsigned long pline) override
Asks the compiler to compile the given input.
Gorgon::Scripting::Data::IsReference
bool IsReference() const
Returns if this data contains a reference.
Definition: Data.cpp:212
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::String::ToLower
std::string ToLower(std::string str)
Converts the given string to lowercase.
Definition: String.h:416
Gorgon::String::Trim
std::string Trim(std::string str, const std::string &chars=" \t\n\r")
Strips whitespace around the given string both from start and end.
Definition: String.h:370
Gorgon::Scripting::ReferenceTag
@ ReferenceTag
Marks the object as a reference.
Definition: Reflection.h:61
Gorgon::Scripting::OptionalTag
@ OptionalTag
Marks the object as optional.
Definition: Reflection.h:56
Gorgon::String::Extract
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
Gorgon::Scripting::Value
This class contains a parsed value.
Definition: Instruction.h:82
Gorgon::Scripting::StretchTag
@ StretchTag
Used only in functions with console dialect.
Definition: Reflection.h:77
Gorgon::Scripting::Compilers::Base::Base
Base(Scope *scope)
Definition: Compilers.h:23
Gorgon::Scripting::Compilers::Base::Duplicate
virtual Base & Duplicate(Scope *scope) const =0
Gorgon::Scripting::KeywordTag
@ KeywordTag
Marks object as a keyword.
Definition: Reflection.h:80
Gorgon::Scripting::Compilers::Programming::Duplicate
virtual Programming & Duplicate(Scope *scope) const override
Definition: Compilers.h:97
Gorgon::Scripting::MapOperator
Scripting::Function::Overload * MapOperator(F_ fn, const Type *returntype, const Type *rhs)
Definition: Embedding.h:626
Gorgon::Scripting::Compilers::Base::~Base
virtual ~Base()
Definition: Compilers.h:25
Gorgon::Scripting::Compilers::Intermediate
Intermediate language complier.
Definition: Compilers.h:59
Gorgon::Scripting::ConstTag
@ ConstTag
Marks a parameter or a function constant.
Definition: Reflection.h:95
Gorgon::Scripting::init_builtin
void init_builtin()
Definition: Builtin.cpp:113
Gorgon::Scripting::MappedValueType
This class allows embedded types to become scripting types that are passed around as values.
Definition: Embedding.h:1300
Gorgon::Scripting::ArrayType
Type * ArrayType()
Definition: Array.cpp:74
Gorgon::Scripting::Data::GetData
Any GetData() const
Returns the data contained in this data element.
Definition: Data.h:133
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::Scripting::Parameter
This class represents a function parameter description.
Definition: Reflection.h:137
Gorgon::Scripting::Compilers::Base::List
std::vector< Instruction > List
The instructions that are compiled.
Definition: Compilers.h:51
Gorgon::Scripting::ParameterType
Type * ParameterType()
Gorgon::Scripting::Compilers::Base::HasScope
bool HasScope() const
Returns if this compiler is bound to a scope.
Definition: Compilers.h:41
Gorgon::Scripting::TypeType
Type * TypeType()
Definition: Reflection.cpp:15
Gorgon::Scripting::VariableTag
@ VariableTag
Makes this parameter a variable accepting parameter.
Definition: Reflection.h:102
Gorgon::Any::Pointer
void * Pointer() const
Returns the pointer without type information.
Definition: Any.h:332
Gorgon::Scripting::VirtualMachine::ResetOutput
void ResetOutput()
Resets the output stream to default stream that is given in the constructor.
Definition: VirtualMachine.h:155
Gorgon::Scripting::ArrayFunctions
std::vector< StaticMember * > ArrayFunctions()
Definition: Array.cpp:198
Data.h
Gorgon::Scripting::Reflection
Library Reflection
Definition: Builtin.cpp:17
Gorgon::Scripting::Namespace::AddMember
virtual void AddMember(StaticMember &member)
Adds a new member to this namespace.
Definition: Reflection.h:1088
Gorgon::Scripting::Integrals
Library Integrals
This library requires Initialize to be called.
Definition: VirtualMachine.h:41
Array.h
Instruction.h
Gorgon::Scripting::MapFunction
Scripting::Function::Overload * MapFunction(F_ fn, const Type *returntype, ParameterList parameters, P_ ...tags)
Definition: Embedding.h:614
Exceptions.h
Exceptions This file contains string related exceptions.
Gorgon::Scripting::VirtualMachine::UnsetVariable
void UnsetVariable(const std::string &name)
Definition: VirtualMachine.cpp:395
Gorgon::Scripting::Types::Function
const Scripting::Type & Function()
Definition: Reflection.h:589
MAP_COMPARE
#define MAP_COMPARE(opname, op, mappedtype, cpptype)
Creates a comparison function.
Definition: Embedding.h:692
Gorgon::Scripting::VirtualMachine::GetOutput
std::ostream & GetOutput() const
Returns the output stream.
Definition: VirtualMachine.h:145
Gorgon::Scripting::Types::Type
const Scripting::Type & Type()
Definition: Reflection.h:316
Gorgon::Scripting::VirtualMachine::getvarref
Variable * getvarref(const std::string &var)
Internal, returns pointer to the variable. Can return nullptr. Only searches in VM variables.
Definition: VirtualMachine.cpp:320
Gorgon::Scripting::Instruction
A single instruction.
Definition: Instruction.h:150
Gorgon::Scripting::ImplicitTag
@ ImplicitTag
Makes a constructor implicit.
Definition: Reflection.h:105
Gorgon::Scripting::InitTypeType
void InitTypeType()
Gorgon::Scripting::Compilers::Programming::Programming
Programming(Scope *scope=nullptr)
Definition: Compilers.h:91
Gorgon::Scripting::InitReflection
void InitReflection()
Definition: Reflection.cpp:30