]> git.tdb.fi Git - libs/gl.git/blob - source/programsyntax.h
Implement an actual expression parser
[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 private:
17         Node &operator=(const Node &);
18 public:
19         virtual ~Node() { }
20
21         virtual Node *clone() const = 0;
22         virtual void visit(NodeVisitor &) = 0;
23 };
24
25 template<typename T>
26 class NodePtr
27 {
28 private:
29         T *node;
30
31 public:
32         NodePtr(T *n = 0): node(n) { }
33         NodePtr(const NodePtr &p): node(clone(p.node)) { }
34         NodePtr &operator=(const NodePtr &p) { delete node; node = clone(p.node); return *this; }
35         ~NodePtr() { delete node; }
36
37 private:
38         static T *clone(T *n) { return n ? n->clone() : 0; }
39
40 public:
41         T *operator->() { return node; }
42         const T *operator->() const { return node; }
43         operator void *() const { return node; }
44 };
45
46 struct VariableDeclaration;
47
48 struct Block: Node
49 {
50         std::vector<NodePtr<Node> > body;
51         bool use_braces;
52
53         Block();
54
55         virtual Block *clone() const { return new Block(*this); }
56         virtual void visit(NodeVisitor &);
57 };
58
59 struct Expression: Node
60 {
61         virtual Expression *clone() const = 0;
62 };
63
64 struct Literal: Expression
65 {
66         std::string token;
67
68         virtual Literal *clone() const { return new Literal(*this); }
69         virtual void visit(NodeVisitor &);
70 };
71
72 struct ParenthesizedExpression: Expression
73 {
74         NodePtr<Expression> expression;
75
76         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
77         virtual void visit(NodeVisitor &);
78 };
79
80 struct VariableReference: Expression
81 {
82         std::string name;
83
84         virtual VariableReference *clone() const { return new VariableReference(*this); }
85         virtual void visit(NodeVisitor &);
86 };
87
88 struct MemberAccess: Expression
89 {
90         NodePtr<Expression> left;
91         std::string member;
92
93         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
94         virtual void visit(NodeVisitor &);
95 };
96
97 struct UnaryExpression: Expression
98 {
99         std::string oper;
100         NodePtr<Expression> expression;
101         bool prefix;
102
103         UnaryExpression();
104
105         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
106         virtual void visit(NodeVisitor &);
107 };
108
109 struct BinaryExpression: Expression
110 {
111         NodePtr<Expression> left;
112         std::string oper;
113         NodePtr<Expression> right;
114         std::string after;
115         bool assignment;
116
117         BinaryExpression();
118
119         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
120         virtual void visit(NodeVisitor &);
121 };
122
123 struct FunctionCall: Expression
124 {
125         std::string name;
126         bool constructor;
127         std::vector<NodePtr<Expression> > arguments;
128
129         FunctionCall();
130
131         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
132         virtual void visit(NodeVisitor &);
133 };
134
135 struct ExpressionStatement: Node
136 {
137         NodePtr<Expression> expression;
138
139         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
140         virtual void visit(NodeVisitor &);
141 };
142
143 struct Layout: Node
144 {
145         struct Qualifier
146         {
147                 std::string identifier;
148                 std::string value;
149         };
150
151         std::vector<Qualifier> qualifiers;
152         std::string interface;
153
154         virtual Layout *clone() const { return new Layout(*this); }
155         virtual void visit(NodeVisitor &);
156 };
157
158 struct StructDeclaration: Node
159 {
160         std::string name;
161         Block members;
162
163         StructDeclaration();
164
165         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
166         virtual void visit(NodeVisitor &);
167 };
168
169 struct VariableDeclaration: Node
170 {
171         bool constant;
172         std::string sampling;
173         std::string interface;
174         std::string type;
175         std::string name;
176         bool array;
177         NodePtr<Expression> array_size;
178         NodePtr<Expression> init_expression;
179
180         VariableDeclaration();
181
182         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
183         virtual void visit(NodeVisitor &);
184 };
185
186 struct InterfaceBlock: Node
187 {
188         std::string interface;
189         std::string name;
190         Block members;
191
192         InterfaceBlock();
193
194         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
195         virtual void visit(NodeVisitor &);
196 };
197
198 struct FunctionDeclaration: Node
199 {
200         std::string return_type;
201         std::string name;
202         std::vector<NodePtr<VariableDeclaration> > parameters;
203         bool definition;
204         Block body;
205
206         FunctionDeclaration();
207
208         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
209         virtual void visit(NodeVisitor &);
210 };
211
212 struct Conditional: Node
213 {
214         Expression *condition;
215         Block body;
216         Block else_body;
217
218         virtual Conditional *clone() const { return new Conditional(*this); }
219         virtual void visit(NodeVisitor &);
220 };
221
222 struct Iteration: Node
223 {
224         NodePtr<Node> init_statement;
225         NodePtr<Expression> condition;
226         NodePtr<Expression> loop_expression;
227         Block body;
228
229         virtual Iteration *clone() const { return new Iteration(*this); }
230         virtual void visit(NodeVisitor &);
231 };
232
233 struct Return: Node
234 {
235         NodePtr<Expression> expression;
236
237         virtual Return *clone() const { return new Return(*this); }
238         virtual void visit(NodeVisitor &);
239 };
240
241 struct NodeVisitor
242 {
243         virtual ~NodeVisitor() { }
244
245         virtual void visit(Block &) { }
246         virtual void visit(Literal &) { }
247         virtual void visit(ParenthesizedExpression &) { }
248         virtual void visit(VariableReference &) { }
249         virtual void visit(MemberAccess &) { }
250         virtual void visit(UnaryExpression &) { }
251         virtual void visit(BinaryExpression &) { }
252         virtual void visit(FunctionCall &) { }
253         virtual void visit(ExpressionStatement &) { }
254         virtual void visit(Layout &) { }
255         virtual void visit(StructDeclaration &) { }
256         virtual void visit(VariableDeclaration &) { }
257         virtual void visit(InterfaceBlock &) { }
258         virtual void visit(FunctionDeclaration &) { }
259         virtual void visit(Conditional &) { }
260         virtual void visit(Iteration &) { }
261         virtual void visit(Return &) { }
262 };
263
264 enum ContextType
265 {
266         GLOBAL,
267         VERTEX,
268         GEOMETRY,
269         FRAGMENT
270 };
271
272 struct Context
273 {
274         ContextType type;
275         bool present;
276         ProgramSyntax::Block content;
277
278         Context(ContextType);
279 };
280
281 struct Module
282 {
283         Context global_context;
284         Context vertex_context;
285         Context geometry_context;
286         Context fragment_context;
287         std::map<std::string, StructDeclaration *> structs;
288
289         Module();
290 };
291
292 } // namespace ProgramSyntax
293 } // namespace GL
294 } // namespace Msp
295
296 #endif