controller_load_parser.y 10 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
2
3
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009, 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2009, 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	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 3 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, see <http://www.gnu.org/licenses/>.

 */

20
21
22
//%code requires {
//typedef void*                 yyscan_t;
//}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
23

Jeroen Vreeken's avatar
Jeroen Vreeken committed
24
25
26
%{

#include <stdio.h>
27
#include <math.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
28
#include <controller/controller_block.h>
29
#include <controller/controller_load_int.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
#include <controller/controller_trace.h>
#include <controller/controller_sample.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
32
#include <controller/controller_time.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
33
#include <controller/controller_module.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
34

Jeroen Vreeken's avatar
Jeroen Vreeken committed
35
#define YYERROR_VERBOSE
Jeroen Vreeken's avatar
Jeroen Vreeken committed
36

Jeroen Vreeken's avatar
Jeroen Vreeken committed
37
38
39
40
#include <controller/controller_load_parser.tab.h>
#include <controller/controller_load_parser.yy.h>

void yyerror(yyscan_t *scanner, char const *s);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
41
42
43

%}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
45
46
47
48
%parse-param {yyscan_t scanner}
%lex-param {yyscan_t scanner}

%pure-parser

Jeroen Vreeken's avatar
Jeroen Vreeken committed
49
50
%start ctrllist

51
%token ALIASSYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
52
53
54
%token BRACEOPENSYM
%token BRACECLOSESYM
%token COMMASYM
55
%token <string> ALIASVARIABLESYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
56
%token <string> STRINGSYM
57
%token <string> VARIABLENAMESYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
%token <string> INTVARIABLESYM
59
60
%token <string> DOUBLEVARIABLESYM
%token <string> STRINGVARIABLESYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
61
62
63
64
%token <integer> BOOLSYM
%token <integer> INTSYM
%token <dbl> DOUBLESYM
%token <ul> UNSIGNEDLONGSYM
65
%token TRIGGERSYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
66
%token BLOCKSSYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
67
%token MODULESYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
68
69
70
%token LINKSSYM
%token TRACESSYM
%token PARAMSSYM
71
%token SETSYM
72
73
%token PARENTHESESOPENSYM
%token PARENTHESESCLOSESYM
74
%token INCLUDESYM
75
%token IMPORTSYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
76

77
78
79
%token FUNC_DEG2RAD_SYM
%token FUNC_RPM2RADS_SYM

80
%token <dbl> CONST_PI_SYM
81
%token <dbl> CONST_INF_SYM
82

83
84
%left DUMMY_DOUBLEVAR
%left DUMMY_INTVAR
85
%left FLOATCASTSYM DOUBLECASTSYM INTCASTSYM BOOLCASTSYM
86
87
88
89
90
91
92

%left MINSYM
%left PLUSSYM
%left SLASHSYM
%left STARSYM
%left UMINUS

Jeroen Vreeken's avatar
Jeroen Vreeken committed
93
94
95
96
97
98
99
100
%union
{
	char *string;
	int integer;
	double dbl;
	unsigned long ul;
}

101
%type <string> stringvar
102
103
104
105
106
107
108
%type <dbl> doublevar
%type <integer> intvar
%type <ul> unsignedlongvar

%type <dbl> deg2rad
%type <dbl> rpm2rads

Jeroen Vreeken's avatar
Jeroen Vreeken committed
109
110
111
112
113
114
%%

ctrllist: ctrl
	| ctrl ctrllist
	;

115
ctrl	: aliases
116
	| triggers
Jeroen Vreeken's avatar
Jeroen Vreeken committed
117
118
119
120
	| links
	| blocks
	| traces
	| params
121
	| include
122
	| import
123
	| set
Jeroen Vreeken's avatar
Jeroen Vreeken committed
124
125
	;

126
include: INCLUDESYM stringvar
127
128
129
130
131
132
		{ 
			if (controller_load_include($2)) {
				yyerror(scanner, "Failed to parse include");
				YYERROR;
			}
		}
133
134
	;

135
import: IMPORTSYM stringvar
136
137
138
139
140
141
142
143
		{ 
			if (controller_load_import($2)) {
				yyerror(scanner, "Failed to parse import");
				YYERROR;
			}
		}
	;

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
aliases: ALIASSYM BRACEOPENSYM aliaslist BRACECLOSESYM

aliaslist: alias
	| alias aliaslist
	;

alias: BRACEOPENSYM
		stringvar COMMASYM
		stringvar COMMASYM
		stringvar
		BRACECLOSESYM
		{
			controller_load_variable_alias_set($2, $4, $6);
		}


160
161
162
163
164
165
166
triggers: TRIGGERSYM BRACEOPENSYM triggerlist BRACECLOSESYM

triggerlist: trigger
	| trigger triggerlist
	;

trigger	: BRACEOPENSYM
167
		stringvar varlist
168
169
170
171
172
173
174
175
176
177
178
		BRACECLOSESYM
		{
			if(controller_load_trigger($2, scanner)) {
				yyerror(scanner, "Error in trigger");
				YYERROR;
			}
			free($2);
			controller_load_var_clear(scanner);
		}


Jeroen Vreeken's avatar
Jeroen Vreeken committed
179
180
181
182
183
blocks	: BLOCKSSYM PARENTHESESOPENSYM doublevar COMMASYM doublevar 
		{
			controller_time_param_set($3, $5);
		}
	 PARENTHESESCLOSESYM BRACEOPENSYM blocklist BRACECLOSESYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
184

Jeroen Vreeken's avatar
Jeroen Vreeken committed
185
186
blocklist: block 
	| module
Jeroen Vreeken's avatar
Jeroen Vreeken committed
187
	| block blocklist
Jeroen Vreeken's avatar
Jeroen Vreeken committed
188
	| module blocklist
Jeroen Vreeken's avatar
Jeroen Vreeken committed
189
190
191
	;

block	: BRACEOPENSYM
192
193
		stringvar COMMASYM 
		stringvar varlist
Jeroen Vreeken's avatar
Jeroen Vreeken committed
194
195
		BRACECLOSESYM
		{
196
			if(controller_load_block_create($2, $4, scanner)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
197
				yyerror(scanner, "Error creating block");
198
199
				YYERROR;
			}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
200
201
			free($2);
			free($4);
202
			controller_load_var_clear(scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
203
204
205
		}
	;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
module	: MODULESYM 
		PARENTHESESOPENSYM stringvar PARENTHESESCLOSESYM
		BRACEOPENSYM
		BRACEOPENSYM moduleinputlist BRACECLOSESYM
		BRACEOPENSYM moduleoutputlist BRACECLOSESYM
		BRACECLOSESYM
		{
			if (controller_module_create($3)) {
				yyerror(scanner, "Error creating module");
				YYERROR;
			}
		}
	;

moduleinputlist : moduleinput
	| moduleinput moduleinputlist
	;

moduleinput : BRACEOPENSYM 
		stringvar COMMASYM stringvar COMMASYM stringvar 
		{
			if (controller_module_input_add($2, $4, $6)) {
				yyerror(scanner, "Error adding module input");
				YYERROR;
			}
		}
	    BRACECLOSESYM

moduleoutputlist : moduleoutput
	| moduleoutput moduleoutputlist
	;

moduleoutput : BRACEOPENSYM 
		stringvar COMMASYM stringvar COMMASYM stringvar
		{
			if (controller_module_output_add($2, $4, $6)) {
				yyerror(scanner, "Error adding module output");
				YYERROR;
			}
		}
	    BRACECLOSESYM

Jeroen Vreeken's avatar
Jeroen Vreeken committed
248
249
250
251
252
253
254
params	: PARAMSSYM BRACEOPENSYM paramlist BRACECLOSESYM

paramlist: param
	| param paramlist
	;

param	: BRACEOPENSYM
255
256
		stringvar COMMASYM
		stringvar
Jeroen Vreeken's avatar
Jeroen Vreeken committed
257
258
259
		varlist
		BRACECLOSESYM
		{
260
			if (controller_load_block_param_set($2, $4, scanner)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
261
				yyerror(scanner, "Error setting parameter");
262
				yyerror(scanner, $4);
263
264
				YYERROR;
			}
265
			controller_load_var_clear(scanner);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
266
267
268
269
270
271
272
		}
	;

varlist	:	/* varlist may be emtpy */
	| COMMASYM var varlist
	;

273
var	: stringvar
274
		{ controller_load_var_add_str($1, scanner); }
275
	| doublevar %prec DUMMY_DOUBLEVAR
276
		{ controller_load_var_add_dbl($1, scanner); }
277
	| intvar %prec DUMMY_INTVAR
278
		{ controller_load_var_add_int($1, scanner); }
279
	| unsignedlongvar
280
		{ controller_load_var_add_ul($1, scanner); }
Jeroen Vreeken's avatar
Jeroen Vreeken committed
281
282
283
284
	| INTCASTSYM
		{ controller_load_var_add_int_array_start(scanner); }
		BRACEOPENSYM intlist BRACECLOSESYM
		{ controller_load_var_add_int_array_end(scanner); }
Jeroen Vreeken's avatar
Jeroen Vreeken committed
285
	| FLOATCASTSYM 
286
		{ controller_load_var_add_float_array_start(scanner); }
Jeroen Vreeken's avatar
Jeroen Vreeken committed
287
		BRACEOPENSYM floatlist BRACECLOSESYM
288
		{ controller_load_var_add_float_array_end(scanner); }
Jeroen Vreeken's avatar
Jeroen Vreeken committed
289
290
	;

291
292
293
294
295
stringvar: STRINGSYM
	| STRINGVARIABLESYM
		{ controller_load_variable_string_get($1); }
	;

296
297
floatlist: floatlistvar
	| floatlistvar COMMASYM floatlist
Jeroen Vreeken's avatar
Jeroen Vreeken committed
298
299
	;

300
floatlistvar: doublevar
301
302
303
		{ controller_load_var_add_flt($1, scanner); }
	;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
304
305
306
307
308
309
310
311
intlist: intlistvar
	| intlistvar COMMASYM intlist
	;

intlistvar: intvar
		{ controller_load_var_add_int($1, scanner); }
	;

312
313
314
doublevar: DOUBLEVARIABLESYM
		{ $$ = controller_load_variable_double_get($1); }
	| DOUBLESYM
315
		{ $$ = $1; }
316
317
	| FLOATCASTSYM doublevar
		{ $$ = $2; }
318
319
	| DOUBLECASTSYM doublevar
		{ $$ = $2; }
320
	| FLOATCASTSYM intvar
321
		{ $$ = $2; }
322
	| DOUBLECASTSYM intvar
323
		{ $$ = $2; }
324
325
326
	| FLOATCASTSYM unsignedlongvar
		{ $$ = $2; }
	| DOUBLECASTSYM unsignedlongvar
327
328
329
330
331
332
333
334
335
		{ $$ = $2; }
	| doublevar MINSYM doublevar
		{ $$ = ($1) - ($3); }
	| doublevar PLUSSYM doublevar
		{ $$ = ($1) + ($3); }
	| doublevar SLASHSYM doublevar
		{ $$ = ($1) / ($3); }
	| doublevar STARSYM doublevar
		{ $$ = ($1) * ($3); }
336
337
338
339
	| deg2rad
		{ $$ = $1; }
	| rpm2rads
		{ $$ = $1; }
340
341
	| CONST_PI_SYM
		{ $$ = M_PI; }
342
343
	| CONST_INF_SYM
		{ $$ = INFINITY; }
344
	| MINSYM doublevar %prec UMINUS
Jeroen Vreeken's avatar
Jeroen Vreeken committed
345
		{ $$ = - $2; }
346
347
348
	;


349
deg2rad: FUNC_DEG2RAD_SYM PARENTHESESOPENSYM doublevar PARENTHESESCLOSESYM
350
351
352
		{ $$ = ($3) * M_PI / 180.0; }
	;

353
rpm2rads: FUNC_RPM2RADS_SYM PARENTHESESOPENSYM doublevar PARENTHESESCLOSESYM
354
355
356
		{ $$ = ($3) * 2.0 * M_PI / 60.0; }
	;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
357
358
359
intvar: INTVARIABLESYM
		{ $$ = controller_load_variable_int_get($1); }
	| INTSYM
360
361
362
		{ $$ = $1; }
	| INTCASTSYM intvar
		{ $$ = $2; }
363
364
	| INTCASTSYM doublevar
		{ $$ = $2; }
365
366
	| INTCASTSYM unsignedlongvar
		{ $$ = $2; }
367
368
369
370
	| BOOLSYM
		{ $$ = $1; }
	| BOOLCASTSYM intvar
		{ $$ = $2; }
371
	| MINSYM intvar %prec UMINUS
Jeroen Vreeken's avatar
Jeroen Vreeken committed
372
		{ $$ = - $2; }
373
374
375
376
377
	;

unsignedlongvar: UNSIGNEDLONGSYM
		{ $$ = $1; }
	;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
378

379
set:	SETSYM VARIABLENAMESYM doublevar
Jeroen Vreeken's avatar
Jeroen Vreeken committed
380
		{ controller_load_variable_double_set($2, $3); }
Jeroen Vreeken's avatar
Jeroen Vreeken committed
381
382
	| SETSYM VARIABLENAMESYM intvar
		{ controller_load_variable_int_set($2, $3); }
383
	| SETSYM VARIABLENAMESYM stringvar
Jeroen Vreeken's avatar
Jeroen Vreeken committed
384
		{ controller_load_variable_string_set($2, $3); }
385
386
	;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
387
388
389
390
391
392
393
links	: LINKSSYM BRACEOPENSYM linklist BRACECLOSESYM

linklist: link
	| link linklist
	;

link	: BRACEOPENSYM
394
395
396
397
		stringvar COMMASYM 
		stringvar COMMASYM 
		stringvar COMMASYM 
		stringvar COMMASYM
Jeroen Vreeken's avatar
Jeroen Vreeken committed
398
399
400
		BOOLSYM
		BRACECLOSESYM
		{
401
			if (controller_block_connect($2, $4, $6, $8, $10)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
402
				yyerror(scanner, "Error connecting blocks");
403
404
				YYERROR;
			}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
405
406
407
408
409
			free($2);
			free($4);
			free($6);
			free($8);
		}
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
	| BRACEOPENSYM
		ALIASVARIABLESYM COMMASYM
		ALIASVARIABLESYM COMMASYM 
		BOOLSYM
		BRACECLOSESYM
		{
			if (controller_block_connect(
			    controller_load_variable_alias_get_block($2),
			    controller_load_variable_alias_get_term($2),
			    controller_load_variable_alias_get_block($4),
			    controller_load_variable_alias_get_term($4), $6)) {
				yyerror(scanner, "Error connecting blocks");
				YYERROR;
			}
		}
	| BRACEOPENSYM
		stringvar COMMASYM 
		stringvar COMMASYM 
		ALIASVARIABLESYM COMMASYM 
		BOOLSYM
		BRACECLOSESYM
		{
			if (controller_block_connect($2, $4,
			    controller_load_variable_alias_get_block($6),
			    controller_load_variable_alias_get_term($6), $8)) {
				yyerror(scanner, "Error connecting blocks");
				YYERROR;
			}
			free($2);
			free($4);
		}
	| BRACEOPENSYM
		ALIASVARIABLESYM COMMASYM 
		stringvar COMMASYM 
		stringvar COMMASYM 
		BOOLSYM
		BRACECLOSESYM
		{
			if (controller_block_connect(
			    controller_load_variable_alias_get_block($2),
			    controller_load_variable_alias_get_term($2),
			    $4, $6, $8)) {
				yyerror(scanner, "Error connecting blocks");
				YYERROR;
			}
			free($2);
			free($4);
		}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
458
459
460
461
462
463
464
465
466
	;

traces	: TRACESSYM BRACEOPENSYM tracelist BRACECLOSESYM

tracelist: trace
	| trace tracelist
	;

trace	: BRACEOPENSYM
467
468
469
470
		stringvar COMMASYM
		stringvar COMMASYM
		stringvar COMMASYM
		stringvar
Jeroen Vreeken's avatar
Jeroen Vreeken committed
471
472
473
474
475
476
477
478
479
		BRACECLOSESYM
		{
			struct controller_trace_name trace;
			
			trace.name = $2;
			trace.unit = $4;
			trace.blockname = $6;
			trace.termname = $8;

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
			controller_trace_list_add(&trace);
		}
	| BRACEOPENSYM
		stringvar COMMASYM
		stringvar COMMASYM
		ALIASVARIABLESYM
		BRACECLOSESYM
		{
			struct controller_trace_name trace;
			
			trace.name = $2;
			trace.unit = $4;
			trace.blockname = controller_load_variable_alias_get_block($6);
			trace.termname = controller_load_variable_alias_get_term($6);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
495
496
497
			controller_trace_list_add(&trace);
		}
	;