Deleted useless code from ss_output_write().
[ahxm.git] / compiler.l
blob478247c0584da9195e7a6356c49723c3dfae8aa5
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 _blk;
70 /* count of parentheses */
71 static int _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.i=atof(yytext) * 1000;
188                                 return(XC_MSECS);
189                         }
190 <XC>{P_INTEGER}ms       {
191                                 /* frame count, in milliseconds */
192                                 yytext[yyleng - 2]='\0';
193                                 yylval.i=atoi(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); }
226 <XC>delay               { return(SS_EFF_DELAY); }
227 <XC>echo                { return(SS_EFF_ECHO); }
228 <XC>comb                { return(SS_EFF_COMB); }
229 <XC>allpass             { return(SS_EFF_ALLPASS); }
230 <XC>flanger             { return(SS_EFF_FLANGER); }
231 <XC>wobble              { return(SS_EFF_WOBBLE); }
232 <XC>square_wobble       { return(SS_EFF_SQWOBBLE); }
233 <XC>fader               { return(SS_EFF_FADER); }
234 <XC>reverb              { return(SS_EFF_REVERB); }
236 <XC>midi_channel        { return(MIDI_CHANNEL); }
237 <XC>midi_program        { return(MIDI_PROGRAM); }
238 <XC>midi_generic        { return(MIDI_GENERIC); }
240 <XC>\/\*                { BEGIN REMXC; /* C-like comments inside XC */ }
241 <REMXC>\*\/             { BEGIN XC; /* jump back to XC processing */ }
242 <REMXC>\n               { yyline++; }
243 <REMXC>.                { ; }
245 <XC>\}                  { BEGIN 0; }
246 <XC>{WSPACE}            { ; /* ignore blanks */ }
247 <XC>\n                  { yyline++; }
248 <XC>.                   { return(*yytext); }
250 \(                      {
251                                 /* start of block */
252                                 ds_init(_blk);
253                                 _blk_i=1;
254                                 BEGIN BLK;
255                         }
256 <BLK>\(                 { _blk_i++; ds_poke(_blk, *yytext); }
257 <BLK>\)                 {
258                                 /* one parentheses less */
259                                 _blk_i--;
261                                 /* ok with block? */
262                                 if(_blk_i == 0)
263                                 {
264                                         ds_poke(_blk, '\0');
265                                         yylval.p=_blk.d;
267                                         BEGIN 0;
268                                         return(BLOCK);
269                                 }
270                                 else
271                                         ds_poke(_blk, ')');
272                         }
273 <BLK>\n                 { yyline++; }
274 <BLK>.                  { ds_poke(_blk, *yytext); }
276 =[ \t\n]*{BLOCKNAME}    {
277                                 /* block assignation */
278                                 yylval.p=yytext + 1;
280                                 /* skip (possible) spaces between
281                                    the = and the blockname; this lex
282                                    rule is written this way to avoid
283                                    having a global match for {BLOCKNAME},
284                                    that could swallow notes and such,
285                                    being mismatched as blocknames */
286                                 while(*yylval.p == ' ' ||
287                                       *yylval.p == '\n' ||
288                                       *yylval.p == '\t')
289                                         yylval.p++;
291                                 return(BLK_ASSIGN);
292                         }
293 \${BLOCKNAME}           {
294                                 /* block insertion */
295                                 yylval.p=yytext + 1;
296                                 return(BLK_INSERT);
297                         }
298 `.+`                    {
299                                 /* file inclusion */
300                                 yytext[yyleng - 1]='\0';
301                                 yylval.p=yytext + 1;
302                                 return(FILE_INSERT);
303                         }
305 .                       { return(*yytext); }
309 int yywrap(void) { return(1); }
311 char * ds_load(char * file)
313         struct ds s;
314         int c;
315         FILE * f;
317         if((f=fopen(file, "r")) == NULL)
318                 return(NULL);
320         ds_init(s);
322         while((c=fgetc(f)) != EOF)
323                 ds_poke(s, c);
325         ds_poke(s, '\0');
326         fclose(f);
328         return(s.d);
332 int push_code(char * code, int times, int dyn)
334         struct code_stack * cs;
336         if(_code_stack_i == MAX_INPUTS)
337                 return(0);
339         _code_stack_i++;
340         cs=&_code_stack[_code_stack_i];
342         cs->code=code;
343         cs->offset=0;
344         cs->times=times;
345         cs->dyn=dyn;
347         return(1);
351 int push_code_from_file(char * file)
353         char * c;
355         if((c=ds_load(file)) == NULL)
356                 return(0);
358         push_code(c, 1, 1);
359         return(1);
363 int code_getchar(void)
365         struct code_stack * cs;
366         int c='\0';
368         while(c == '\0' && _code_stack_i > -1)
369         {
370                 /* get current char */
371                 cs=&_code_stack[_code_stack_i];
372                 c=cs->code[cs->offset++];
374                 /* end of code? */
375                 if(c == '\0')
376                 {
377                         /* decrease times */
378                         if(--cs->times)
379                         {
380                                 /* rewind */
381                                 cs->offset=0;
382                         }
383                         else
384                         {
385                                 /* free if needed */
386                                 if(cs->dyn)
387                                         free(cs->code);
389                                 /* move to previous */
390                                 _code_stack_i--;
391                         }
393                         /* in any case, return a separator */
394                         c=' ';
395                 }
396         }
398         return(c);
402 int yy_input_for_flex(char * buf, int max)
404         buf[0]=code_getchar();
406         return(buf[0] == '\0' ? 0 : 1);