-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglsl.y
More file actions
307 lines (280 loc) · 9.03 KB
/
glsl.y
File metadata and controls
307 lines (280 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
%{
#include <stdio.h>
#include "AstNode.hpp"
extern FILE *yyin;
void yyerror(const char *s);
extern int yylex(void);
inline Type itot(int i) {
return static_cast<Type>(i);
}
AstNode *result;
template<typename T>
inline std::vector<T*>* push_node(void *vec, void *node) {
std::vector<T*>* vec2 = reinterpret_cast<std::vector<T*>*>(vec);
vec2->push_back(reinterpret_cast<T*>(node));
return vec2;
}
template<typename T>
inline T* re(void *node) {
return reinterpret_cast<T*>(node);
}
extern int yylineno;
%}
%union {
int ival;
double fval;
char *sval;
void *ast;
void *vector;
int enm;
}
%token <ival> INTCONST
%token <sval> FLOATCONST
%token <sval> IDENTIFIER
%token VOID
%token INT
%token FLOAT
%token VEC2
%token VEC3
%token VEC4
%token MAT2
%token MAT3
%token MAT4
%token LEFT_BRACE
%token RIGHT_BRACE
%token LEFT_PAREN
%token RIGHT_PAREN
%token COMMA
%token SEMICOLON
%token EQUAL
%token ATTRIBUTE
%token UNIFORM
%token LAYOUT
%token LOCATION
%token IN
%token OUT
%token PLUS
%token MINUS
%token STAR
%token SLASH
%token MODULUS
%token LEFT_SHIFT
%token RIGHT_SHIFT
%token AMPERSAND
%token CARET
%token PIPE
%token DOT
%token RETURN
%token IF
%token ELSE
%token FOR
%token LT
%token GT
%token LTE
%token GTE
%token EQ
%token STRUCT
%token HASH
%token VERSION
%token PRECISION
%token LOWP
%token MEDIUMP
%token HIGHP
%type <ast> source
%type <ast> function
%type <ast> attribute_decl
%type <ast> in_decl
%type <ast> out_decl
%type <ast> uniform_decl
%type <ast> global_decl
%type <enm> type
%type <enm> type_constructable
%type <vector> statements
%type <ast> statement
%type <ast> control_struct
%type <ast> expression
%type <ast> block
%type <vector> nodes
%type <vector> arg_list
%type <ast> arg_decl
%type <vector> arg_decl_list
%type <enm> comp_op
%type <enm> assign_op
%type <ast> struct_decl
%type <vector> struct_members
%type <ast> version_decl
%type <ast> precision_decl
%type <enm> precision_qualifier
%%
source: nodes { result = new Source(reinterpret_cast<std::vector<AstNode*>*>($1)); }
;
nodes: /* empty */ {
$$ = new std::vector<AstNode*>;
}
| nodes function { $$ = push_node<AstNode>($1, $2); }
| nodes attribute_decl { $$ = push_node<AstNode>($1, $2); }
| nodes in_decl { $$ = push_node<AstNode>($1, $2); }
| nodes out_decl { $$ = push_node<AstNode>($1, $2); }
| nodes uniform_decl { $$ = push_node<AstNode>($1, $2); }
| nodes global_decl { $$ = push_node<AstNode>($1, $2); }
| nodes struct_decl { $$ = push_node<AstNode>($1, $2); }
| nodes version_decl { $$ = push_node<AstNode>($1, $2); }
| nodes precision_decl { $$ = push_node<AstNode>($1, $2); }
;
precision_decl: PRECISION precision_qualifier FLOAT { $$ = new Precision(static_cast<PrecisionQualifier>($2), Float); }
| PRECISION precision_qualifier INT { $$ = new Precision(static_cast<PrecisionQualifier>($2), Int); }
;
precision_qualifier: LOWP { $$ = Lowp; }
| MEDIUMP { $$ = Mediump; }
| HIGHP { $$ = Highp; }
;
version_decl: HASH VERSION INTCONST { $$ = new Version($3); };
attribute_decl: ATTRIBUTE type IDENTIFIER SEMICOLON { $$ = new Attribute(itot($2), $3); };
in_decl: IN type IDENTIFIER SEMICOLON { $$ = new In(itot($2), $3); }
| LAYOUT LEFT_PAREN LOCATION EQUAL INTCONST RIGHT_PAREN IN type IDENTIFIER SEMICOLON {
$$ = new In(itot($8), $9, $5);
}
;
out_decl: OUT type IDENTIFIER SEMICOLON { $$ = new Out(itot($2), $3); };
uniform_decl: UNIFORM type IDENTIFIER SEMICOLON { $$ = new Uniform(itot($2), $3); };
global_decl: type IDENTIFIER EQUAL expression SEMICOLON { $$ = new TypedAssignment(itot($1), $2, re<Expression>($4)); };
struct_decl: STRUCT IDENTIFIER LEFT_BRACE struct_members RIGHT_BRACE SEMICOLON {
$$ = new StructDecl(reinterpret_cast<std::vector<StructMember*>*>($4), $2);
}
| STRUCT IDENTIFIER LEFT_BRACE struct_members RIGHT_BRACE IDENTIFIER SEMICOLON {
$$ = new StructDecl(reinterpret_cast<std::vector<StructMember*>*>($4), $2, $6);
}
;
struct_members: /* empty */ {
$$ = new std::vector<StructMember*>;
}
| struct_members type IDENTIFIER SEMICOLON {
$$ = push_node<StructMember>($1, new StructMember(itot($2), $3));
}
;
function: type IDENTIFIER LEFT_PAREN arg_decl_list RIGHT_PAREN block {
$$ = new Function(itot($1), $2, re<std::vector<ArgDecl*>>($4), re<Block>($6));
}
;
block: LEFT_BRACE statements RIGHT_BRACE {
$$ = new Block(reinterpret_cast<std::vector<Statement*>*>($2));
}
;
statements: /* empty */ {
$$ = new std::vector<Statement*>;
}
| statements statement SEMICOLON {
$$ = push_node<Statement>($1, $2);
}
| statements control_struct {
$$ = push_node<Statement>($1, $2);
}
;
statement: type IDENTIFIER EQUAL expression {
$$ = new TypedAssignment(itot($1), $2, re<Expression>($4));
}
| IDENTIFIER EQUAL expression {
$$ = new Assignment($1, re<Expression>($3));
}
| IDENTIFIER assign_op expression {
$$ = new OpAssignment(static_cast<AssignOp>($2), $1, re<Expression>($3));
}
| IDENTIFIER LEFT_PAREN arg_list RIGHT_PAREN {
$$ = new FunctionCallStm($1, reinterpret_cast<std::vector<Expression*>*>($3));
}
| IDENTIFIER PLUS PLUS { $$ = new PrePostFixStm($1, true, true); }
| IDENTIFIER MINUS MINUS { $$ = new PrePostFixStm($1, true, false); }
| PLUS PLUS IDENTIFIER { $$ = new PrePostFixStm($3, false, true); }
| MINUS MINUS IDENTIFIER { $$ = new PrePostFixStm($3, false, false); }
| RETURN expression {
$$ = new Return(re<Expression>($2));
}
;
control_struct: IF LEFT_PAREN expression RIGHT_PAREN block ELSE block {
$$ = new If(re<Expression>($3), re<Block>($5), re<Block>($7));
}
| IF LEFT_PAREN expression RIGHT_PAREN block {
$$ = new If(re<Expression>($3), re<Block>($5));
}
| FOR LEFT_PAREN statement SEMICOLON expression SEMICOLON statement RIGHT_PAREN block {
$$ = new ForLoop(re<Statement>($3), re<Expression>($5), re<Statement>($7), re<Block>($9));
}
;
expression: INTCONST { $$ = new IntConst($1); }
| FLOATCONST { $$ = new FloatConst(atof($1), strlen($1) - (strrchr($1, '.') - $1) - 1); }
| MINUS INTCONST { $$ = new IntConst($2 * -1); }
| MINUS FLOATCONST { $$ = new FloatConst(atof($2) * -1.0, strlen($2) - (strrchr($2, '.') - $2) - 1); }
| type_constructable LEFT_PAREN arg_list RIGHT_PAREN {
$$ = new TypeConstructor(static_cast<Type>($1), reinterpret_cast<std::vector<Expression*>*>($3));
}
| IDENTIFIER LEFT_PAREN arg_list RIGHT_PAREN {
$$ = new FunctionCall($1, reinterpret_cast<std::vector<Expression*>*>($3));
}
| IDENTIFIER { $$ = new Reference($1); }
/* This allows whitespace, which should be error */
| expression DOT IDENTIFIER { $$ = new FieldReference(re<Expression>($1), $3); }
| expression STAR expression { $$ = new MultiplyExpr(re<Expression>($1), re<Expression>($3)); }
| expression SLASH expression { $$ = new DivideExpr(re<Expression>($1), re<Expression>($3)); }
| expression PLUS expression { $$ = new PlusExpr(re<Expression>($1), re<Expression>($3)); }
| expression MINUS expression { $$ = new MinusExpr(re<Expression>($1), re<Expression>($3)); }
| LEFT_PAREN expression RIGHT_PAREN { $$ = new BracedExpr(re<Expression>($2)); }
| expression comp_op expression { $$ = new Comparison(static_cast<CompOp>($2), re<Expression>($1), re<Expression>($3)); }
| MINUS expression { $$ = new UnaryMinus(re<Expression>($2)); }
;
arg_list: /* empty */ {
$$ = new std::vector<Expression*>;
}
| arg_list expression {
$$ = push_node<Expression>($1, $2);
}
| arg_list COMMA expression {
$$ = push_node<Expression>($1, $3);
}
;
arg_decl: type IDENTIFIER { $$ = new ArgDecl(itot($1), $2); };
arg_decl_list: /* empty */ {
$$ = new std::vector<ArgDecl*>;
}
| arg_decl_list arg_decl {
$$ = push_node<ArgDecl>($1, $2);
}
| arg_decl_list COMMA arg_decl {
$$ = push_node<ArgDecl>($1, $3);
}
;
type_constructable: VEC2 { $$ = Vec2; }
| VEC3 { $$ = Vec3; }
| VEC4 { $$ = Vec4; }
| MAT2 { $$ = Mat2; }
| MAT3 { $$ = Mat3; }
| MAT4 { $$ = Mat4; }
;
type : VOID { $$ = Void; }
| INT { $$ = Int; }
| VEC2 { $$ = Vec2; }
| VEC3 { $$ = Vec3; }
| VEC4 { $$ = Vec4; }
| FLOAT { $$ = Float; }
| MAT2 { $$ = Mat2; }
| MAT3 { $$ = Mat3; }
| MAT4 { $$ = Mat4; }
;
comp_op : LT { $$ = Lt; }
| GT { $$ = Gt; }
| LTE { $$ = Lte; }
| GTE { $$ = Gte; }
| EQ { $$ = Eq; }
;
assign_op : PLUS EQUAL { $$ = PlusAssign; }
| MINUS EQUAL { $$ = MinusAssign; }
| SLASH EQUAL { $$ = DivideAssign; }
| MODULUS EQUAL { $$ = ModulusAssign; }
| LEFT_SHIFT EQUAL { $$ = LeftShiftAssign; }
| RIGHT_SHIFT EQUAL { $$ = RightShiftAssign; }
| AMPERSAND EQUAL { $$ = BitAndAssign; }
| CARET EQUAL { $$ = BitOrAssign; }
| PIPE EQUAL { $$ = BitIncOrAssign; }
%%
void yyerror(const char *s) {
printf("Parse error on line %d: %s", yylineno, s);
}