Added processing for ss_eff_fader. Also, yacc code for fader effects
[ahxm.git] / compiler.l
blob514759012ac8ca1dd5c5228baf88c59eb5f90dd2
1 %{
2 /*
4     Ann Hell Ex Machina - Music Software
5     Copyright (C) 2003/2004 Angel Ortega <angel@triptico.com>
7     compiler.l - Scripting language [F]lex lexer
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License
11     as published by the Free Software Foundation; either version 2
12     of the License, or (at your option) any later version.
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23     http://www.triptico.com
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math.h>
31 #include "y.tab.h"
33 void yyerror(char * s);
34 int yy_input_for_flex(char * buf, int max);
36 /* the notes */
37 static char * ascii_notes="ccddeffggaab";
39 /* redefinition of input function for GNU Flex */
40 #undef YY_INPUT
41 #define YY_INPUT(b,r,m) (r=yy_input_for_flex(b,m))
43 #define MAX_INPUTS 256
45 struct code_stack
47         char * code;    /* code string */
48         int offset;     /* current offset */
49         int times;      /* times to be fed to lexer/parser */
50         int dyn;        /* if set, free() after usage */
53 static struct code_stack _code_stack[MAX_INPUTS];
54 static int _code_stack_i=-1;
56 /* dynamic string manipulation macros */
57 #ifndef ds_init
58 struct ds { char * d; int p; int s; };
59 #define ds_init(x) do { x.d=(char *)0; x.p=x.s=0; } while(0)
60 #define ds_rewind(x) x.p=0;
61 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
62 #define ds_redim(x) do { if(x.p >= x.s) x.d=realloc(x.d, x.s += 32); } while(0)
63 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++]=c; } while(0)
64 #define ds_pokes(x,t) do { char *p=t; while(*p) ds_poke(x, *p++); } while(0)
65 #endif /* ds_init */
67 /* block dynstring */
68 static struct ds _blk;
70 /* count of parentheses */
71 static int _blk_i;
75 DIGIT           [0-9]
76 P_INTEGER       {DIGIT}+
77 S_INTEGER       [-+]{P_INTEGER}
78 P_REAL          {DIGIT}*[\.]?{DIGIT}+
79 S_REAL          [-+]{P_REAL}
81 NOTE_P          [a-g]
82 NOTE_T3         \/3
83 NOTE_T5         \/5
85 MARKNAME        [-a-zA-Z0-9_]+
86 NEW_MARK        \^{MARKNAME}
87 GOTO_MARK       @{MARKNAME}
88 ASSERT_MARK     !{MARKNAME}
90 BLOCKNAME       [-a-zA-Z0-9_]+
92 WSPACE          [ \t\n]+
94 %x REM XC BLK
98 {P_INTEGER}             {
99                                 /* integers without sign */
100                                 yylval.i=atoi(yytext);
101                                 return(P_INTEGER);
102                         }
103 {S_INTEGER}             {
104                                 /* signed integers */
105                                 yylval.i=atoi(yytext);
106                                 return(S_INTEGER);
107                         }
108 {P_REAL}                {
109                                 /* real numbers without sign */
110                                 yylval.d=atof(yytext);
111                                 return(P_REAL);
112                         }
113 {S_REAL}                {
114                                 /* signel real numbers */
115                                 yylval.d=atof(yytext);
116                                 return(S_REAL);
117                         }
119 {NOTE_P}                {
120                                 /* note pitch */
121                                 yylval.i=strchr(ascii_notes, *yytext) - ascii_notes;
122                                 return(NOTE_P);
123                         }
124 {NOTE_T3}               { return(NOTE_T3); }
125 {NOTE_T5}               { return(NOTE_T5); }
127 {NEW_MARK}              {
128                                 /* create new mark */
129                                 yylval.p=yytext + 1;
130                                 return(NEW_MARK);
131                         }
132 {GOTO_MARK}             {
133                                 /* go to mark */
134                                 yylval.p=yytext + 1;
135                                 return(GOTO_MARK);
136                         }
137 {ASSERT_MARK}           {
138                                 /* assert mark */
139                                 yylval.p=yytext + 1;
140                                 return(ASSERT_MARK);
141                         }
143 {WSPACE}                { ; /* ignore blanks */ }
145 \/\*                    { BEGIN REM; /* C-like comments */ }
146 <REM>\*\/               { BEGIN 0; }
147 <REM>.                  { ; /* drop anything inside a comment */ }
149 \{                      { BEGIN XC; /* start of extended commands */ }
150 <XC>\"[^\"]*\"          {
151                                 /* double-quoted string */
152                                 yytext[yyleng - 1]='\0';
153                                 yylval.p=strdup(yytext + 1);
154                                 return(XC_STR);
155                         }
156 <XC>\'[^\']*\'          {
157                                 /* single-quoted string */
158                                 yytext[yyleng - 1]='\0';
159                                 yylval.p=strdup(yytext + 1);
160                                 return(XC_STR);
161                         }
162 <XC>{P_INTEGER}         {
163                                 yylval.i=atoi(yytext);
164                                 return(P_INTEGER);
165                         }
166 <XC>{S_INTEGER}         {
167                                 yylval.i=atoi(yytext);
168                                 return(S_INTEGER);
169                         }
170 <XC>{P_REAL}            {
171                                 yylval.d=atof(yytext);
172                                 return(P_REAL);
173                         }
174 <XC>{S_REAL}            {
175                                 yylval.d=atof(yytext);
176                                 return(S_REAL);
177                         }
178 <XC>{P_REAL}s           {
179                                 /* frame count, in seconds */
180                                 yytext[yyleng - 1]='\0';
181                                 yylval.i=atof(yytext) * 1000;
182                                 return(XC_MSECS);
183                         }
184 <XC>{P_INTEGER}ms       {
185                                 /* frame count, in milliseconds */
186                                 yytext[yyleng - 2]='\0';
187                                 yylval.i=atoi(yytext);
188                                 return(XC_MSECS);
189                         }
190 <XC>{NOTE_P}[\#\&]?{P_INTEGER} {
191                                 char * ptr=yytext;
193                                 /* process note */
194                                 yylval.i=strchr(ascii_notes, *ptr) - ascii_notes;
195                                 ptr++;
197                                 /* process optional sharps or flats */
198                                 if(*ptr == '#')
199                                 {
200                                         yylval.i++;
201                                         ptr++;
202                                 }
203                                 else
204                                 if(*ptr == '&')
205                                 {
206                                         yylval.i--;
207                                         ptr++;
208                                 }
210                                 /* process octave */
211                                 yylval.i += atoi(ptr) * 12;
213                                 return(XC_ABSNOTE);
214                         }
216 <XC>wav                 { return(SS_WAV); }
217 <XC>pat                 { return(SS_PAT); }
218 <XC>sustain             { return(SS_SUSTAIN); }
220 <XC>delay               { return(SS_EFF_DELAY); }
221 <XC>echo                { return(SS_EFF_ECHO); }
222 <XC>comb                { return(SS_EFF_COMB); }
223 <XC>allpass             { return(SS_EFF_ALLPASS); }
224 <XC>flanger             { return(SS_EFF_FLANGER); }
225 <XC>wobble              { return(SS_EFF_WOBBLE); }
226 <XC>square_wobble       { return(SS_EFF_SQWOBBLE); }
227 <XC>fader               { return(SS_EFF_FADER); }
228 <XC>reverb              { return(SS_EFF_REVERB); }
230 <XC>\}                  { BEGIN 0; }
231 <XC>{WSPACE}            { ; /* ignore blanks */ }
232 <XC>.                   { return(*yytext); }
234 \(                      {
235                                 /* start of block */
236                                 ds_init(_blk);
237                                 _blk_i=1;
238                                 BEGIN BLK;
239                         }
240 <BLK>\(                 { _blk_i++; ds_poke(_blk, *yytext); }
241 <BLK>\)                 {
242                                 /* one parentheses less */
243                                 _blk_i--;
245                                 /* ok with block? */
246                                 if(_blk_i == 0)
247                                 {
248                                         ds_poke(_blk, '\0');
249                                         yylval.p=_blk.d;
251                                         BEGIN 0;
252                                         return(BLOCK);
253                                 }
254                                 else
255                                         ds_poke(_blk, ')');
256                         }
257 <BLK>.                  { ds_poke(_blk, *yytext); }
259 =[ \t\n]*{BLOCKNAME}    {
260                                 /* block assignation */
261                                 yylval.p=yytext + 1;
263                                 /* skip (possible) spaces between
264                                    the = and the blockname; this lex
265                                    rule is written this way to avoid
266                                    having a global match for {BLOCKNAME},
267                                    that could swallow notes and such,
268                                    being mismatched as blocknames */
269                                 while(*yylval.p == ' ' ||
270                                       *yylval.p == '\n' ||
271                                       *yylval.p == '\t')
272                                         yylval.p++;
274                                 return(BLK_ASSIGN);
275                         }
276 \${BLOCKNAME}           {
277                                 /* block insertion */
278                                 yylval.p=yytext + 1;
279                                 return(BLK_INSERT);
280                         }
281 `.+`                    {
282                                 /* file inclusion */
283                                 yytext[yyleng - 1]='\0';
284                                 yylval.p=yytext + 1;
285                                 return(FILE_INSERT);
286                         }
288 .                       { return(*yytext); }
292 int yywrap(void) { return(1); }
294 char * ds_load(char * file)
296         struct ds s;
297         int c;
298         FILE * f;
300         if((f=fopen(file, "r")) == NULL)
301                 return(NULL);
303         ds_init(s);
305         while((c=fgetc(f)) != EOF)
306                 ds_poke(s, c);
308         ds_poke(s, '\0');
309         fclose(f);
311         return(s.d);
315 int push_code(char * code, int times, int dyn)
317         struct code_stack * cs;
319         if(_code_stack_i == MAX_INPUTS)
320                 return(0);
322         _code_stack_i++;
323         cs=&_code_stack[_code_stack_i];
325         cs->code=code;
326         cs->offset=0;
327         cs->times=times;
328         cs->dyn=dyn;
330         return(1);
334 int push_code_from_file(char * file)
336         char * c;
338         if((c=ds_load(file)) == NULL)
339                 return(0);
341         push_code(c, 1, 1);
342         return(1);
346 int code_getchar(void)
348         struct code_stack * cs;
349         int c='\0';
351         while(c == '\0' && _code_stack_i > -1)
352         {
353                 /* get current char */
354                 cs=&_code_stack[_code_stack_i];
355                 c=cs->code[cs->offset++];
357                 /* end of code? */
358                 if(c == '\0')
359                 {
360                         /* decrease times */
361                         if(--cs->times)
362                         {
363                                 /* rewind */
364                                 cs->offset=0;
365                         }
366                         else
367                         {
368                                 /* free if needed */
369                                 if(cs->dyn)
370                                         free(cs->code);
372                                 /* move to previous */
373                                 _code_stack_i--;
374                         }
376                         /* in any case, return a separator */
377                         c='\n';
378                 }
379         }
381         return(c);
385 int yy_input_for_flex(char * buf, int max)
387         buf[0]=code_getchar();
389         return(buf[0] == '\0' ? 0 : 1);