Symphony Of Empires
glsl_trans.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // glsl_trans.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <string>
28 #include <vector>
29 #include <exception>
30 
31 namespace Eng3D::GLSL {
32  class Token {
33  public:
34  enum class Type {
35  NONE,
37  ADD, SUB, MUL, DIV, REM, AND, OR, XOR,
40  LPAREN, RPAREN,
42  LBRACE, RBRACE,
43  NEWLINE,
44  // Special "hacky" stuff
45  MACRO,
46  };
47 
49  : type{ _type }
50  {
51 
52  }
53 
54  ~Token() = default;
55 
57  std::string data;
58  };
59 
60  enum class VariableType {
62  };
63 
64  class Variable {
65  public:
66  Variable() = default;
67  ~Variable() = default;
68 
70  std::string type_name;
71  std::string name;
72  bool is_const;
73  int layout_n;
74  };
75 
76  class Type {
77  public:
80  enum class PrimitiveType {
81  NONE,
82  // Primitives
83  INT, FLOAT, BOOL,
84  // Vectors
85  VEC2, VEC3, VEC4,
86  // Matrices
87  MAT4,
88  // Textures
90  };
91  std::string name;
93  };
94 
95  class Function {
96  public:
97  std::string name;
98  std::vector<std::pair<std::string, std::string>> args;
99  std::string ret_type;
100  };
101 
102  class Define {
103  public:
104  std::string name;
105  std::string value;
106  };
107 
108  class Context {
109  public:
110  Context(const std::string& _buffer)
111  : buffer{ _buffer }
112  {
113 
114  }
115  ~Context() = default;
116  std::string get_identifier(std::string::iterator& it);
117  std::string get_literal(std::string::iterator& it);
118  void lexer();
119  void parser();
120  std::string to_text();
121 
122  std::vector<int> line_numbers;
123  std::vector<Eng3D::GLSL::Type> types;
124  std::vector<Eng3D::GLSL::Variable> vars;
125  std::vector<Eng3D::GLSL::Function> funcs;
126  std::vector<Eng3D::GLSL::Token> tokens;
127  std::vector<Eng3D::GLSL::Define> defines;
128  std::string buffer;
129  };
130 
131  class Exception: public std::exception {
132  std::string buffer;
133  public:
134  Exception(std::vector<Eng3D::GLSL::Token>::iterator _it, const std::string& _buffer)
135  : buffer{ _buffer },
136  it{ _it }
137  {
138 
139  }
140 
141  virtual const char* what() const noexcept {
142  return buffer.c_str();
143  }
144 
145  std::vector<Eng3D::GLSL::Token>::iterator it;
146  };
147 }
Context(const std::string &_buffer)
Definition: glsl_trans.hpp:110
void parser()
Parses the current context's tokens and optimizes them.
Definition: glsl_trans.cpp:204
std::string get_literal(std::string::iterator &it)
Definition: glsl_trans.cpp:42
std::string to_text()
Definition: glsl_trans.cpp:297
std::vector< Eng3D::GLSL::Function > funcs
Definition: glsl_trans.hpp:125
std::vector< Eng3D::GLSL::Type > types
Definition: glsl_trans.hpp:123
std::vector< Eng3D::GLSL::Variable > vars
Definition: glsl_trans.hpp:124
std::vector< Eng3D::GLSL::Define > defines
Definition: glsl_trans.hpp:127
std::vector< int > line_numbers
Definition: glsl_trans.hpp:122
std::string get_identifier(std::string::iterator &it)
Definition: glsl_trans.cpp:31
std::vector< Eng3D::GLSL::Token > tokens
Definition: glsl_trans.hpp:126
std::vector< Eng3D::GLSL::Token >::iterator it
Definition: glsl_trans.hpp:145
virtual const char * what() const noexcept
Definition: glsl_trans.hpp:141
Exception(std::vector< Eng3D::GLSL::Token >::iterator _it, const std::string &_buffer)
Definition: glsl_trans.hpp:134
std::vector< std::pair< std::string, std::string > > args
Definition: glsl_trans.hpp:98
std::string ret_type
Definition: glsl_trans.hpp:99
std::string data
Definition: glsl_trans.hpp:57
Token(Eng3D::GLSL::Token::Type _type)
Definition: glsl_trans.hpp:48
enum Eng3D::GLSL::Token::Type type
Definition: glsl_trans.hpp:56
std::string name
Definition: glsl_trans.hpp:91
Eng3D::GLSL::Type::PrimitiveType primitive_type
Definition: glsl_trans.hpp:92
PrimitiveType
Since GLSL doesn't allow complex structures we can rely on a simple table of primitives.
Definition: glsl_trans.hpp:80
std::string type_name
Definition: glsl_trans.hpp:70
enum Eng3D::GLSL::VariableType type
Definition: glsl_trans.hpp:69