-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparser.y
283 lines (250 loc) · 10.8 KB
/
parser.y
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
/* -*- c -*- */
/*
* parser.y
*
* MathMap
*
* Copyright (C) 1997-2009 Mark Probst
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
%{
#include <stdio.h>
#include "mathmap.h"
#include "exprtree.h"
#include "builtins/builtins.h"
#include "jump.h"
#include "scanner.h"
%}
%union {
scanner_ident_t *ident;
int arg_type;
exprtree *exprtree;
limits_t *limits;
arg_decl_t *arg_decl;
top_level_decl_t *top_level;
option_t *options;
}
%token T_IDENT T_STRING T_INT T_FLOAT T_RANGE
%token T_FILTER
%token T_FLOAT_TYPE T_INT_TYPE T_BOOL_TYPE T_COLOR_TYPE T_GRADIENT_TYPE T_CURVE_TYPE T_IMAGE_TYPE
%token T_IF T_THEN T_ELSE T_END
%token T_WHILE T_DO
%token T_FOR
%left ';'
%right '='
%left T_OR T_AND T_XOR
%left T_EQUAL '<' '>' T_LESSEQUAL T_GREATEREQUAL T_NOTEQUAL
%left '+' '-'
%left '*' '/' '%'
%right '^'
%left UNARY
%right ':' T_CONVERT
%left '['
%%
start : filters
;
filters :
| filters filter
;
filter : options_opt T_FILTER T_IDENT '(' args_decl ')' docstring_opt
{
top_level_decl_t *decl = make_filter_decl($<ident>3, $<ident>7, $<arg_decl>5,
$<options>1);
start_parsing_filter(the_mathmap, decl);
register_args_as_uservals(the_mathmap->current_filter, $<arg_decl>5);
}
expr T_END
{
free($<ident>3);
if ($<ident>7 != NULL)
free($<ident>7);
the_mathmap->current_filter->v.mathmap.decl->v.filter.body = $<exprtree>9;
finish_parsing_filter(the_mathmap);
}
;
args_decl : { $<arg_decl>$ = 0; }
| arg_decl_list { $<arg_decl>$ = $<arg_decl>1; }
;
arg_decl_list : arg_decl { $<arg_decl>$ = $<arg_decl>1; }
| arg_decl_list ',' arg_decl
{ $<arg_decl>$ = arg_decl_list_append($<arg_decl>1, $<arg_decl>3); }
;
arg_decl : options_opt type T_IDENT limits_opt default_opt docstring_opt
{
arg_decl_t *arg_decl = make_simple_arg_decl($<arg_type>2, $<ident>3,
$<ident>6, $<options>1);
if ($<limits>4 != 0)
{
apply_limits_to_arg_decl(arg_decl, $<limits>4);
free_limits($<limits>4);
}
if ($<exprtree>5 != 0)
{
apply_default_to_arg_decl(arg_decl, $<exprtree>5);
free_exprtree($<exprtree>5);
}
free($<ident>3); if ($<ident>6 != NULL) free($<ident>6);
$<arg_decl>$ = arg_decl;
}
| options_opt T_FILTER T_IDENT '(' args_decl ')' docstring_opt
{
arg_decl_t *arg_decl = make_filter_arg_decl($<ident>3, $<arg_decl>5,
$<ident>7, $<options>1);
free($<ident>3); if ($<ident>7 != NULL) free($<ident>7);
$<arg_decl>$ = arg_decl;
}
;
docstring_opt : { $<ident>$ = NULL; }
| T_STRING { $<ident>$ = $<ident>1; }
;
options : option
{ $<options>$ = $<options>1; }
| options option
{ $<options>$ = options_append($<options>2, $<options>1); }
;
options_opt : { $<options>$ = 0; }
| options
{ $<options>$ = $<options>1; }
;
option : T_IDENT { $<options>$ = make_option($<ident>1, 0); free($<ident>1); }
| T_IDENT '(' options ')'
{ $<options>$ = make_option($<ident>1, $<options>3); free($<ident>1); }
;
int_const : T_INT { $<exprtree>$ = $<exprtree>1; }
| '-' T_INT { $<exprtree>$ = $<exprtree>2;
$<exprtree>$->val.int_const = -$<exprtree>$->val.int_const; }
;
float_const : T_FLOAT { $<exprtree>$ = $<exprtree>1; }
| '-' T_FLOAT { $<exprtree>$ = $<exprtree>2;
$<exprtree>$->val.float_const = -$<exprtree>$->val.float_const; }
;
limits_opt : { $<limits>$ = 0; }
| ':' int_const '-' int_const
{ $<limits>$ = make_int_limits($<exprtree>2, $<exprtree>4); }
| ':' float_const '-' int_const
{ $<limits>$ = make_float_limits($<exprtree>2, $<exprtree>4); }
| ':' int_const '-' float_const
{ $<limits>$ = make_float_limits($<exprtree>2, $<exprtree>4); }
| ':' float_const '-' float_const
{ $<limits>$ = make_float_limits($<exprtree>2, $<exprtree>4); }
;
default_opt : { $<exprtree>$ = 0; }
| '(' int_const ')'
{ $<exprtree>$ = $<exprtree>2; }
| '(' float_const ')'
{ $<exprtree>$ = $<exprtree>2; }
;
type : T_FLOAT_TYPE { $<arg_type>$ = ARG_TYPE_FLOAT; }
| T_INT_TYPE { $<arg_type>$ = ARG_TYPE_INT; }
| T_BOOL_TYPE { $<arg_type>$ = ARG_TYPE_BOOL; }
| T_COLOR_TYPE { $<arg_type>$ = ARG_TYPE_COLOR; }
| T_GRADIENT_TYPE { $<arg_type>$ = ARG_TYPE_GRADIENT; }
| T_CURVE_TYPE { $<arg_type>$ = ARG_TYPE_CURVE; }
| T_IMAGE_TYPE { $<arg_type>$ = ARG_TYPE_IMAGE; }
;
expr : T_INT { $<exprtree>$ = $<exprtree>1; }
| T_FLOAT { $<exprtree>$ = $<exprtree>1; }
| T_IDENT { $<exprtree>$ = make_var($<ident>1); free($<ident>1); }
| '[' exprlist ']' { $<exprtree>$ = make_tuple_exprtree($<exprtree>2); }
| T_IDENT '[' subscripts ']'
{
$<exprtree>$ = make_select(make_var($<ident>1), make_tuple_exprtree($<exprtree>3));
free($<ident>1);
}
| '(' expr ')' '[' subscripts ']' { $<exprtree>$ = make_select($<exprtree>2, make_tuple_exprtree($<exprtree>5)); }
| T_IDENT ':' expr { $<exprtree>$ = make_cast($<ident>1, $<exprtree>3);
free($<ident>1); }
| T_IDENT T_CONVERT expr { $<exprtree>$ = make_convert($<ident>1, $<exprtree>3); free($<ident>1); }
| expr '+' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '-' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '*' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '/' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '%' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '^' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_EQUAL expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '<' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr '>' expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_LESSEQUAL expr
{ $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_GREATEREQUAL expr
{ $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_NOTEQUAL expr
{ $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_OR expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_AND expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| expr T_XOR expr { $<exprtree>$ = make_operator_function($<ident>2, $<exprtree>1, $<exprtree>3); free($<ident>2); }
| '-' expr %prec UNARY
{ $<exprtree>$ = make_unary_operator_function($<ident>1, $<exprtree>2); free($<ident>1); }
| '!' expr %prec UNARY
{ $<exprtree>$ = make_unary_operator_function($<ident>1, $<exprtree>2); free($<ident>1); }
| '(' expr ')' { $<exprtree>$ = $<exprtree>2; }
| T_IDENT '(' arglist ')'
{ $<exprtree>$ = make_function($<ident>1, $<exprtree>3);
free($<ident>1); }
| T_IDENT '=' expr { $<exprtree>$ = make_assignment($<ident>1, $<exprtree>3);
free($<ident>1); }
| T_IDENT '[' subscripts ']' '=' expr
{ $<exprtree>$ = make_sub_assignment($<ident>1, make_tuple_exprtree($<exprtree>3), $<exprtree>6);
free($<ident>1); }
| expr ';' expr { $<exprtree>$ = make_sequence($<exprtree>1, $<exprtree>3); }
| T_IF expr T_THEN expr end
{ $<exprtree>$ = make_if_then($<exprtree>2, $<exprtree>4); }
| T_IF expr T_THEN expr else expr end
{ $<exprtree>$ = make_if_then_else($<exprtree>2,
$<exprtree>4,
$<exprtree>6); }
| T_WHILE expr T_DO expr end
{ $<exprtree>$ = make_while($<exprtree>2, $<exprtree>4); }
| T_DO expr T_WHILE expr T_END
{ $<exprtree>$ = make_do_while($<exprtree>2, $<exprtree>4); }
| T_FOR T_IDENT '=' expr
{
check_for_start($<exprtree>4);
$<exprtree>$ = make_assignment($<ident>2, $<exprtree>4);
}
T_RANGE expr T_DO expr T_END
{
$<exprtree>$ = make_for($<ident>2, $<exprtree>5, $<exprtree>4, $<exprtree>7, $<exprtree>9);
free($<ident>2);
}
;
arglist : { $<exprtree>$ = 0; }
| exprlist { $<exprtree>$ = $<exprtree>1; }
;
exprlist : expr { $<exprtree>$ = $<exprtree>1; }
| exprlist ',' expr { $<exprtree>$ = exprlist_append($<exprtree>1, $<exprtree>3); }
;
subscripts : subscript { $<exprtree>$ = $<exprtree>1; }
| subscripts ',' subscript { $<exprtree>$ = exprlist_append($<exprtree>1, $<exprtree>3); }
;
subscript : expr { $<exprtree>$ = $<exprtree>1; }
| T_INT T_RANGE T_INT { $<exprtree>$ = make_range($<exprtree>1, $<exprtree>3); }
;
else : T_ELSE
| ';' T_ELSE
;
end : T_END
| ';' T_END
;
%%
int
yyerror (char *s)
{
sprintf(error_string, _("Parse error."));
error_region = scanner_last_token_region();
JUMP(1);
return 0;
}