Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / faust-0.9.47mr3 / compiler / parser / faustparser.y
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4 -*- */
2
3 /* Parser for the Faust language */
4
5 %{
6
7 #include "tree.hh"
8 #include "xtended.hh"
9 #include "boxes.hh"
10 #include "prim2.hh"
11 #include "signals.hh"
12 #include "errormsg.hh"
13 #include "sourcereader.hh"
14 #include "doc.hh"
15 #include "ppbox.hh"
16
17 #include <string>
18 #include <list>
19
20 #define YYDEBUG 1
21 #define YYERROR_VERBOSE 1
22 #define YYMAXDEPTH 100000
23
24 using namespace std;
25
26 extern char* yytext;
27 extern const char* yyfilename;
28 extern int yylineno;
29 extern int yyerr;
30 extern Tree gResult;
31 extern bool gStripDocSwitch;
32 extern bool gLstDependenciesSwitch;
33 extern bool gLstDistributedSwitch;
34 extern bool gLstMdocTagsSwitch;
35
36 extern map<Tree, set<Tree> > gMetaDataSet;
37 extern vector<Tree> gDocVector;
38
39
40 int yylex();
41
42 //----------------------------------------------------------
43 // unquote() : remove enclosing quotes and carriage return
44 // characters from string. Returns a Tree
45 //----------------------------------------------------------
46 char replaceCR(char c)
47 {
48 return (c!='\n') ? c : ' ';
49 }
50
51 Tree unquote(char* str)
52 {
53 //-----------copy unquoted filename-------------
54 char buf[512];
55 int j=0;
56
57 if (str[0] == '"') {
58 //it is a quoted string, we remove the quotes
59 for (int i=1; j<511 && str[i];) {
60 buf[j++] = replaceCR(str[i++]);
61 }
62 // remove last quote
63 if (j>0) buf[j-1] = 0;
64 } else {
65 for (int i=0; j<511 && str[i];) {
66 buf[j++] = replaceCR(str[i++]);
67 }
68 }
69 buf[j] = 0;
70
71 return tree(buf);
72 //----------------------------------------------
73 }
74
75 %}
76
77
78 %union {
79 CTree* exp;
80 char* str;
81 string* cppstr;
82 bool b;
83 }
84
85 %start program
86
87 /* With local environment (lowest priority)*/
88 %left WITH
89
90 /* Block Diagram Algebra */
91 /*%left SEQ SPLIT MIX*/
92 %right SPLIT MIX
93 %right SEQ
94 %right PAR
95 %left REC
96
97 /* Primitive boxes */
98
99 %left LT LE EQ GT GE NE
100
101 %left ADD SUB OR
102 %left MUL DIV MOD AND XOR LSH RSH
103 %left POWOP
104 %left FDELAY
105 %left DELAY1
106 %left APPL DOT
107
108 %left HASH
109
110 %token MEM
111 %token PREFIX
112
113 %token INTCAST
114 %token FLOATCAST
115 %token FFUNCTION
116 %token FCONSTANT
117 %token FVARIABLE
118
119 %token BUTTON
120 %token CHECKBOX
121 %token VSLIDER
122 %token HSLIDER
123 %token NENTRY
124 %token VGROUP
125 %token HGROUP
126 %token TGROUP
127
128 %token HBARGRAPH
129 %token VBARGRAPH
130 %token ATTACH
131
132
133 %token ACOS
134 %token ASIN
135 %token ATAN
136 %token ATAN2
137 %token COS
138 %token SIN
139 %token TAN
140
141 %token EXP
142 %token LOG
143 %token LOG10
144 %token POWFUN
145 %token SQRT
146
147 %token ABS
148 %token MIN
149 %token MAX
150
151 %token FMOD
152 %token REMAINDER
153
154 %token FLOOR
155 %token CEIL
156 %token RINT
157
158
159
160 %token RDTBL
161 %token RWTBL
162
163 %token SELECT2
164 %token SELECT3
165
166 %token INT
167 %token FLOAT
168
169
170 %token LAMBDA
171 %token DOT
172
173 %token WIRE
174 %token CUT
175 %token ENDDEF
176 %token VIRG
177 %right LPAR
178 %left RPAR
179 %right LBRAQ
180 %left RBRAQ
181 %right LCROC
182 %left RCROC
183 %token WITH
184 %token DEF
185
186 %token IMPORT
187 %token COMPONENT
188 %token LIBRARY
189 %token ENVIRONMENT
190
191 %token IPAR
192 %token ISEQ
193 %token ISUM
194 %token IPROD
195
196 %token STRING
197 %token FSTRING
198 %token IDENT
199 %token EXTRA
200
201 %token DECLARE
202
203 %token CASE
204 %token ARROW
205
206 %token VECTORIZE
207 %token SERIALIZE
208 %token HASH
209 %token RATE
210 %token UPSAMPLE
211 %token DOWNSAMPLE
212
213 /* Begin and End tags for documentations, equations and diagrams */
214 %token BDOC
215 %token EDOC
216 %token BEQN
217 %token EEQN
218 %token BDGM
219 %token EDGM
220 %token BLST
221 %token ELST
222 %token BMETADATA
223 %token EMETADATA
224 %token <cppstr> DOCCHAR
225 %token NOTICE
226 %token LISTING
227
228 %token LSTTRUE
229 %token LSTFALSE
230 %token LSTDEPENDENCIES
231 %token LSTMDOCTAGS
232 %token LSTDISTRIBUTED
233 %token LSTEQ
234 %token LSTQ
235
236
237 %type <exp> program
238
239 %type <exp> stmtlist
240 %type <exp> statement
241
242 %type <exp> deflist
243 %type <exp> definition
244
245 %type <exp> params
246
247 %type <exp> expression
248
249 %type <exp> defname
250 %type <exp> infixexp
251 %type <exp> primitive
252 %type <exp> argument
253 %type <exp> arglist
254
255 %type <exp> ident
256 %type <exp> name
257
258 %type <exp> ffunction
259 %type <exp> fconst
260 %type <exp> fvariable
261 %type <exp> signature
262 %type <exp> string
263 %type <exp> uqstring
264 %type <exp> fstring
265 %type <exp> type
266 %type <exp> typelist
267 %type <exp> fun
268
269 %type <exp> fpar
270 %type <exp> fseq
271 %type <exp> fsum
272 %type <exp> fprod
273
274 %type <exp> button
275 %type <exp> checkbox
276 %type <exp> vslider
277 %type <exp> hslider
278 %type <exp> nentry
279 %type <exp> vgroup
280 %type <exp> hgroup
281 %type <exp> tgroup
282
283 %type <exp> vbargraph
284 %type <exp> hbargraph
285
286 %type <exp> rule
287 %type <exp> rulelist
288
289 %type <exp> doc
290 %type <exp> docelem
291 %type <cppstr> doctxt
292 %type <exp> doceqn
293 %type <exp> docdgm
294 %type <exp> docntc
295 %type <exp> doclst
296 %type <exp> docmtd
297
298 %type <exp> lstattrlist
299 %type <exp> lstattrdef
300 %type <b> lstattrval
301
302
303
304
305
306 %% /* grammar rules and actions follow */
307
308 program : stmtlist { $$ = $1; gResult = formatDefinitions($$); }
309 ;
310
311 stmtlist : /*empty*/ { $$ = nil; }
312 | stmtlist statement { $$ = cons ($2,$1); }
313
314 deflist : /*empty*/ { $$ = nil; }
315 | deflist definition { $$ = cons ($2,$1); }
316 ;
317
318 statement : IMPORT LPAR uqstring RPAR ENDDEF { $$ = importFile($3); }
319 | DECLARE name string ENDDEF { declareMetadata($2,$3); $$ = nil; }
320 | definition { $$ = $1; }
321 | BDOC doc EDOC { declareDoc($2); $$ = nil; /* cerr << "Yacc : doc : " << *$2 << endl; */ }
322 ;
323
324 doc : /* empty */ { $$ = nil; }
325 | doc docelem { $$ = cons ($2,$1); }
326 ;
327
328 docelem : doctxt { $$ = docTxt($1->c_str()); delete $1; }
329 | doceqn { $$ = docEqn($1); }
330 | docdgm { $$ = docDgm($1); }
331 | docntc { $$ = docNtc(); }
332 | doclst { $$ = docLst(); }
333 | docmtd { $$ = docMtd($1); }
334 ;
335
336 doctxt : /* empty */ { $$ = new string(); }
337 | doctxt DOCCHAR { $$ = &($1->append(yytext)); }
338 ;
339
340 doceqn : BEQN expression EEQN { $$ = $2; }
341 ;
342
343 docdgm : BDGM expression EDGM { $$ = $2; }
344 ;
345
346 docntc : NOTICE { }
347 ;
348
349 doclst : BLST lstattrlist ELST { }
350 ;
351
352 lstattrlist : /* empty */ { }
353 | lstattrlist lstattrdef { }
354 ;
355
356 lstattrdef : LSTDEPENDENCIES LSTEQ LSTQ lstattrval LSTQ { gLstDependenciesSwitch = $4; }
357 | LSTMDOCTAGS LSTEQ LSTQ lstattrval LSTQ { gStripDocSwitch = $4; gStripDocSwitch==true ? gStripDocSwitch=false : gStripDocSwitch=true; }
358 | LSTDISTRIBUTED LSTEQ LSTQ lstattrval LSTQ { gLstDistributedSwitch = $4; }
359 ;
360
361 lstattrval : LSTTRUE { $$ = true; }
362 | LSTFALSE { $$ = false; }
363 ;
364
365 docmtd : BMETADATA name EMETADATA { $$ = $2; }
366 ;
367
368 definition : defname LPAR arglist RPAR DEF expression ENDDEF { $$ = cons($1,cons($3,$6)); }
369 | defname DEF expression ENDDEF { $$ = cons($1,cons(nil,$3)); }
370 | error ENDDEF { $$ = nil; yyerr++; }
371 ;
372
373 defname : ident { $$=$1; setDefProp($1, yyfilename, yylineno); }
374 ;
375
376 params : ident { $$ = cons($1,nil); }
377 | params PAR ident { $$ = cons($3,$1); }
378 ;
379
380 expression : expression WITH LBRAQ deflist RBRAQ { $$ = boxWithLocalDef($1,formatDefinitions($4)); }
381 | expression PAR expression { $$ = boxPar($1,$3); }
382 | expression SEQ expression { $$ = boxSeq($1,$3); }
383 | expression SPLIT expression { $$ = boxSplit($1,$3); }
384 | expression MIX expression { $$ = boxMerge($1,$3); }
385 | expression REC expression { $$ = boxRec($1,$3); }
386 | infixexp { $$ = $1; }
387 ;
388
389 infixexp : infixexp ADD infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigAdd)); }
390 | infixexp SUB infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigSub)); }
391 | infixexp MUL infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigMul)); }
392 | infixexp DIV infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigDiv)); }
393 | infixexp MOD infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigRem)); }
394 | infixexp POWOP infixexp { $$ = boxSeq(boxPar($1,$3),gPowPrim->box()); }
395 | infixexp FDELAY infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigFixDelay)); }
396 | infixexp DELAY1 { $$ = boxSeq($1,boxPrim1(sigDelay1)); }
397 | infixexp DOT ident { $$ = boxAccess($1,$3); }
398
399 | infixexp AND infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigAND)); }
400 | infixexp OR infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigOR)); }
401 | infixexp XOR infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigXOR)); }
402
403 | infixexp LSH infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigLeftShift)); }
404 | infixexp RSH infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigRightShift)); }
405
406 | infixexp LT infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigLT)); }
407 | infixexp LE infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigLE)); }
408 | infixexp GT infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigGT)); }
409 | infixexp GE infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigGE)); }
410 | infixexp EQ infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigEQ)); }
411 | infixexp NE infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigNE)); }
412
413 | infixexp HASH infixexp { $$ = boxSeq(boxPar($1,$3),boxPrim2(sigConcat)); }
414
415 | infixexp LPAR arglist RPAR %prec APPL { $$ = buildBoxAppl($1,$3); }
416 | infixexp LCROC deflist RCROC %prec APPL { $$ = boxModifLocalDef($1,formatDefinitions($3)); }
417
418 | primitive { $$ = $1; }
419 ;
420
421 primitive : INT { $$ = boxInt(atoi(yytext)); }
422 | FLOAT { $$ = boxReal(atof(yytext)); }
423
424 | ADD INT { $$ = boxInt (atoi(yytext)); }
425 | ADD FLOAT { $$ = boxReal(atof(yytext)); }
426
427 | SUB INT { $$ = boxInt ( -atoi(yytext) ); }
428 | SUB FLOAT { $$ = boxReal( -atof(yytext) ); }
429
430 | WIRE { $$ = boxWire(); }
431 | CUT { $$ = boxCut(); }
432
433 | MEM { $$ = boxPrim1(sigDelay1); }
434 | PREFIX { $$ = boxPrim2(sigPrefix); }
435
436 | INTCAST { $$ = boxPrim1(sigIntCast); }
437 | FLOATCAST { $$ = boxPrim1(sigFloatCast); }
438
439 | ADD { $$ = boxPrim2(sigAdd); }
440 | SUB { $$ = boxPrim2(sigSub); }
441 | MUL { $$ = boxPrim2(sigMul); }
442 | DIV { $$ = boxPrim2(sigDiv); }
443 | MOD { $$ = boxPrim2(sigRem); }
444 | FDELAY { $$ = boxPrim2(sigFixDelay); }
445
446 | AND { $$ = boxPrim2(sigAND); }
447 | OR { $$ = boxPrim2(sigOR); }
448 | XOR { $$ = boxPrim2(sigXOR); }
449
450 | LSH { $$ = boxPrim2(sigLeftShift); }
451 | RSH { $$ = boxPrim2(sigRightShift); }
452
453 | LT { $$ = boxPrim2(sigLT); }
454 | LE { $$ = boxPrim2(sigLE); }
455 | GT { $$ = boxPrim2(sigGT); }
456 | GE { $$ = boxPrim2(sigGE); }
457 | EQ { $$ = boxPrim2(sigEQ); }
458 | NE { $$ = boxPrim2(sigNE); }
459
460 | ATTACH { $$ = boxPrim2(sigAttach); }
461
462 | ACOS { $$ = gAcosPrim->box(); }
463 | ASIN { $$ = gAsinPrim->box(); }
464 | ATAN { $$ = gAtanPrim->box(); }
465 | ATAN2 { $$ = gAtan2Prim->box(); }
466 | COS { $$ = gCosPrim->box(); }
467 | SIN { $$ = gSinPrim->box(); }
468 | TAN { $$ = gTanPrim->box(); }
469
470 | EXP { $$ = gExpPrim->box(); }
471 | LOG { $$ = gLogPrim->box(); }
472 | LOG10 { $$ = gLog10Prim->box(); }
473 | POWOP { $$ = gPowPrim->box(); }
474 | POWFUN { $$ = gPowPrim->box(); }
475 | SQRT { $$ = gSqrtPrim->box(); }
476
477 | ABS { $$ = gAbsPrim->box(); }
478 | MIN { $$ = gMinPrim->box(); }
479 | MAX { $$ = gMaxPrim->box(); }
480
481 | FMOD { $$ = gFmodPrim->box(); }
482 | REMAINDER { $$ = gRemainderPrim->box(); }
483
484 | FLOOR { $$ = gFloorPrim->box(); }
485 | CEIL { $$ = gCeilPrim->box(); }
486 | RINT { $$ = gRintPrim->box(); }
487
488
489 | RDTBL { $$ = boxPrim3(sigReadOnlyTable); }
490 | RWTBL { $$ = boxPrim5(sigWriteReadTable); }
491
492 | SELECT2 { $$ = boxPrim3(sigSelect2); }
493 | SELECT3 { $$ = boxPrim4(sigSelect3); }
494
495 | VECTORIZE { $$ = boxPrim2(sigVectorize); }
496 | SERIALIZE { $$ = boxPrim1(sigSerialize); }
497 | HASH { $$ = boxPrim2(sigConcat); }
498
499 | UPSAMPLE { $$ = boxPrim2(sigUpSample); }
500 | DOWNSAMPLE { $$ = boxPrim2(sigDownSample); }
501
502 | LCROC RCROC { $$ = boxPrim2(sigVectorAt); }
503 | LCROC infixexp RCROC { $$ = boxSeq(boxPar(boxWire(),$2),boxPrim2(sigVectorAt)); }
504
505 | ident { $$ = $1; }
506 | SUB ident { $$ = boxSeq(boxPar(boxInt(0),$2),boxPrim2(sigSub)); }
507
508 | LPAR expression RPAR { $$ = $2; }
509 | LAMBDA LPAR params RPAR DOT LPAR expression RPAR
510 { $$ = buildBoxAbstr($3,$7); }
511
512 | CASE LBRAQ rulelist RBRAQ { $$ = boxCase(checkRulelist($3)); }
513
514 | ffunction { $$ = boxFFun($1); }
515 | fconst { $$ = $1; }
516 | fvariable { $$ = $1; }
517 | COMPONENT LPAR uqstring RPAR { $$ = boxComponent($3); }
518 | LIBRARY LPAR uqstring RPAR { $$ = boxLibrary($3); }
519 | ENVIRONMENT LBRAQ deflist RBRAQ { $$ = boxWithLocalDef(boxEnvironment(),formatDefinitions($3)); }
520
521 | button { $$ = $1; }
522 | checkbox { $$ = $1; }
523 | vslider { $$ = $1; }
524 | hslider { $$ = $1; }
525 | nentry { $$ = $1; }
526 | vgroup { $$ = $1; }
527 | hgroup { $$ = $1; }
528 | tgroup { $$ = $1; }
529 | vbargraph { $$ = $1; }
530 | hbargraph { $$ = $1; }
531
532 | fpar { $$ = $1; }
533 | fseq { $$ = $1; }
534 | fsum { $$ = $1; }
535 | fprod { $$ = $1; }
536 ;
537
538
539 ident : IDENT { $$ = boxIdent(yytext); }
540 ;
541
542 name : IDENT { $$ = tree(yytext); }
543 ;
544
545
546
547 arglist : argument { $$ = cons($1,nil); }
548 | arglist PAR argument { $$ = cons($3,$1); }
549 ;
550
551 argument : argument SEQ argument { $$ = boxSeq($1,$3); }
552 | argument SPLIT argument { $$ = boxSplit($1,$3); }
553 | argument MIX argument { $$ = boxMerge($1,$3); }
554 | argument REC argument { $$ = boxRec($1,$3); }
555 | infixexp { $$ = $1; }
556 ;
557
558 string : STRING { $$ = tree(yytext); }
559 ;
560
561 uqstring : STRING { $$ = unquote(yytext); }
562 ;
563
564 fstring : STRING { $$ = tree(yytext); }
565 | FSTRING { $$ = tree(yytext); }
566 ;
567
568 /* description of iterative expressions */
569
570 fpar : IPAR LPAR ident PAR argument PAR expression RPAR
571 { $$ = boxIPar($3,$5,$7); }
572 ;
573
574 fseq : ISEQ LPAR ident PAR argument PAR expression RPAR
575 { $$ = boxISeq($3,$5,$7); }
576 ;
577
578 fsum : ISUM LPAR ident PAR argument PAR expression RPAR
579 { $$ = boxISum($3,$5,$7); }
580 ;
581
582 fprod : IPROD LPAR ident PAR argument PAR expression RPAR
583 { $$ = boxIProd($3,$5,$7); }
584 ;
585
586
587 /* description of foreign functions */
588
589 ffunction : FFUNCTION LPAR signature PAR fstring PAR string RPAR
590 { $$ = ffunction($3,$5,$7); }
591 ;
592
593 fconst : FCONSTANT LPAR type name PAR fstring RPAR
594 { $$ = boxFConst($3,$4,$6); }
595
596 fvariable : FVARIABLE LPAR type name PAR fstring RPAR
597 { $$ = boxFVar($3,$4,$6); }
598 ;
599
600 /* Description of user interface building blocks */
601 button : BUTTON LPAR uqstring RPAR { $$ = boxButton($3); }
602 ;
603
604 checkbox : CHECKBOX LPAR uqstring RPAR { $$ = boxCheckbox($3); }
605 ;
606
607 vslider : VSLIDER LPAR uqstring PAR argument PAR argument PAR argument PAR argument RPAR
608 { $$ = boxVSlider($3,$5,$7,$9,$11); }
609 ;
610 hslider : HSLIDER LPAR uqstring PAR argument PAR argument PAR argument PAR argument RPAR
611 { $$ = boxHSlider($3,$5,$7,$9,$11); }
612 ;
613 nentry : NENTRY LPAR uqstring PAR argument PAR argument PAR argument PAR argument RPAR
614 { $$ = boxNumEntry($3,$5,$7,$9,$11); }
615 ;
616 vgroup : VGROUP LPAR uqstring PAR expression RPAR
617 { $$ = boxVGroup($3, $5); }
618 ;
619 hgroup : HGROUP LPAR uqstring PAR expression RPAR
620 { $$ = boxHGroup($3, $5); }
621 ;
622 tgroup : TGROUP LPAR uqstring PAR expression RPAR
623 { $$ = boxTGroup($3, $5); }
624 ;
625
626 vbargraph : VBARGRAPH LPAR uqstring PAR argument PAR argument RPAR
627 { $$ = boxVBargraph($3,$5,$7); }
628 ;
629 hbargraph : HBARGRAPH LPAR uqstring PAR argument PAR argument RPAR
630 { $$ = boxHBargraph($3,$5,$7); }
631 ;
632
633 /* Description of foreign functions */
634
635 signature : type fun LPAR typelist RPAR { $$ = cons($1, cons($2, $4)); }
636 | type fun LPAR RPAR { $$ = cons($1, cons($2, nil)); }
637 ;
638
639 fun : IDENT { $$ = tree(yytext); }
640 ;
641
642 typelist : type { $$ = cons($1,nil); }
643 | typelist PAR type { $$ = cons($3,$1); }
644 ;
645
646 rulelist : rule { $$ = cons($1,nil); }
647 | rulelist rule { $$ = cons($2,$1); }
648 ;
649
650 rule : LPAR arglist RPAR ARROW expression ENDDEF
651 { $$ = cons($2,$5); }
652 ;
653
654 type : INTCAST { $$ = tree(0); }
655 | FLOATCAST { $$ = tree(1); }
656 ;
657
658 %%
659