3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2004 Angel Ortega <angel@triptico.com>
6 preprocessor.c - Language preprocessor
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
35 #include "preprocessor.h"
47 static struct _named_block
* _named_blocks
=NULL
;
48 static int _n_named_blocks
=0;
49 static int _named_blocks_size
=0;
56 char * ds_load(char * file
)
62 if((f
=fopen(file
, "r")) == NULL
)
67 while((c
=fgetc(f
)) != EOF
)
74 static int _set_named_block(char * name
, char * block
)
78 /* find first if block is previously defined */
79 for(n
=0;n
< _n_named_blocks
;n
++)
81 if(strcmp(name
, _named_blocks
[n
].name
) == 0)
83 free(_named_blocks
[n
].name
);
84 free(_named_blocks
[n
].block
);
89 if(n
== _n_named_blocks
)
92 if(_n_named_blocks
== _named_blocks_size
)
94 _named_blocks_size
+= 4;
96 _named_blocks
=(struct _named_block
*)
97 realloc(_named_blocks
,
99 sizeof(struct _named_block
));
105 _named_blocks
[n
].name
=name
;
106 _named_blocks
[n
].block
=block
;
112 static char * _find_named_block(char * name
)
116 for(n
=0;n
< _n_named_blocks
;n
++)
118 if(strcmp(name
, _named_blocks
[n
].name
) == 0)
119 return(_named_blocks
[n
].block
);
126 #define _isblockname(c) (isalpha(c) || c == '-' || c == '_')
128 char * preprocess(char * i
)
139 ds_init(o
); ds_init(block
); ds_init(tmp
);
147 /* pass through mode */
165 /* possible start of comment */
182 /* C style comment */
189 /* * found in C style comment */
199 /* C++ style comment */
224 /* what to do with block? */
225 ds_poke(block
, '\0');
235 ds_pokes(o
, "?bad-block-command:");
248 if(!(isspace(c
) && tmp
.p
== 0))
254 sscanf(tmp
.d
, "%d", &t
);
256 /* preprocess block */
257 ptr
=preprocess(block
.d
);
278 if(!(isspace(c
) && tmp
.p
== 0))
281 _set_named_block(tmp
.d
, block
.d
);
293 /* named block insertion */
300 if((ptr
=_find_named_block(tmp
.d
)))
314 ds_pokes(o
, "?undefined-block:");
331 if((ptr
=ds_load(tmp
.d
)) != NULL
)
344 ds_pokes(o
, "?file-not-found:");
355 /* insert line number */
360 sprintf(str
, "$%d ", ++line
+ 1);