]> git.tdb.fi Git - libs/gl.git/blob - source/programsyntax.h
Begin implementing a new shader program generator system
[libs/gl.git] / source / programsyntax.h
1 #ifndef MSP_GL_PROGRAMSYNTAX_H_
2 #define MSP_GL_PROGRAMSYNTAX_H_
3
4 #include <map>
5 #include <string>
6 #include <vector>
7
8 namespace Msp {
9 namespace GL {
10 namespace ProgramSyntax {
11
12 struct NodeVisitor;
13
14 struct Node
15 {
16         virtual ~Node() { }
17
18         virtual void visit(NodeVisitor &) = 0;
19 };
20
21 struct Block: Node
22 {
23         std::vector<Node *> body;
24         bool use_braces;
25
26         Block();
27         virtual ~Block();
28
29         virtual void visit(NodeVisitor &);
30 };
31
32 struct Expression
33 {
34         std::vector<std::string> tokens;
35
36         bool empty() const { return tokens.empty(); }
37 };
38
39 struct ExpressionStatement: Node
40 {
41         Expression expression;
42
43         virtual void visit(NodeVisitor &);
44 };
45
46 struct Layout: Node
47 {
48         struct Qualifier
49         {
50                 std::string identifier;
51                 std::string value;
52         };
53
54         std::vector<Qualifier> qualifiers;
55         std::string interface;
56
57         virtual void visit(NodeVisitor &);
58 };
59
60 struct StructDeclaration: Node
61 {
62         std::string name;
63         Block members;
64
65         StructDeclaration();
66
67         virtual void visit(NodeVisitor &);
68 };
69
70 struct VariableDeclaration: Node
71 {
72         bool constant;
73         std::string sampling;
74         std::string interface;
75         std::string type;
76         std::string name;
77         bool array;
78         Expression array_size;
79         Expression init_expression;
80
81         VariableDeclaration();
82
83         virtual void visit(NodeVisitor &);
84 };
85
86 struct InterfaceBlock: Node
87 {
88         std::string interface;
89         std::string name;
90         Block members;
91
92         InterfaceBlock();
93
94         virtual void visit(NodeVisitor &);
95 };
96
97 struct FunctionDeclaration: Node
98 {
99         std::string return_type;
100         std::string name;
101         std::vector<VariableDeclaration *> parameters;
102         bool definition;
103         Block body;
104
105         FunctionDeclaration();
106         ~FunctionDeclaration();
107
108         virtual void visit(NodeVisitor &);
109 };
110
111 struct Conditional: Node
112 {
113         Expression condition;
114         Block body;
115         Block else_body;
116
117         virtual void visit(NodeVisitor &);
118 };
119
120 struct Iteration: Node
121 {
122         Node *init_statement;
123         Expression condition;
124         Expression loop_expression;
125         Block body;
126
127         Iteration();
128         virtual ~Iteration();
129
130         virtual void visit(NodeVisitor &);
131 };
132
133 struct Return: Node
134 {
135         Expression expression;
136
137         virtual void visit(NodeVisitor &);
138 };
139
140 struct NodeVisitor
141 {
142         virtual ~NodeVisitor() { }
143
144         virtual void visit(Block &) { }
145         virtual void visit(ExpressionStatement &) { }
146         virtual void visit(Layout &) { }
147         virtual void visit(StructDeclaration &) { }
148         virtual void visit(VariableDeclaration &) { }
149         virtual void visit(InterfaceBlock &) { }
150         virtual void visit(FunctionDeclaration &) { }
151         virtual void visit(Conditional &) { }
152         virtual void visit(Iteration &) { }
153         virtual void visit(Return &) { }
154 };
155
156 enum ContextType
157 {
158         GLOBAL,
159         VERTEX,
160         GEOMETRY,
161         FRAGMENT
162 };
163
164 struct Context
165 {
166         ContextType type;
167         bool present;
168         ProgramSyntax::Block content;
169
170         Context(ContextType);
171 };
172
173 struct Module
174 {
175         Context global_context;
176         Context vertex_context;
177         Context geometry_context;
178         Context fragment_context;
179         std::map<std::string, StructDeclaration *> structs;
180
181         Module();
182 };
183
184 } // namespace ProgramSyntax
185 } // namespace GL
186 } // namespace Msp
187
188 #endif