607
|
1 /* Micro-C Preprocessor Part */
|
327
|
2
|
607
|
3 /************************************************************************
|
|
4 ** Copyright (C) 2006 Shinji Kono
|
|
5 ** 連絡先: 琉球大学情報工学科 河野 真治
|
|
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
7 **
|
|
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
9 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
11 ** 営利利用も上記に反しない範囲で許可します。
|
|
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
13 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
14 **
|
|
15 ** Everyone is permitted to do anything on this program
|
|
16 ** including copying, modifying, improving,
|
|
17 ** as long as you don't try to pretend that you wrote it.
|
|
18 ** i.e., the above copyright notice has to appear in all copies.
|
|
19 ** Binary distribution requires original version messages.
|
|
20 ** You don't have to ask before copying, redistribution or publishing.
|
|
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
22 ***********************************************************************/
|
327
|
23 #include <stdio.h>
|
|
24 #include "mc.h"
|
|
25 #include "mc-parse.h"
|
|
26 #include "mc-macro.h"
|
456
|
27 #include "mc-codegen.h"
|
327
|
28 #include "mc-code.h"
|
|
29
|
|
30 extern struct {int fd,ln;char *name0;int inc;FILE *fcb;} *filep,filestack[FILES];
|
|
31
|
|
32 int in_macro_if = 0;
|
|
33 char *chinput;
|
|
34
|
|
35 static int mconcat=0;
|
|
36
|
|
37 static void macro_define0();
|
346
|
38 static int macro_args(char **pchptr);
|
327
|
39 static int macro_function(int macrop,char **pchptr,NMTBL *nptr,int history);
|
|
40 static void local_define(char *macro,char *value);
|
|
41 static int macro_eval(int macrop,char *body0,int history);
|
553
|
42 static char * mappend0(int lists,char **result);
|
327
|
43 static int macro_processing();
|
|
44
|
561
|
45 /*
|
|
46
|
|
47 macro_expansion(NMTBL *nptrm)
|
|
48 innput macro term (and chptr for arguments)
|
|
49 result put into cheap, and chptr is set.
|
|
50 current ch and chptr are pushed into chptr stack.
|
327
|
51
|
561
|
52 ## concatenation requires repeated replace.
|
|
53
|
|
54 In macro_function and macro_eavl,
|
|
55 expand result is put into macrop local variable.
|
|
56 list2("replaced string",next)
|
|
57 to generate result, mappend/reverse0 is necessary.
|
|
58
|
363
|
59 */
|
|
60
|
327
|
61 extern void
|
|
62 macro_expansion(NMTBL *nptrm)
|
|
63 {
|
|
64 int i = mode;
|
|
65 int macrop = 0;
|
|
66 int slfree = lfree;
|
540
|
67 int c;
|
|
68 char *macropp,*s,*t;
|
346
|
69 struct cheap scheap;
|
327
|
70 mode = STAT;
|
|
71
|
346
|
72 save_cheap(&scheap,cheap);
|
|
73
|
327
|
74 if (nptrm->sc == FMACRO) {
|
|
75 macrop=macro_function(macrop,&chptr,nptrm,0);
|
|
76 } else {
|
|
77 macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0);
|
|
78 }
|
347
|
79 cheap = reset_cheap(&scheap);
|
346
|
80 macropp = cheap->ptr;
|
561
|
81 // append result override, working cheap, but it's OK.
|
553
|
82 mappend0(reverse0(macrop),¯opp);
|
564
|
83 // cheap->ptr[-1] ='\n'; // makes some tokenize happy
|
|
84 // ## macro_result needs \n at end
|
|
85 cheap->ptr[-1] = 0; // makes some tokenize happy
|
|
86 t = cheap->ptr-2;
|
346
|
87 cheap->ptr[0] =0;
|
540
|
88 cheap = increment_cheap(cheap,¯opp);
|
327
|
89 while (mconcat) {
|
|
90 // ## re-eval macro
|
540
|
91 // if (lsrc) printf("## before %s",macropp);
|
327
|
92 mconcat = 0;
|
|
93 macrop = 0;
|
540
|
94 // remove \s**##\s*
|
561
|
95 // it is difficult to remove previous space on the fly,
|
|
96 // so multi path loop is required
|
540
|
97 for(s=t=macropp;*s;) {
|
|
98 if ((c=*s++)=='#'&&*s=='#') {
|
|
99 if (t>s-3) t=s-2; else t--;
|
|
100 while(*t<=' '&&t>macropp) t--; t++;
|
|
101 for(s++;*s && *s<=' ';) s++;
|
|
102 continue;
|
|
103 }
|
|
104 *t++=c;
|
|
105 }
|
|
106 *t++=0;
|
561
|
107 // evaluate generated result again
|
564
|
108 if (lsrc) {
|
|
109 printf("### %s\n",macropp);
|
|
110 if (t[-2]!='\n') putchar('\n');
|
|
111 }
|
346
|
112 macrop=macro_eval(macrop,macropp,0);
|
347
|
113 cheap = reset_cheap(&scheap);
|
346
|
114 macropp = cheap->ptr;
|
347
|
115 // will not override evaled list
|
553
|
116 mappend0(reverse0(macrop),¯opp);
|
564
|
117 // cheap->ptr[-1] ='\n';
|
|
118 cheap->ptr[-1] =0;
|
346
|
119 cheap->ptr[0] =0;
|
540
|
120 cheap = increment_cheap(cheap,¯opp);
|
327
|
121 }
|
346
|
122 cheap = reset_cheap(&scheap);
|
561
|
123 // genrated macro will be overwrited by cheap, but it's OK, again
|
327
|
124 mconcat = 0;
|
447
|
125 set_lfree(slfree);
|
564
|
126 if (lsrc && !asmf && nptrm->sc==FMACRO) {
|
|
127 gen_comment(macropp);
|
|
128 if (t[-2]!='\n') putchar('\n');
|
|
129 }
|
561
|
130 // push previous chptr, and change it to the generate macro
|
327
|
131 chptrsave = glist2((int)chptr,chptrsave);
|
|
132 chsave = glist2(ch,chsave);
|
346
|
133 chptr = macropp;
|
327
|
134 ch = *chptr++;
|
|
135 mode = i;
|
|
136 }
|
|
137
|
|
138 /* file inclusion */
|
|
139
|
363
|
140 /*
|
|
141 file name concatenation
|
561
|
142 on cheap
|
363
|
143 */
|
|
144
|
327
|
145 static char *
|
346
|
146 expand_file_name(char *path,char *name)
|
327
|
147 {
|
346
|
148 char *p = cheap->ptr;
|
|
149 if (! *path) return name;
|
|
150 while(( *cheap->ptr = *path++ )) cheap = increment_cheap(cheap,&p);
|
|
151 if (cheap->ptr[-1]!='/') {
|
|
152 *cheap->ptr = '/'; cheap = increment_cheap(cheap,&p);
|
|
153 }
|
|
154 while(( *cheap->ptr = *name++ )) cheap = increment_cheap(cheap,&p);
|
351
|
155 *cheap->ptr = 0;
|
|
156 cheap = increment_cheap(cheap,&p);
|
327
|
157 return p;
|
|
158 }
|
|
159
|
363
|
160 /*
|
|
161 get file name
|
|
162 <name> => name
|
|
163 current_file_name_dir / name
|
|
164 libdir / name
|
|
165 "name" => name
|
|
166 current_file_name_dir / name
|
|
167 include_path / name
|
561
|
168 (no difference?)
|
|
169 next flag ignores the first occurence.
|
363
|
170 */
|
|
171
|
540
|
172 static int
|
|
173 nameeq(char *p, char *q)
|
|
174 {
|
|
175 if (!p)
|
|
176 return 0;
|
|
177 while(*p)
|
|
178 if(*p++ != *q++) return 0;
|
|
179 return (*q==0);
|
|
180 }
|
|
181
|
327
|
182 static FILE *
|
469
|
183 getfname(int next)
|
327
|
184 {
|
|
185 int i,end='"',err=0;
|
540
|
186 char *s,*p,**pp,*name,*prev=0;
|
327
|
187 FILE *fp;
|
346
|
188 struct cheap scheap;
|
|
189 name = cheap->ptr;
|
327
|
190
|
|
191 getch();
|
|
192 if(skipspc()=='"') { end = '"';
|
|
193 } else if (ch=='<') { end = '>';
|
|
194 } else { error(INCERR); err=1;
|
|
195 }
|
|
196 for(i=0;(getch()!=end && ch!='\n');) {
|
346
|
197 *cheap->ptr = ch;
|
|
198 cheap = increment_cheap(cheap,&name);
|
327
|
199 }
|
|
200 if(ch=='\n') error(INCERR);
|
|
201 if (err) return filep->fcb;
|
351
|
202 *cheap->ptr = 0;
|
|
203 cheap = increment_cheap(cheap,&name);
|
|
204 save_cheap(&scheap,cheap);
|
327
|
205 fp = fopen(name,"r") ;
|
540
|
206 if (next && fp) { fclose(fp); fp=0; next=0; prev=name; }
|
327
|
207 if (fp) {
|
|
208 p = name;
|
|
209 } else {
|
556
|
210 // no deferenced on "" and <>?
|
|
211 for(pp=include_path; *pp;pp++) {
|
346
|
212 p = expand_file_name(*pp,name);
|
540
|
213 if(prev && nameeq(p,prev)) continue;
|
469
|
214 if ((fp = fopen(p,"r"))) {
|
|
215 if (next) {
|
540
|
216 fclose(fp); fp=0; next=0; prev=p;
|
469
|
217 continue;
|
|
218 } else
|
|
219 break ;
|
|
220 }
|
327
|
221 }
|
556
|
222 if (!fp /* && (end=='>'||filep->inc=='>') */ ) { // <> case only
|
539
|
223 for(pp=l_include_path; *pp;pp++) {
|
|
224 p = expand_file_name(*pp,name);
|
540
|
225 if(prev && nameeq(p,prev)) continue;
|
539
|
226 if ((fp = fopen(p,"r"))) {
|
|
227 if (next) {
|
540
|
228 fclose(fp); fp=0; next=0; prev=p;
|
539
|
229 continue;
|
|
230 } else
|
|
231 break ;
|
|
232 }
|
|
233 }
|
|
234 }
|
327
|
235 }
|
|
236 if(!fp) { error(FILERR); return filep->fcb; }
|
561
|
237 // we have so search current directory of the included file
|
|
238 // keep track the name
|
327
|
239 copy_current_file_dir(s=p);
|
351
|
240 // File name determined. Dispose extra copies.
|
|
241 cheap = reset_cheap(&scheap);
|
561
|
242 // Generated name needs copy.
|
346
|
243 if (p!=name) {
|
|
244 name = cheap->ptr;
|
|
245 while((*cheap->ptr = *s++)) cheap = increment_cheap(cheap,&name);
|
351
|
246 *cheap->ptr = 0;
|
|
247 cheap = increment_cheap(cheap,&name);
|
346
|
248 }
|
327
|
249 (filep+1)->inc = end;
|
346
|
250 (filep+1)->name0 = name;
|
327
|
251 return ( (filep+1)->fcb = fp );
|
|
252 }
|
|
253
|
|
254 /* line input and conversion */
|
|
255
|
|
256 static int macro_if_depth ;
|
|
257 static int macro_if_current ;
|
|
258 static int macro_if_skip ;
|
|
259
|
363
|
260 /* there may extra non-terminate comment after #if/#else directive */
|
364
|
261 /* #endif / * hoge */
|
363
|
262 /* */
|
|
263 /* */
|
|
264
|
327
|
265 static int
|
|
266 skip_rest_of_line()
|
|
267 {
|
|
268 getch();
|
|
269 do {
|
|
270 while(ch!='\n'&&ch!='\r') {
|
|
271 if (!in_comment) {
|
|
272 if (ch=='/') {
|
|
273 getch();
|
|
274 if (ch=='/') in_comment=2;
|
|
275 else if (ch=='*') {
|
|
276 in_comment=1;
|
|
277 } else continue;
|
|
278 }
|
|
279 } else if (ch=='*') {
|
|
280 getch();
|
|
281 if (ch=='/') {
|
|
282 in_comment=0;
|
|
283 return macro_if_skip?0:1;
|
|
284 }
|
|
285 else continue;
|
|
286 }
|
|
287 getch();
|
|
288 }
|
|
289 if (in_comment==1) { getline(); getch(); }
|
|
290 } while(in_comment==1);
|
|
291 in_comment=0;
|
|
292 return 0;
|
|
293 }
|
|
294
|
363
|
295 /*
|
|
296 getline from chptr or chinput (for internal source)
|
|
297 with macro processing
|
561
|
298 generate comment
|
|
299 generate ST_COMMENT parse tree, in inline mode
|
|
300 In getch, if chptr is empty, pop chptr stack, if stack is empy
|
|
301 read from fp.
|
363
|
302 */
|
|
303
|
343
|
304 static int next_eof;
|
560
|
305 struct cheap *st_cheap, *cheap1;
|
|
306 // ST_COMMENT may interfere with other inrement_cheap, so we use
|
|
307 // another cheap area.
|
343
|
308
|
327
|
309 extern void
|
|
310 getline(void)
|
|
311 {
|
|
312 int i;
|
|
313 int c;
|
455
|
314 char num[10];
|
609
|
315 char *p;
|
327
|
316
|
343
|
317 if (next_eof) {
|
|
318 next_eof=0;
|
|
319 error(EOFERR);
|
|
320 }
|
327
|
321 do {
|
|
322 if (chinput) {
|
|
323 if (! *chinput) {
|
|
324 chinput=0;
|
|
325 continue;
|
|
326 }
|
|
327 chptr=linebuf;
|
|
328 i=0;
|
|
329 while((*chptr++=c=*chinput++)&&(c!='\n')) {
|
|
330 if (++i > LBUFSIZE-2) error(LNERR);
|
|
331 }
|
|
332 } else {
|
|
333 lineno++;
|
|
334 glineno++;
|
|
335 chptr=linebuf;
|
|
336 i=0;
|
|
337 while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
|
|
338 if (++i > LBUFSIZE-2) error(LNERR);
|
609
|
339 if (c=='\r') {
|
|
340 c = getc(filep->fcb);
|
|
341 if (c == '\n') {
|
|
342 chptr[-1]='\n'; break;
|
|
343 } else {
|
|
344 // single cr equal to nl
|
|
345 ungetc(c,filep->fcb);
|
|
346 chptr[-1]=c='\n'; i--; break;
|
|
347 }
|
|
348 }
|
327
|
349 if (c==EOF) {
|
343
|
350 next_eof=1;
|
|
351 --chptr;
|
|
352 break;
|
327
|
353 }
|
|
354 }
|
|
355 }
|
|
356 *chptr = '\0';
|
455
|
357 if (lsrc && !asmf && !macro_if_skip && linebuf[0]) {
|
|
358 gen_comment(linebuf);
|
|
359 if (inmode) {
|
561
|
360 // inline mode
|
455
|
361 int i=0;
|
|
362 int c;
|
560
|
363 // should be done in some init
|
|
364 if (!st_cheap) {
|
|
365 st_cheap = cheap1 = new_cheap();
|
|
366 }
|
|
367
|
|
368 p = st_cheap->ptr;
|
455
|
369 sprintf(num,"%d: ",lineno);
|
509
|
370 parse = list3(ST_COMMENT,parse,(int)p);
|
|
371 // should contain file name
|
455
|
372 c = 0;
|
560
|
373 while((*st_cheap->ptr = num[c++]))
|
|
374 st_cheap = increment_cheap(st_cheap,&p);
|
|
375 while((c = *st_cheap->ptr = linebuf[i++])) {
|
|
376 st_cheap = increment_cheap(st_cheap,&p);
|
455
|
377 if (c=='\n') {
|
560
|
378 *st_cheap->ptr = 0;
|
|
379 st_cheap = increment_cheap(st_cheap,&p);
|
|
380 p = st_cheap->ptr;
|
509
|
381 // parse = list3(ST_COMMENT,parse,(int)p);
|
455
|
382 sprintf(num,"%d: ",lineno);
|
|
383 c = 0;
|
|
384 while((*cheap->ptr = num[c++]))
|
560
|
385 st_cheap = increment_cheap(st_cheap,&p);
|
455
|
386 }
|
|
387 }
|
|
388 }
|
|
389 }
|
609
|
390 p = chptr = linebuf; while(*p==' '||*p=='\t') p++;
|
|
391 if (*p == '#' && !in_comment && !in_quote) {
|
|
392 chptr = p;
|
327
|
393 if (macro_processing()) return;
|
|
394 }
|
|
395 } while(!in_quote && (macro_if_skip || linebuf[0] == '#'));
|
|
396 }
|
|
397
|
|
398 /* preprocessor directive */
|
|
399
|
|
400 /* line continuation \\ */
|
|
401
|
|
402 extern void
|
|
403 check_macro_eof()
|
|
404 {
|
|
405 int c;
|
346
|
406 // can't be in macro expansion
|
327
|
407 for(c=0;c<LBUFSIZE-3&&chptr[c];c++);
|
|
408 if (c>0&&chptr[c-1]=='\\') {
|
|
409 return;
|
|
410 } else if (c>0&&chptr[c-1]=='\n') {
|
|
411 if (c>0&&chptr[c-2]=='\\') {
|
|
412 return;
|
|
413 } else {
|
|
414 c--;
|
|
415 }
|
|
416 }
|
|
417 chptr[c] = ';';
|
|
418 chptr[c+1] = '\n';
|
|
419 chptr[c+2] = 0;
|
|
420 }
|
|
421
|
363
|
422 /* #if hoge case */
|
|
423
|
327
|
424 static void
|
|
425 macro_if()
|
|
426 {
|
599
|
427 int i,stype=type; // expr destroy type
|
327
|
428 ch= *chptr;
|
599
|
429 in_macro_if = 1; // makes undefined symbol==list2(CONST,0)
|
327
|
430 check_macro_eof();
|
|
431 getsym(0);
|
|
432 /* i=cexpr(expr(1)); #if allow undefined symbols.. */
|
|
433 i=expr(1);
|
|
434 in_macro_if = 0;
|
|
435 if (car(i)==CONST) i=cadr(i);
|
|
436 else i=0;
|
|
437 if (ch) {
|
|
438 if (chptr[-1]==ch) {
|
|
439 /* we are fall into getch(), which lost the last ch */
|
|
440 /* chptr[-1]==ch check is fanatic, but ... */
|
|
441 chptr--;
|
|
442 } else error(-1);
|
|
443 }
|
|
444 macro_if_depth = macro_if_current;
|
|
445 macro_if_skip = !i;
|
591
|
446 type=stype;
|
327
|
447 }
|
|
448
|
|
449 static int
|
|
450 macro_processing()
|
|
451 {
|
|
452 int i;
|
569
|
453 int c=0;
|
327
|
454 int mode_save;
|
469
|
455 int next;
|
327
|
456
|
|
457 ++chptr;
|
|
458 while (*chptr==' '||*chptr=='\t') ++chptr;
|
361
|
459 switch(chptr[0]*chptr[1]) {
|
|
460 case 'i'*'f':
|
|
461 if ((macroeq("ifdef") || macroeq("ifndef"))) {
|
|
462 c = (chptr[-4]=='n');
|
|
463 macro_if_current++;
|
|
464 if (!macro_if_skip) {
|
561
|
465 // try getsym in IFDEF mode to avoid symbol define
|
361
|
466 mode_save = mode; mode = IFDEF;
|
|
467 ch= *chptr;
|
|
468 i = getsym(0);
|
|
469 mode = mode_save;
|
|
470 macro_if_depth = macro_if_current;
|
|
471 macro_if_skip = (!i)^c;
|
|
472 }
|
327
|
473 return 0;
|
361
|
474 } else if (macroeq("if")) {
|
|
475 macro_if_current++;
|
|
476 if (!macro_if_skip) {
|
|
477 macro_if();
|
327
|
478 }
|
|
479 return 0;
|
|
480 }
|
361
|
481 break;
|
|
482 case 'e'*'l':
|
|
483 if (macroeq("elif")) {
|
|
484 if (macro_if_current==0) {
|
|
485 error(MCERR); /* extra #else */
|
|
486 return 0;
|
|
487 }
|
|
488 if (macro_if_current == macro_if_depth) {
|
|
489 if (!macro_if_skip || macro_if_skip==2) {
|
|
490 macro_if_skip=2;
|
|
491 return 0;
|
|
492 }
|
|
493 macro_if();
|
|
494 }
|
|
495 return 0;
|
|
496 } else if (macroeq("else")) {
|
|
497 if (macro_if_current==0) {
|
|
498 error(MCERR); /* extra #else */
|
327
|
499 return 0;
|
|
500 }
|
361
|
501 if (macro_if_current == macro_if_depth) {
|
|
502 if (macro_if_skip==2) ;
|
|
503 else if (macro_if_skip) macro_if_skip=0;
|
|
504 else macro_if_skip=1;
|
|
505 }
|
|
506 return skip_rest_of_line();
|
327
|
507 }
|
361
|
508 break;
|
|
509 case 'e'*'n':
|
|
510 if (macroeq("endif")) {
|
|
511 if (macro_if_current == macro_if_depth) {
|
|
512 macro_if_skip = 0;
|
|
513 macro_if_depth = --macro_if_current;
|
|
514 } else {
|
|
515 if (macro_if_current<=0) {
|
|
516 error(MCERR); /* extra #if */
|
|
517 return 0;
|
|
518 }
|
|
519 macro_if_current--;
|
|
520 }
|
|
521 return skip_rest_of_line();
|
|
522 }
|
327
|
523 }
|
|
524 if (macro_if_skip) return 0;
|
361
|
525 switch(chptr[0]) {
|
|
526 case 'd':
|
|
527 if (macroeq("define")) {
|
|
528 ch= *chptr;
|
|
529 macro_define0();
|
|
530 *(chptr = linebuf) = '\0';
|
|
531 return 0;
|
|
532 }
|
|
533 break;
|
|
534 case 'u':
|
|
535 if (macroeq("undef")) {
|
|
536 i=mode;
|
|
537 mode=IFDEF;
|
|
538 ch= *chptr;
|
|
539 if (getsym(0)) {
|
561
|
540 // make it EMPTY
|
361
|
541 if (nptr->sc == MACRO) {
|
|
542 nptr->sc = EMPTY;
|
|
543 } else if (nptr->sc == FMACRO) {
|
|
544 nptr->sc = EMPTY;
|
|
545 /* we cannot reclaim it's arg */
|
|
546 } else error(MCERR);
|
|
547 }
|
|
548 mode=i;
|
|
549 return 0;
|
327
|
550 }
|
361
|
551 break;
|
|
552 case 'i':
|
469
|
553 next = 1;
|
|
554 if (macroeq("include_next")|| (next=0, macroeq("include"))) {
|
361
|
555 if(filep+1 >= filestack + FILES) error(FILERR);
|
469
|
556 if ( ((filep+1)->fcb=getfname(next)) == NULL) error(FILERR);
|
361
|
557 (filep+1)->ln=lineno;
|
|
558 lineno=0;
|
|
559 ++filep;
|
|
560 *(chptr = linebuf) = '\0';
|
|
561 return 0;
|
|
562 }
|
|
563 break;
|
609
|
564 case 'p':
|
|
565 if (macroeq("pragma")) {
|
|
566 getline();
|
|
567 return 0;
|
|
568 }
|
|
569 break;
|
327
|
570 #if ASM_CODE
|
561
|
571 // deprecated, use asm function
|
361
|
572 case 'a':
|
609
|
573 if (macroeq("asm")) {
|
361
|
574 if (asmf) error(MCERR);
|
|
575 asmf = 1;
|
327
|
576 getline();
|
361
|
577 while (asmf) {
|
561
|
578 printf("%s",linebuf);
|
361
|
579 getline();
|
|
580 }
|
|
581 return 0;
|
327
|
582 }
|
361
|
583 break;
|
|
584 case 'e':
|
|
585 if (macroeq("endasm")) {
|
|
586 if (!asmf) error(MCERR);
|
|
587 asmf = 0;
|
|
588 return 0;
|
|
589 }
|
|
590 break;
|
327
|
591 #endif
|
609
|
592 case ' ': case '\t': case '\n': case 0:
|
327
|
593 getline();
|
361
|
594 return 0;
|
|
595 }
|
|
596 error(MCERR);
|
327
|
597 return 0;
|
|
598 }
|
|
599
|
|
600 extern int
|
|
601 macroeq(char *s)
|
|
602 {
|
|
603 char *p;
|
|
604
|
|
605 for (p = chptr; *s;) if (*s++ != *p++) return 0;
|
|
606 chptr = p;
|
|
607 return 1;
|
|
608 }
|
|
609
|
|
610 /* macro interpreter */
|
|
611
|
561
|
612 /* generate macro define */
|
|
613
|
327
|
614 extern void
|
|
615 macro_define(char *macro)
|
|
616 {
|
|
617 char *chptr_save;
|
|
618 int chsave;
|
|
619
|
|
620 chptr_save = chptr;
|
|
621 chsave = ch;
|
|
622 chptr = macro;
|
|
623 ch= *chptr++;
|
|
624 macro_define0();
|
|
625 chptr = chptr_save;
|
|
626 ch = chsave;
|
|
627 }
|
|
628
|
363
|
629 /* macro define from chptr */
|
|
630
|
327
|
631 static void
|
|
632 macro_define0()
|
|
633 {
|
|
634 int i,args,c;
|
346
|
635 char **body;
|
327
|
636
|
|
637 i=mode;
|
|
638 mode=MDECL;
|
|
639 // ch= *chptr; ??
|
|
640 // fprintf(stderr,"macro def: ch %c *chptr %c\n",ch,*chptr);
|
|
641 getsym(0);
|
|
642 // fprintf(stderr,"macro def: %s =>",name);
|
|
643 if (nptr->sc != EMPTY) { /* override existing macro */
|
|
644 }
|
|
645 args = 0;
|
|
646 if (ch=='(') {
|
|
647 nptr->sc = FMACRO;
|
346
|
648 args = macro_args(&chptr);
|
327
|
649 } else {
|
|
650 nptr->sc = MACRO;
|
|
651 nptr->ty = -1;
|
|
652 }
|
|
653 // equal is allowed for -Dhoge=aho option
|
534
|
654 // if (ch=='=') chptr++;
|
327
|
655 while((c=*chptr)==' '||c=='\t') chptr++;
|
346
|
656 nptr->dsp = list2((int)cheap->ptr,args); /* macro body */
|
|
657 body = (char **)&car(nptr->dsp);
|
561
|
658
|
|
659 // now copy it to the body
|
346
|
660 while ((*cheap->ptr = c = *chptr++)
|
327
|
661 && c != '\n') {
|
346
|
662 cheap = increment_cheap(cheap,body);
|
327
|
663 if (c=='/'&&chptr[0]=='/') {
|
351
|
664 cheap->ptr--;
|
|
665 *cheap->ptr = '\0';
|
|
666 while(*chptr++); break;
|
327
|
667 } else if (c=='/'&&chptr[0]=='*') {
|
346
|
668 cheap->ptr--; chptr++;
|
614
|
669 for(;;) {
|
|
670 c = *chptr++;
|
|
671 if (!c) {
|
|
672 getline();
|
|
673 continue;
|
|
674 }
|
327
|
675 if (c=='*'&&chptr[0]=='/') {
|
|
676 c = *chptr++; break;
|
|
677 }
|
|
678 }
|
|
679 if (!c) break;
|
|
680 } else if (c=='\\' && (*chptr=='\n'||*chptr==0)) {
|
|
681 chptr++;
|
346
|
682 cheap->ptr--;
|
327
|
683 getline();
|
|
684 }
|
|
685 }
|
346
|
686 if (c=='\n') {
|
|
687 *cheap->ptr = '\0';
|
|
688 }
|
|
689 cheap = increment_cheap(cheap,body);
|
327
|
690 // fprintf(stderr,"%s\n",(char *)car(nptr->dsp));
|
|
691 mode=i;
|
|
692 }
|
|
693
|
|
694 // create function macro argument list
|
|
695 // return list2((char*)arg,next)
|
|
696
|
|
697 static int
|
346
|
698 macro_args(char **pchptr)
|
327
|
699 {
|
|
700 int c;
|
|
701 int in_quote = 0;
|
|
702 int in_wquote = 0;
|
|
703 int plevel = 0;
|
346
|
704 char **body;
|
327
|
705 char *chptr = *pchptr;
|
346
|
706 int args = list2((int)cheap->ptr,0);
|
|
707 body = (char **)&car(args);
|
327
|
708 for(;;) {
|
346
|
709 *cheap->ptr = c = *chptr++;
|
|
710 cheap = increment_cheap(cheap,body);
|
539
|
711 if (c=='\\') {
|
|
712 if (*chptr=='\n') {
|
|
713 cheap->ptr--;
|
|
714 getline();
|
|
715 chptr = *pchptr;
|
|
716 continue;
|
|
717 }
|
|
718 }
|
327
|
719 if (!c) {
|
|
720 chptr--;
|
|
721 error(MCERR);
|
|
722 *pchptr = chptr;
|
|
723 return reverse0(args);
|
|
724 }
|
|
725 if (in_quote) {
|
|
726 if (c=='\\') {
|
|
727 if (*chptr != '\n') {
|
346
|
728 *cheap->ptr = *chptr++;
|
|
729 cheap = increment_cheap(cheap,body);
|
327
|
730 } else {
|
|
731 getline();
|
539
|
732 chptr = *pchptr;
|
327
|
733 }
|
|
734 } else if (c=='\'') {
|
|
735 in_quote = 0;
|
|
736 }
|
|
737 } else if (in_wquote) {
|
|
738 if (c=='\\') {
|
|
739 if (*chptr !='\n') {
|
346
|
740 *cheap->ptr = *chptr++;
|
|
741 cheap = increment_cheap(cheap,body);
|
327
|
742 } else {
|
346
|
743 *cheap->ptr = '\n';
|
327
|
744 getline();
|
539
|
745 chptr = *pchptr;
|
327
|
746 }
|
|
747 } else if (c=='"') {
|
|
748 in_wquote = 0;
|
|
749 }
|
|
750 } else if (c=='"') {
|
|
751 in_wquote = 1;
|
|
752 } else if (c=='\'') {
|
|
753 in_quote = 1;
|
|
754 } if (plevel==0) {
|
|
755 if (c==',') {
|
346
|
756 cheap->ptr[-1] = 0;
|
|
757 args = list2((int)cheap->ptr,args);
|
|
758 body = (char **)&car(args);
|
327
|
759 } else if (c==')') {
|
346
|
760 cheap->ptr[-1] = 0;
|
327
|
761 break;
|
|
762 } else if (c=='(') {
|
|
763 plevel++;
|
|
764 } else if (c=='\\') {
|
|
765 if (*chptr=='\n') {
|
346
|
766 cheap->ptr--;
|
327
|
767 getline();
|
539
|
768 chptr = *pchptr;
|
327
|
769 }
|
|
770 // } else if (c==' '||c=='\t') {
|
346
|
771 // cheap->ptr--;
|
327
|
772 } else if (c=='\n') {
|
346
|
773 cheap->ptr--;
|
327
|
774 getline();
|
|
775 chptr = *pchptr;
|
|
776 }
|
|
777 } else if (c==')') {
|
|
778 plevel--;
|
|
779 } else if (c=='(') {
|
|
780 plevel++;
|
|
781 } else if (c=='\n') {
|
346
|
782 cheap->ptr--;
|
327
|
783 getline();
|
|
784 chptr = *pchptr;
|
|
785 }
|
|
786 }
|
|
787 *pchptr = chptr;
|
|
788 return reverse0(args);
|
|
789 }
|
|
790
|
|
791 /* output macro expansion result into macrobuf (macropp) */
|
|
792
|
|
793 static int
|
|
794 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
|
|
795 {
|
|
796 int args,sargs,values,evalues;
|
|
797 char *macro;
|
|
798
|
561
|
799 // make argument list
|
327
|
800 sargs = args = cadr(nptr->dsp);
|
346
|
801 values = macro_args(pchptr);
|
327
|
802 if (pchptr==&chptr) {
|
|
803 ch = *chptr++;
|
|
804 }
|
561
|
805 // eval all argument list
|
327
|
806 evalues = 0;
|
|
807 while(values) {
|
|
808 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
|
|
809 values = cadr(values);
|
|
810 }
|
561
|
811 // define all arguments locally
|
|
812 // #define arg0 arg0_value
|
|
813 // #define arg1 arg2_value ....
|
327
|
814 evalues = reverse0(evalues);
|
359
|
815 enter_scope();
|
327
|
816 while(args) {
|
553
|
817 mappend0(reverse0(car(evalues)),¯o);
|
346
|
818 local_define((char *)car(args),macro);
|
327
|
819 args = cadr(args);
|
|
820 evalues = cadr(evalues);
|
|
821 }
|
561
|
822 // process body replacement
|
327
|
823 macro = (char *)car(nptr->dsp);
|
|
824 macrop = macro_eval(macrop,macro,list2((int)macro,history));
|
|
825 args = sargs;
|
561
|
826 // unbind arguments
|
359
|
827 leave_scope();
|
327
|
828 return macrop;
|
|
829 }
|
|
830
|
|
831 static void
|
|
832 local_define(char *macro,char *value)
|
|
833 {
|
359
|
834 NMTBL *nptr0,*nlist;
|
327
|
835 while(*macro==' '||*macro=='\t') macro++;
|
359
|
836 nptr0 = name_space_search(nlist=get_name(macro,0,DEF),MACRO);
|
|
837 nptr0 = make_local_scope(nlist,nptr0,MACRO);
|
|
838 nptr0->nm = value;
|
327
|
839 }
|
|
840
|
363
|
841 /*
|
|
842 Evaluate macro string.
|
|
843 reuslt: list2("replaced string",next)
|
561
|
844 history is necessary to avoid recursion
|
363
|
845 */
|
|
846
|
327
|
847 static int
|
|
848 macro_eval(int macrop,char *body0,int history)
|
|
849 {
|
350
|
850 int c,len;
|
327
|
851 int in_quote = 0;
|
|
852 int in_wquote = 0;
|
623
|
853 int string_flag = 0;
|
327
|
854 char *macro;
|
|
855 char *body = body0;
|
346
|
856 char **expand;
|
327
|
857 NMTBL *nptrm;
|
346
|
858 macrop = list2((int)cheap->ptr,macrop);
|
|
859 expand = (char **)&car(macrop);
|
327
|
860 for(; (c = *body++) ;) {
|
|
861 if (in_quote) {
|
|
862 if (c=='\\') {
|
346
|
863 *cheap->ptr = c; c = *body++;
|
|
864 cheap = increment_cheap(cheap,expand);
|
327
|
865 } else if (c=='\'') {
|
|
866 in_quote = 0;
|
|
867 }
|
|
868 } else if (in_wquote) {
|
|
869 if (c=='\\') {
|
346
|
870 *cheap->ptr = c; c = *body++;
|
|
871 cheap = increment_cheap(cheap,expand);
|
327
|
872 } else if (c=='"') {
|
|
873 in_wquote = 0;
|
|
874 }
|
|
875 } else if (c=='"') {
|
|
876 in_wquote = 1;
|
|
877 } else if (c=='\'') {
|
|
878 in_quote = 1;
|
|
879 } else if (c=='#' && *body=='#') {
|
540
|
880 mconcat = 1;
|
561
|
881 // name concatenation. flag only. remove and re-evaluate
|
540
|
882 // in the top level. (and skip space)
|
623
|
883 } else if (!mconcat && c=='#' && alpha(*body)) {
|
|
884 // turn into string next macro literal
|
|
885 string_flag = 1;
|
|
886 *cheap->ptr = '"';
|
|
887 cheap = increment_cheap(cheap,expand);
|
|
888 goto names;
|
327
|
889 } else if (alpha(c)) {
|
561
|
890 // find a name
|
349
|
891 body--; // ungetc
|
623
|
892 names:
|
350
|
893 nptrm = get_name(body,&len,NONDEF);
|
|
894 if (!nptrm) {
|
|
895 while((*cheap->ptr = *body++) && len--)
|
|
896 cheap = increment_cheap(cheap,expand);
|
|
897 body--;
|
623
|
898 if (string_flag) {
|
|
899 string_flag = 0;
|
|
900 *cheap->ptr = '"';
|
|
901 cheap = increment_cheap(cheap,expand);
|
|
902 }
|
350
|
903 continue;
|
|
904 }
|
|
905 body += len;
|
349
|
906 c = *body;
|
347
|
907 nptrm = name_space_search(nptrm,MACRO);
|
327
|
908 macro = (char *)car(nptrm->dsp);
|
561
|
909 // if (check_recurse(macro,history)) goto skip;
|
623
|
910 // string_falg = 0;
|
347
|
911 switch(nptrm->sc) {
|
|
912 case FMACRO:
|
327
|
913 if (c==' '||c=='\t') {
|
|
914 while (c==' '||c=='\t') c=*body++;
|
|
915 body--;
|
|
916 }
|
|
917 if(c!='(') error(MCERR);
|
349
|
918 *cheap->ptr = 0;
|
346
|
919 cheap = increment_cheap(cheap,expand);
|
|
920 body++;
|
327
|
921 macrop = macro_function(macrop,&body,nptrm,
|
|
922 list2((int)macro,history));
|
346
|
923 macrop = list2((int)cheap->ptr,macrop);
|
|
924 expand = (char **)&(car(macrop));
|
347
|
925 break;
|
|
926 case MACRO:
|
|
927 if (neqname(nptrm->nm,macro)) {
|
623
|
928 if (macro[0]==0) {
|
|
929 if (string_flag) {
|
|
930 string_flag = 0;
|
|
931 *cheap->ptr = '"';
|
|
932 cheap = increment_cheap(cheap,expand);
|
|
933 }
|
|
934 continue;
|
|
935 }
|
347
|
936 *cheap->ptr = 0;
|
|
937 cheap = increment_cheap(cheap,expand);
|
|
938 macrop=macro_eval(macrop,macro,list2((int)macro,history));
|
|
939 macrop = list2((int)cheap->ptr,macrop);
|
|
940 expand = (char **)&(car(macrop));
|
|
941 break;
|
|
942 }
|
|
943 default:
|
346
|
944 macro = nptrm->nm;
|
561
|
945 // skip:
|
347
|
946 case LMACRO:
|
349
|
947 while((*cheap->ptr = *macro++)/* && len-- */)
|
346
|
948 cheap = increment_cheap(cheap,expand);
|
327
|
949 }
|
623
|
950 if (string_flag) {
|
|
951 string_flag = 0;
|
|
952 *cheap->ptr = '"';
|
|
953 cheap = increment_cheap(cheap,expand);
|
|
954 }
|
327
|
955 continue;
|
|
956 }
|
346
|
957 *cheap->ptr = c;
|
|
958 cheap = increment_cheap(cheap,expand);
|
327
|
959 }
|
346
|
960 *cheap->ptr = 0;
|
|
961 cheap = increment_cheap(cheap,expand);
|
327
|
962 return macrop;
|
|
963 }
|
|
964
|
363
|
965 /*
|
|
966 cancat list2("string",next) into cheap.
|
|
967 result overwrited by next cheap allocation
|
|
968 */
|
327
|
969
|
553
|
970 static char *
|
|
971 mappend0(int lists,char **result)
|
327
|
972 {
|
|
973 char *p;
|
346
|
974 *result = cheap->ptr;
|
348
|
975 for(;lists;lists = cadr(lists)) {
|
327
|
976 p = (char *)car(lists);
|
609
|
977 for(;(*cheap->ptr = *p++);cheap = increment_cheap(cheap,result)) {
|
346
|
978 // in_quote + \n case ? should be \n.
|
350
|
979 if (p[-1]=='\n') cheap->ptr[0]=' ';
|
346
|
980 }
|
327
|
981 }
|
609
|
982 cheap = increment_cheap(cheap,result);
|
346
|
983 return *result;
|
327
|
984 }
|
|
985
|
553
|
986 // do not replace \n
|
|
987 extern char *
|
|
988 mappend(int lists,char **result)
|
|
989 {
|
|
990 char *p;
|
|
991 *result = cheap->ptr;
|
|
992 for(;lists;lists = cadr(lists)) {
|
|
993 p = (char *)car(lists);
|
609
|
994 for(;(*cheap->ptr=*p++);cheap = increment_cheap(cheap,result)) {
|
553
|
995 // in_quote + \n case ? should be \n.
|
|
996 // if (p[-1]=='\n') cheap->ptr[0]=' ';
|
|
997 }
|
|
998 }
|
609
|
999 cheap = increment_cheap(cheap,result);
|
553
|
1000 return *result;
|
|
1001 }
|
|
1002
|
327
|
1003 /* end */
|