File: faustparser.y

package info (click to toggle)
faust 0.9.9.4b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,164 kB
  • ctags: 6,050
  • sloc: cpp: 25,384; xml: 4,102; makefile: 648; yacc: 372; ruby: 247; lex: 161; sh: 50
file content (521 lines) | stat: -rw-r--r-- 12,675 bytes parent folder | download
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

/* Parser for the Faust language */

%{

#include "tree.hh"
#include "xtended.hh"
#include "boxes.hh"
#include "prim2.hh"
#include "signals.hh"
#include "errormsg.hh"
#include "sourcereader.hh"

#include <string>
#include <list>

#define YYDEBUG 1
#define YYERROR_VERBOSE 1
#define YYMAXDEPTH	100000

extern char* 		yytext;
extern const char* 	yyfilename;
extern int 			yylineno;
extern int 			yyerr;
extern Tree 		gResult;

extern map<Tree, set<Tree> > gMetaDataSet;

int yylex();

Tree unquote(char* str)
{
	//-----------copy unquoted filename-------------
	char buf[512];
	int j=0;

	if (str[0] == '"') {
		//it is a quoted string, we remove the quotes
		for (int i=1; j<511 && str[i];) {
			buf[j++] = str[i++];
		}
		// remove last quote
		if (j>0) buf[j-1] = 0;
	} else {
		for (int i=0; j<511 && str[i];) {
			buf[j++] = str[i++];
		}
	}
	buf[j] = 0;

	return tree(buf);
	//----------------------------------------------
}

%}


%union {
	CTree* 	exp;
}

%start program

/* With local environment (lowest priority)*/
%left WITH

/* Block Diagram Algebra */
/*%left SEQ SPLIT MIX*/
%left SPLIT MIX
%left SEQ
%left PAR
%left REC

/* Primitive boxes */

%left LT LE EQ GT GE NE

%left ADD SUB OR
%left MUL DIV MOD AND XOR LSH RSH
%left FDELAY
%left DELAY1
%left APPL DOT


%token MEM
%token PREFIX

%token INTCAST
%token FLOATCAST
%token FFUNCTION
%token FCONSTANT
%token FVARIABLE

%token BUTTON
%token CHECKBOX
%token VSLIDER
%token HSLIDER
%token NENTRY
%token VGROUP
%token HGROUP
%token TGROUP

%token HBARGRAPH
%token VBARGRAPH
%token ATTACH


%token ACOS
%token ASIN
%token ATAN
%token ATAN2
%token COS
%token SIN
%token TAN

%token EXP
%token LOG
%token LOG10
%token POW
%token SQRT

%token ABS
%token MIN
%token MAX

%token FMOD
%token REMAINDER

%token FLOOR
%token CEIL
%token RINT



%token RDTBL
%token RWTBL

%token SELECT2
%token SELECT3

%token INT
%token FLOAT


%token LAMBDA
%token DOT

%token WIRE
%token CUT
%token ENDDEF
%token VIRG
%token LPAR
%token RPAR
%token LBRAQ
%token RBRAQ
%token WITH
%token DEF

%token IMPORT
%token COMPONENT

%token IPAR
%token ISEQ
%token ISUM
%token IPROD

%token STRING
%token FSTRING
%token IDENT
%token EXTRA

%token DECLARE

%token CASE
%token ARROW


%type <exp> program

%type <exp> eqlist
%type <exp> equation

%type <exp> params

%type <exp> diagram
%type <exp> eqname
%type <exp> expression
%type <exp> primitive
%type <exp> argument
%type <exp> arglist

%type <exp> ident
%type <exp> name

%type <exp> ffunction
%type <exp> fconst
%type <exp> fvariable
%type <exp> signature
%type <exp> string
%type <exp> uqstring
%type <exp> fstring
%type <exp> type
%type <exp> typelist
%type <exp> fun

%type <exp> fpar
%type <exp> fseq
%type <exp> fsum
%type <exp> fprod

%type <exp> button
%type <exp> checkbox
%type <exp> vslider
%type <exp> hslider
%type <exp> nentry
%type <exp> vgroup
%type <exp> hgroup
%type <exp> tgroup

%type <exp> vbargraph
%type <exp> hbargraph

%type <exp> rule
%type <exp> rulelist









%% /* grammar rules and actions follow */

program         : eqlist 						{$$ = $1; gResult = formatDefinitions($$); }
				;

eqlist			: /*empty*/						{$$ = nil; }
				| eqlist equation 				{$$ = cons ($2,$1); }
                ;
/*
equation		: eqname LPAR arglist RPAR DEF diagram ENDDEF	{$$ = cons($1,buildBoxAbstr($3,$6)); }
				| eqname DEF diagram ENDDEF						{$$ = cons($1,$3); }
*/
equation		: eqname LPAR arglist RPAR DEF diagram ENDDEF	{$$ = cons($1,cons($3,$6)); }
				| eqname DEF diagram ENDDEF						{$$ = cons($1,cons(nil,$3)); }
				| IMPORT LPAR uqstring RPAR ENDDEF				{$$ = importFile($3); }
                | DECLARE name string  ENDDEF                   {gMetaDataSet[$2].insert($3); $$ = nil; }
				| error ENDDEF									{$$ = nil; yyerr++;}
               	;

eqname			: ident 						{$$=$1; setDefProp($1, yyfilename, yylineno); }
				;

params			: ident							{$$ = cons($1,nil); }
				| params PAR ident				{$$ = cons($3,$1); }
                ;

diagram			: diagram WITH LBRAQ eqlist RBRAQ	{$$ = boxWithLocalDef($1,formatDefinitions($4)); }
				| diagram PAR diagram  			{$$ = boxPar($1,$3);}
				| diagram SEQ diagram  			{$$ = boxSeq($1,$3);}
				| diagram SPLIT  diagram 		{$$ = boxSplit($1,$3);}
				| diagram MIX diagram 			{$$ = boxMerge($1,$3);}
				| diagram REC diagram  			{$$ = boxRec($1,$3);}
				| expression					{$$ = $1; }
				;

expression		: expression ADD expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigAdd)); }
				| expression SUB expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigSub)); }
				| expression MUL expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigMul)); }
				| expression DIV expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigDiv)); }
				| expression MOD expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigRem)); }
				| expression FDELAY expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigFixDelay)); }
				| expression DELAY1  			{$$ = boxSeq($1,boxPrim1(sigDelay1)); }
				| expression DOT ident  		{$$ = boxAccess($1,$3); }

				| expression AND expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigAND)); }
				| expression OR expression 		{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigOR)); }
				| expression XOR expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigXOR)); }

				| expression LSH expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigLeftShift)); }
				| expression RSH expression 	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigRightShift)); }

				| expression LT expression  	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigLT)); }
				| expression LE expression  	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigLE)); }
				| expression GT expression  	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigGT)); }
				| expression GE expression  	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigGE)); }
				| expression EQ expression  	{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigEQ)); }
				| expression NE expression		{$$ = boxSeq(boxPar($1,$3),boxPrim2(sigNE)); }

				| expression LPAR arglist RPAR %prec APPL	{$$ = buildBoxAppl($1,$3); }
				
				| primitive						{$$ = $1;}
				;

primitive		: INT   						{$$ = boxInt(atoi(yytext));}
				| FLOAT 						{$$ = boxReal(float(atof(yytext)));}

				| ADD INT   					{$$ = boxInt(atoi(yytext));}
				| ADD FLOAT 					{$$ = boxReal(float(atof(yytext)));}

				| SUB INT   					{$$ = boxInt(0 - atoi(yytext));}
				| SUB FLOAT 					{$$ = boxReal(0.0f - float(atof(yytext)));}

				| WIRE   						{$$ = boxWire();}
				| CUT   						{$$ = boxCut();}

				| MEM   						{$$ = boxPrim1(sigDelay1);}
				| PREFIX   						{$$ = boxPrim2(sigPrefix);}

				| INTCAST   					{$$ = boxPrim1(sigIntCast);}
				| FLOATCAST   					{$$ = boxPrim1(sigFloatCast);}

				| ADD							{$$ = boxPrim2(sigAdd);}
				| SUB 							{$$ = boxPrim2(sigSub);}
				| MUL  							{$$ = boxPrim2(sigMul);}
				| DIV							{$$ = boxPrim2(sigDiv);}
				| MOD							{$$ = boxPrim2(sigRem);}
				| FDELAY						{$$ = boxPrim2(sigFixDelay);}

				| AND							{$$ = boxPrim2(sigAND);}
				| OR 							{$$ = boxPrim2(sigOR);}
				| XOR  							{$$ = boxPrim2(sigXOR);}

				| LSH							{$$ = boxPrim2(sigLeftShift);}
				| RSH 							{$$ = boxPrim2(sigRightShift);}

				| LT							{$$ = boxPrim2(sigLT);}
				| LE							{$$ = boxPrim2(sigLE);}
				| GT							{$$ = boxPrim2(sigGT);}
				| GE							{$$ = boxPrim2(sigGE);}
				| EQ							{$$ = boxPrim2(sigEQ);}
				| NE							{$$ = boxPrim2(sigNE);}

				| ATTACH						{$$ = boxPrim2(sigAttach);}

				| ACOS							{$$ = gAcosPrim->box(); }
				| ASIN							{$$ = gAsinPrim->box(); }
				| ATAN							{$$ = gAtanPrim->box(); }
				| ATAN2							{$$ = gAtan2Prim->box(); }
				| COS							{$$ = gCosPrim->box(); }
				| SIN							{$$ = gSinPrim->box(); }
				| TAN							{$$ = gTanPrim->box(); }

				| EXP							{$$ = gExpPrim->box(); }
				| LOG							{$$ = gLogPrim->box(); }
				| LOG10							{$$ = gLog10Prim->box(); }
				| POW							{$$ = gPowPrim->box(); }
				| SQRT							{$$ = gSqrtPrim->box(); }

				| ABS							{$$ = gAbsPrim->box(); }
				| MIN							{$$ = gMinPrim->box(); }
				| MAX							{$$ = gMaxPrim->box(); }

				| FMOD							{$$ = gFmodPrim->box(); }
				| REMAINDER						{$$ = gRemainderPrim->box(); }

				| FLOOR							{$$ = gFloorPrim->box(); }
				| CEIL							{$$ = gCeilPrim->box(); }
				| RINT							{$$ = gRintPrim->box(); }


				| RDTBL 						{$$ = boxPrim3(sigReadOnlyTable);}
				| RWTBL							{$$ = boxPrim5(sigWriteReadTable);}

				| SELECT2 						{$$ = boxPrim3(sigSelect2);}
				| SELECT3						{$$ = boxPrim4(sigSelect3);}

				| ident 						{$$ = $1;}
                | SUB ident                     {$$ = boxSeq(boxPar(boxInt(0),$2),boxPrim2(sigSub));}

				| LPAR diagram RPAR				{$$ = $2;}
				| LAMBDA LPAR params RPAR DOT LPAR diagram RPAR
												{$$ = buildBoxAbstr($3,$7);}

				| CASE LBRAQ rulelist RBRAQ		{$$ = boxCase(checkRulelist($3));}
				
				| ffunction						{$$ = boxFFun($1); }
                | fconst                        {$$ = $1;}
                | fvariable                     {$$ = $1;}
				| COMPONENT LPAR uqstring RPAR	{$$ = boxComponent($3); }

				| button						{$$ = $1;}
				| checkbox						{$$ = $1;}
				| vslider						{$$ = $1;}
				| hslider						{$$ = $1;}
				| nentry						{$$ = $1;}
				| vgroup						{$$ = $1;}
				| hgroup						{$$ = $1;}
				| tgroup						{$$ = $1;}
				| vbargraph						{$$ = $1;}
				| hbargraph						{$$ = $1;}

				| fpar							{$$ = $1;}
				| fseq							{$$ = $1;}
				| fsum							{$$ = $1;}
				| fprod							{$$ = $1;}
				;


ident			: IDENT							{$$ = boxIdent(yytext);}
				;

name			: IDENT							{$$ = tree(yytext);}
				;



arglist			: argument						{$$ = cons($1,nil); }
				| arglist PAR argument			{$$ = cons($3,$1); }
				;

argument		: argument SEQ argument  		{$$ = boxSeq($1,$3);}
				| argument SPLIT argument 		{$$ = boxSplit($1,$3);}
				| argument MIX argument 		{$$ = boxMerge($1,$3);}
				| argument REC argument  		{$$ = boxRec($1,$3);}
				| expression					{$$ = $1;}
				;

string			: STRING						{$$ = tree(yytext); }
				;

uqstring		: STRING						{$$ = unquote(yytext); }
				;

fstring			: STRING						{$$ = tree(yytext); }
				| FSTRING						{$$ = tree(yytext); }
				;

/* description of iterative expressions */

fpar			: IPAR LPAR ident PAR argument PAR diagram RPAR
												{$$ = boxIPar($3,$5,$7);}
				;

fseq			: ISEQ LPAR ident PAR argument PAR diagram RPAR
												{$$ = boxISeq($3,$5,$7);}
				;

fsum			: ISUM LPAR ident PAR argument PAR diagram RPAR
												{$$ = boxISum($3,$5,$7);}
				;

fprod			: IPROD LPAR ident PAR argument PAR diagram RPAR
												{$$ = boxIProd($3,$5,$7);}
				;


/* description of foreign functions */

ffunction		: FFUNCTION LPAR signature PAR fstring PAR string RPAR
												{$$ = ffunction($3,$5,$7);}
				;

fconst          : FCONSTANT LPAR type name PAR fstring RPAR
                                                {$$ = boxFConst($3,$4,$6);}

fvariable       : FVARIABLE LPAR type name PAR fstring RPAR
                                                {$$ = boxFVar($3,$4,$6);}
				;

/* Description of user interface building blocks */
button			: BUTTON LPAR string RPAR		{$$ = boxButton($3); }
				;

checkbox		: CHECKBOX LPAR string RPAR		{$$ = boxCheckbox($3); }
				;

vslider			: VSLIDER LPAR string PAR argument PAR argument PAR argument PAR argument RPAR
												{$$ = boxVSlider($3,$5,$7,$9,$11); }
				;
hslider			: HSLIDER LPAR string PAR argument PAR argument PAR argument PAR argument RPAR
												{$$ = boxHSlider($3,$5,$7,$9,$11); }
				;
nentry			: NENTRY LPAR string PAR argument PAR argument PAR argument PAR argument RPAR
												{$$ = boxNumEntry($3,$5,$7,$9,$11); }
				;
vgroup			: VGROUP LPAR string PAR diagram RPAR
												{$$ = boxVGroup($3, $5); }
				;
hgroup			: HGROUP LPAR string PAR diagram RPAR
												{$$ = boxHGroup($3, $5); }
				;
tgroup			: TGROUP LPAR string PAR diagram RPAR
												{$$ = boxTGroup($3, $5); }
				;

vbargraph		: VBARGRAPH LPAR string PAR argument PAR argument RPAR
												{$$ = boxVBargraph($3,$5,$7); }
				;
hbargraph		: HBARGRAPH LPAR string PAR argument PAR argument RPAR
												{$$ = boxHBargraph($3,$5,$7); }
				;


signature		: type fun LPAR typelist RPAR	{$$ = cons($1, cons($2, $4)); }
				| type fun LPAR RPAR			{$$ = cons($1, cons($2, nil)); }
				;

fun				: IDENT							{$$ = tree(yytext);}
				;

typelist		: type							{$$ = cons($1,nil); }
				| typelist PAR type				{$$ = cons($3,$1); }
                ;

rulelist		: rule							{$$ = cons($1,nil); }
				| rulelist rule					{$$ = cons($2,$1); }
				;

rule			: LPAR arglist RPAR ARROW diagram ENDDEF
												{$$ = cons($2,$5); }
				;

type			: INTCAST						{$$ = tree(0); }
				| FLOATCAST						{$$ = tree(1); }
				;

%%