A new script, gigdump2ahs.pl, to convert the output from gigdump to
[ahxm.git] / compiler.l
blob82536f6448e4ee8f3d26808e3c9140e8b6d54c3c
1 %{
2 /*
4     Ann Hell Ex Machina - Music Software
5     Copyright (C) 2003/2005 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 ds_blk;
70 /* count of parentheses */
71 static int ds_blk_i;
73 /* line number */
74 int yyline=0;
78 DIGIT           [0-9]
79 P_INTEGER       {DIGIT}+
80 S_INTEGER       [-+]{P_INTEGER}
81 P_REAL          {DIGIT}*[\.]?{DIGIT}+
82 S_REAL          [-+]{P_REAL}
84 NOTE_P          [a-g]
85 NOTE_T3         \/3
86 NOTE_T5         \/5
88 MARKNAME        [-a-zA-Z0-9_]+
89 NEW_MARK        \^{MARKNAME}
90 GOTO_MARK       @{MARKNAME}
91 ASSERT_MARK     !{MARKNAME}
93 BLOCKNAME       [-a-zA-Z0-9_]+
95 WSPACE          [ \t]+
97 %x REM XC REMXC BLK
101 {P_INTEGER}             {
102                                 /* integers without sign */
103                                 yylval.i=atoi(yytext);
104                                 return(P_INTEGER);
105                         }
106 {S_INTEGER}             {
107                                 /* signed integers */
108                                 yylval.i=atoi(yytext);
109                                 return(S_INTEGER);
110                         }
111 {P_REAL}                {
112                                 /* real numbers without sign */
113                                 yylval.d=atof(yytext);
114                                 return(P_REAL);
115                         }
116 {S_REAL}                {
117                                 /* signel real numbers */
118                                 yylval.d=atof(yytext);
119                                 return(S_REAL);
120                         }
122 {NOTE_P}                {
123                                 /* note pitch */
124                                 yylval.i=strchr(ascii_notes, *yytext) - ascii_notes;
125                                 return(NOTE_P);
126                         }
127 {NOTE_T3}               { return(NOTE_T3); }
128 {NOTE_T5}               { return(NOTE_T5); }
130 {NEW_MARK}              {
131                                 /* create new mark */
132                                 yylval.p=yytext + 1;
133                                 return(NEW_MARK);
134                         }
135 {GOTO_MARK}             {
136                                 /* go to mark */
137                                 yylval.p=yytext + 1;
138                                 return(GOTO_MARK);
139                         }
140 {ASSERT_MARK}           {
141                                 /* assert mark */
142                                 yylval.p=yytext + 1;
143                                 return(ASSERT_MARK);
144                         }
146 {WSPACE}                { ; /* ignore blanks */ }
148 \n                      { yyline++; }
150 \/\*                    { BEGIN REM; /* C-like comments */ }
151 <REM>\*\/               { BEGIN 0; }
152 <REM>\n                 { yyline++; }
153 <REM>.                  { ; /* drop anything inside a comment */ }
155 \{                      { BEGIN XC; /* start of extended commands */ }
156 <XC>\"[^\"]*\"          {
157                                 /* double-quoted string */
158                                 yytext[yyleng - 1]='\0';
159                                 yylval.p=strdup(yytext + 1);
160                                 return(XC_STR);
161                         }
162 <XC>\'[^\']*\'          {
163                                 /* single-quoted string */
164                                 yytext[yyleng - 1]='\0';
165                                 yylval.p=strdup(yytext + 1);
166                                 return(XC_STR);
167                         }
168 <XC>{P_INTEGER}         {
169                                 yylval.i=atoi(yytext);
170                                 return(P_INTEGER);
171                         }
172 <XC>{S_INTEGER}         {
173                                 yylval.i=atoi(yytext);
174                                 return(S_INTEGER);
175                         }
176 <XC>{P_REAL}            {
177                                 yylval.d=atof(yytext);
178                                 return(P_REAL);
179                         }
180 <XC>{S_REAL}            {
181                                 yylval.d=atof(yytext);
182                                 return(S_REAL);
183                         }
184 <XC>{P_REAL}s           {
185                                 /* frame count, in seconds */
186                                 yytext[yyleng - 1]='\0';
187                                 yylval.d=atof(yytext) * 1000;
188                                 return(XC_MSECS);
189                         }
190 <XC>{P_REAL}ms          {
191                                 /* frame count, in milliseconds */
192                                 yytext[yyleng - 2]='\0';
193                                 yylval.d=atof(yytext);
194                                 return(XC_MSECS);
195                         }
196 <XC>{NOTE_P}[\#\&]?{P_INTEGER} {
197                                 char * ptr=yytext;
199                                 /* process note */
200                                 yylval.i=strchr(ascii_notes, *ptr) - ascii_notes;
201                                 ptr++;
203                                 /* process optional sharps or flats */
204                                 if(*ptr == '#')
205                                 {
206                                         yylval.i++;
207                                         ptr++;
208                                 }
209                                 else
210                                 if(*ptr == '&')
211                                 {
212                                         yylval.i--;
213                                         ptr++;
214                                 }
216                                 /* process octave */
217                                 yylval.i += atoi(ptr) * 12;
219                                 return(XC_ABSNOTE);
220                         }
222 <XC>wav                 { return(SS_WAV); }
223 <XC>pat                 { return(SS_PAT); }
224 <XC>sustain             { return(SS_SUSTAIN); }
225 <XC>vibrato             { return(SS_VIBRATO); }
226 <XC>channel             { return(SS_CHANNEL); }
227 <XC>vol                 { return(SS_VOL); }
229 <XC>delay               { return(SS_EFF_DELAY); }
230 <XC>echo                { return(SS_EFF_ECHO); }
231 <XC>comb                { return(SS_EFF_COMB); }
232 <XC>allpass             { return(SS_EFF_ALLPASS); }
233 <XC>flanger             { return(SS_EFF_FLANGER); }
234 <XC>wobble              { return(SS_EFF_WOBBLE); }
235 <XC>square_wobble       { return(SS_EFF_SQWOBBLE); }
236 <XC>fader               { return(SS_EFF_FADER); }
237 <XC>reverb              { return(SS_EFF_REVERB); }
238 <XC>off                 { return(SS_EFF_OFF); }
240 <XC>pitch_stretch       { return(SS_PITCH_STRETCH); }
241 <XC>time_stretch        { return(SS_TIME_STRETCH); }
243 <XC>midi_channel        { return(MIDI_CHANNEL); }
244 <XC>midi_program        { return(MIDI_PROGRAM); }
245 <XC>midi_generic        { return(MIDI_GENERIC); }
247 <XC>\/\*                { BEGIN REMXC; /* C-like comments inside XC */ }
248 <REMXC>\*\/             { BEGIN XC; /* jump back to XC processing */ }
249 <REMXC>\n               { yyline++; }
250 <REMXC>.                { ; }
252 <XC>\}                  { BEGIN 0; }
253 <XC>{WSPACE}            { ; /* ignore blanks */ }
254 <XC>\n                  { yyline++; }
255 <XC>.                   { return(*yytext); }
257 \(                      {
258                                 /* start of block */
259                                 ds_init(ds_blk);
260                                 ds_blk_i=1;
261                                 BEGIN BLK;
262                         }
263 <BLK>\(                 { ds_blk_i++; ds_poke(ds_blk, *yytext); }
264 <BLK>\)                 {
265                                 /* one parentheses less */
266                                 ds_blk_i--;
268                                 /* ok with block? */
269                                 if(ds_blk_i == 0)
270                                 {
271                                         ds_poke(ds_blk, '\0');
272                                         yylval.p=ds_blk.d;
274                                         BEGIN 0;
275                                         return(BLOCK);
276                                 }
277                                 else
278                                         ds_poke(ds_blk, ')');
279                         }
280 <BLK>\n                 { yyline++; }
281 <BLK>.                  { ds_poke(ds_blk, *yytext); }
283 =[ \t\n]*{BLOCKNAME}    {
284                                 /* block assignation */
285                                 yylval.p=yytext + 1;
287                                 /* skip (possible) spaces between
288                                    the = and the blockname; this lex
289                                    rule is written this way to avoid
290                                    having a global match for {BLOCKNAME},
291                                    that could swallow notes and such,
292                                    being mismatched as blocknames */
293                                 while(*yylval.p == ' ' ||
294                                       *yylval.p == '\n' ||
295                                       *yylval.p == '\t')
296                                         yylval.p++;
298                                 return(BLK_ASSIGN);
299                         }
300 \${BLOCKNAME}           {
301                                 /* block insertion */
302                                 yylval.p=yytext + 1;
303                                 return(BLK_INSERT);
304                         }
305 `.+`                    {
306                                 /* file inclusion */
307                                 yytext[yyleng - 1]='\0';
308                                 yylval.p=yytext + 1;
309                                 return(FILE_INSERT);
310                         }
312 .                       { return(*yytext); }
316 int yywrap(void) { return(1); }
318 char * ds_load(char * file)
320         struct ds s;
321         int c;
322         FILE * f;
324         if((f=fopen(file, "r")) == NULL)
325                 return(NULL);
327         ds_init(s);
329         while((c=fgetc(f)) != EOF)
330                 ds_poke(s, c);
332         ds_poke(s, '\0');
333         fclose(f);
335         return(s.d);
339 int push_code(char * code, int times, int dyn)
341         struct code_stack * cs;
343         if(code_stack_i == MAX_INPUTS)
344                 return(0);
346         code_stack_i++;
347         cs=&code_stack[code_stack_i];
349         cs->code=code;
350         cs->offset=0;
351         cs->times=times;
352         cs->dyn=dyn;
354         return(1);
358 int push_code_from_file(char * file)
360         char * c;
362         if((c=ds_load(file)) == NULL)
363                 return(0);
365         push_code(c, 1, 1);
366         return(1);
370 int code_getchar(void)
372         struct code_stack * cs;
373         int c='\0';
375         while(c == '\0' && code_stack_i > -1)
376         {
377                 /* get current char */
378                 cs=&code_stack[code_stack_i];
379                 c=cs->code[cs->offset++];
381                 /* end of code? */
382                 if(c == '\0')
383                 {
384                         /* decrease times */
385                         if(--cs->times)
386                         {
387                                 /* rewind */
388                                 cs->offset=0;
389                         }
390                         else
391                         {
392                                 /* free if needed */
393                                 if(cs->dyn)
394                                         free(cs->code);
396                                 /* move to previous */
397                                 code_stack_i--;
398                         }
400                         /* in any case, return a separator */
401                         c=' ';
402                 }
403         }
405         return(c);
409 int yy_input_for_flex(char * buf, int max)
411         buf[0]=code_getchar();
413         return(buf[0] == '\0' ? 0 : 1);