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];
|
327
|
315
|
343
|
316 if (next_eof) {
|
|
317 next_eof=0;
|
|
318 error(EOFERR);
|
|
319 }
|
327
|
320 do {
|
|
321 if (chinput) {
|
|
322 if (! *chinput) {
|
|
323 chinput=0;
|
|
324 continue;
|
|
325 }
|
|
326 chptr=linebuf;
|
|
327 i=0;
|
|
328 while((*chptr++=c=*chinput++)&&(c!='\n')) {
|
|
329 if (++i > LBUFSIZE-2) error(LNERR);
|
|
330 }
|
|
331 } else {
|
|
332 lineno++;
|
|
333 glineno++;
|
|
334 chptr=linebuf;
|
|
335 i=0;
|
|
336 while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
|
|
337 if (++i > LBUFSIZE-2) error(LNERR);
|
|
338 if (c==EOF) {
|
343
|
339 next_eof=1;
|
|
340 --chptr;
|
|
341 break;
|
327
|
342 }
|
|
343 }
|
|
344 }
|
|
345 *chptr = '\0';
|
455
|
346 if (lsrc && !asmf && !macro_if_skip && linebuf[0]) {
|
|
347 gen_comment(linebuf);
|
|
348 if (inmode) {
|
561
|
349 // inline mode
|
455
|
350 int i=0;
|
|
351 int c;
|
560
|
352 char *p;
|
|
353 // should be done in some init
|
|
354 if (!st_cheap) {
|
|
355 st_cheap = cheap1 = new_cheap();
|
|
356 }
|
|
357
|
|
358 p = st_cheap->ptr;
|
455
|
359 sprintf(num,"%d: ",lineno);
|
509
|
360 parse = list3(ST_COMMENT,parse,(int)p);
|
|
361 // should contain file name
|
455
|
362 c = 0;
|
560
|
363 while((*st_cheap->ptr = num[c++]))
|
|
364 st_cheap = increment_cheap(st_cheap,&p);
|
|
365 while((c = *st_cheap->ptr = linebuf[i++])) {
|
|
366 st_cheap = increment_cheap(st_cheap,&p);
|
455
|
367 if (c=='\n') {
|
560
|
368 *st_cheap->ptr = 0;
|
|
369 st_cheap = increment_cheap(st_cheap,&p);
|
|
370 p = st_cheap->ptr;
|
509
|
371 // parse = list3(ST_COMMENT,parse,(int)p);
|
455
|
372 sprintf(num,"%d: ",lineno);
|
|
373 c = 0;
|
|
374 while((*cheap->ptr = num[c++]))
|
560
|
375 st_cheap = increment_cheap(st_cheap,&p);
|
455
|
376 }
|
|
377 }
|
|
378 }
|
|
379 }
|
327
|
380 if (*(chptr = linebuf) == '#' && !in_comment && !in_quote) {
|
|
381 if (macro_processing()) return;
|
|
382 }
|
|
383 } while(!in_quote && (macro_if_skip || linebuf[0] == '#'));
|
|
384 }
|
|
385
|
|
386 /* preprocessor directive */
|
|
387
|
|
388 /* line continuation \\ */
|
|
389
|
|
390 extern void
|
|
391 check_macro_eof()
|
|
392 {
|
|
393 int c;
|
346
|
394 // can't be in macro expansion
|
327
|
395 for(c=0;c<LBUFSIZE-3&&chptr[c];c++);
|
|
396 if (c>0&&chptr[c-1]=='\\') {
|
|
397 return;
|
|
398 } else if (c>0&&chptr[c-1]=='\n') {
|
|
399 if (c>0&&chptr[c-2]=='\\') {
|
|
400 return;
|
|
401 } else {
|
|
402 c--;
|
|
403 }
|
|
404 }
|
|
405 chptr[c] = ';';
|
|
406 chptr[c+1] = '\n';
|
|
407 chptr[c+2] = 0;
|
|
408 }
|
|
409
|
363
|
410 /* #if hoge case */
|
|
411
|
327
|
412 static void
|
|
413 macro_if()
|
|
414 {
|
599
|
415 int i,stype=type; // expr destroy type
|
327
|
416 ch= *chptr;
|
599
|
417 in_macro_if = 1; // makes undefined symbol==list2(CONST,0)
|
327
|
418 check_macro_eof();
|
|
419 getsym(0);
|
|
420 /* i=cexpr(expr(1)); #if allow undefined symbols.. */
|
|
421 i=expr(1);
|
|
422 in_macro_if = 0;
|
|
423 if (car(i)==CONST) i=cadr(i);
|
|
424 else i=0;
|
|
425 if (ch) {
|
|
426 if (chptr[-1]==ch) {
|
|
427 /* we are fall into getch(), which lost the last ch */
|
|
428 /* chptr[-1]==ch check is fanatic, but ... */
|
|
429 chptr--;
|
|
430 } else error(-1);
|
|
431 }
|
|
432 macro_if_depth = macro_if_current;
|
|
433 macro_if_skip = !i;
|
591
|
434 type=stype;
|
327
|
435 }
|
|
436
|
|
437 static int
|
|
438 macro_processing()
|
|
439 {
|
|
440 int i;
|
569
|
441 int c=0;
|
327
|
442 int mode_save;
|
469
|
443 int next;
|
327
|
444
|
|
445 ++chptr;
|
|
446 while (*chptr==' '||*chptr=='\t') ++chptr;
|
361
|
447 switch(chptr[0]*chptr[1]) {
|
|
448 case 'i'*'f':
|
|
449 if ((macroeq("ifdef") || macroeq("ifndef"))) {
|
|
450 c = (chptr[-4]=='n');
|
|
451 macro_if_current++;
|
|
452 if (!macro_if_skip) {
|
561
|
453 // try getsym in IFDEF mode to avoid symbol define
|
361
|
454 mode_save = mode; mode = IFDEF;
|
|
455 ch= *chptr;
|
|
456 i = getsym(0);
|
|
457 mode = mode_save;
|
|
458 macro_if_depth = macro_if_current;
|
|
459 macro_if_skip = (!i)^c;
|
|
460 }
|
327
|
461 return 0;
|
361
|
462 } else if (macroeq("if")) {
|
|
463 macro_if_current++;
|
|
464 if (!macro_if_skip) {
|
|
465 macro_if();
|
327
|
466 }
|
|
467 return 0;
|
|
468 }
|
361
|
469 break;
|
|
470 case 'e'*'l':
|
|
471 if (macroeq("elif")) {
|
|
472 if (macro_if_current==0) {
|
|
473 error(MCERR); /* extra #else */
|
|
474 return 0;
|
|
475 }
|
|
476 if (macro_if_current == macro_if_depth) {
|
|
477 if (!macro_if_skip || macro_if_skip==2) {
|
|
478 macro_if_skip=2;
|
|
479 return 0;
|
|
480 }
|
|
481 macro_if();
|
|
482 }
|
|
483 return 0;
|
|
484 } else if (macroeq("else")) {
|
|
485 if (macro_if_current==0) {
|
|
486 error(MCERR); /* extra #else */
|
327
|
487 return 0;
|
|
488 }
|
361
|
489 if (macro_if_current == macro_if_depth) {
|
|
490 if (macro_if_skip==2) ;
|
|
491 else if (macro_if_skip) macro_if_skip=0;
|
|
492 else macro_if_skip=1;
|
|
493 }
|
|
494 return skip_rest_of_line();
|
327
|
495 }
|
361
|
496 break;
|
|
497 case 'e'*'n':
|
|
498 if (macroeq("endif")) {
|
|
499 if (macro_if_current == macro_if_depth) {
|
|
500 macro_if_skip = 0;
|
|
501 macro_if_depth = --macro_if_current;
|
|
502 } else {
|
|
503 if (macro_if_current<=0) {
|
|
504 error(MCERR); /* extra #if */
|
|
505 return 0;
|
|
506 }
|
|
507 macro_if_current--;
|
|
508 }
|
|
509 return skip_rest_of_line();
|
|
510 }
|
327
|
511 }
|
|
512 if (macro_if_skip) return 0;
|
361
|
513 switch(chptr[0]) {
|
|
514 case 'd':
|
|
515 if (macroeq("define")) {
|
|
516 ch= *chptr;
|
|
517 macro_define0();
|
|
518 *(chptr = linebuf) = '\0';
|
|
519 return 0;
|
|
520 }
|
|
521 break;
|
|
522 case 'u':
|
|
523 if (macroeq("undef")) {
|
|
524 i=mode;
|
|
525 mode=IFDEF;
|
|
526 ch= *chptr;
|
|
527 if (getsym(0)) {
|
561
|
528 // make it EMPTY
|
361
|
529 if (nptr->sc == MACRO) {
|
|
530 nptr->sc = EMPTY;
|
|
531 } else if (nptr->sc == FMACRO) {
|
|
532 nptr->sc = EMPTY;
|
|
533 /* we cannot reclaim it's arg */
|
|
534 } else error(MCERR);
|
|
535 }
|
|
536 mode=i;
|
|
537 return 0;
|
327
|
538 }
|
361
|
539 break;
|
|
540 case 'i':
|
469
|
541 next = 1;
|
|
542 if (macroeq("include_next")|| (next=0, macroeq("include"))) {
|
361
|
543 if(filep+1 >= filestack + FILES) error(FILERR);
|
469
|
544 if ( ((filep+1)->fcb=getfname(next)) == NULL) error(FILERR);
|
361
|
545 (filep+1)->ln=lineno;
|
|
546 lineno=0;
|
|
547 ++filep;
|
|
548 *(chptr = linebuf) = '\0';
|
|
549 return 0;
|
|
550 }
|
|
551 break;
|
327
|
552 #if ASM_CODE
|
561
|
553 // deprecated, use asm function
|
361
|
554 case 'a':
|
|
555 if (c=='a'&¯oeq("asm")) {
|
|
556 if (asmf) error(MCERR);
|
|
557 asmf = 1;
|
327
|
558 getline();
|
361
|
559 while (asmf) {
|
561
|
560 printf("%s",linebuf);
|
361
|
561 getline();
|
|
562 }
|
|
563 return 0;
|
327
|
564 }
|
361
|
565 break;
|
|
566 case 'e':
|
|
567 if (macroeq("endasm")) {
|
|
568 if (!asmf) error(MCERR);
|
|
569 asmf = 0;
|
|
570 return 0;
|
|
571 }
|
|
572 break;
|
327
|
573 #endif
|
361
|
574 case ' ': case '\t':
|
327
|
575 getline();
|
361
|
576 return 0;
|
|
577 }
|
|
578 error(MCERR);
|
327
|
579 return 0;
|
|
580 }
|
|
581
|
|
582 extern int
|
|
583 macroeq(char *s)
|
|
584 {
|
|
585 char *p;
|
|
586
|
|
587 for (p = chptr; *s;) if (*s++ != *p++) return 0;
|
|
588 chptr = p;
|
|
589 return 1;
|
|
590 }
|
|
591
|
|
592 /* macro interpreter */
|
|
593
|
561
|
594 /* generate macro define */
|
|
595
|
327
|
596 extern void
|
|
597 macro_define(char *macro)
|
|
598 {
|
|
599 char *chptr_save;
|
|
600 int chsave;
|
|
601
|
|
602 chptr_save = chptr;
|
|
603 chsave = ch;
|
|
604 chptr = macro;
|
|
605 ch= *chptr++;
|
|
606 macro_define0();
|
|
607 chptr = chptr_save;
|
|
608 ch = chsave;
|
|
609 }
|
|
610
|
363
|
611 /* macro define from chptr */
|
|
612
|
327
|
613 static void
|
|
614 macro_define0()
|
|
615 {
|
|
616 int i,args,c;
|
346
|
617 char **body;
|
327
|
618
|
|
619 i=mode;
|
|
620 mode=MDECL;
|
|
621 // ch= *chptr; ??
|
|
622 // fprintf(stderr,"macro def: ch %c *chptr %c\n",ch,*chptr);
|
|
623 getsym(0);
|
|
624 // fprintf(stderr,"macro def: %s =>",name);
|
|
625 if (nptr->sc != EMPTY) { /* override existing macro */
|
|
626 }
|
|
627 args = 0;
|
|
628 if (ch=='(') {
|
|
629 nptr->sc = FMACRO;
|
346
|
630 args = macro_args(&chptr);
|
327
|
631 } else {
|
|
632 nptr->sc = MACRO;
|
|
633 nptr->ty = -1;
|
|
634 }
|
|
635 // equal is allowed for -Dhoge=aho option
|
534
|
636 // if (ch=='=') chptr++;
|
327
|
637 while((c=*chptr)==' '||c=='\t') chptr++;
|
346
|
638 nptr->dsp = list2((int)cheap->ptr,args); /* macro body */
|
|
639 body = (char **)&car(nptr->dsp);
|
561
|
640
|
|
641 // now copy it to the body
|
346
|
642 while ((*cheap->ptr = c = *chptr++)
|
327
|
643 && c != '\n') {
|
346
|
644 cheap = increment_cheap(cheap,body);
|
327
|
645 if (c=='/'&&chptr[0]=='/') {
|
351
|
646 cheap->ptr--;
|
|
647 *cheap->ptr = '\0';
|
|
648 while(*chptr++); break;
|
327
|
649 } else if (c=='/'&&chptr[0]=='*') {
|
346
|
650 cheap->ptr--; chptr++;
|
327
|
651 while((c = *chptr++)) {
|
|
652 if (c=='*'&&chptr[0]=='/') {
|
|
653 c = *chptr++; break;
|
|
654 }
|
|
655 }
|
|
656 if (!c) break;
|
|
657 } else if (c=='\\' && (*chptr=='\n'||*chptr==0)) {
|
|
658 chptr++;
|
346
|
659 cheap->ptr--;
|
327
|
660 getline();
|
|
661 }
|
|
662 }
|
346
|
663 if (c=='\n') {
|
|
664 *cheap->ptr = '\0';
|
|
665 }
|
|
666 cheap = increment_cheap(cheap,body);
|
327
|
667 // fprintf(stderr,"%s\n",(char *)car(nptr->dsp));
|
|
668 mode=i;
|
|
669 }
|
|
670
|
|
671 // create function macro argument list
|
|
672 // return list2((char*)arg,next)
|
|
673
|
|
674 static int
|
346
|
675 macro_args(char **pchptr)
|
327
|
676 {
|
|
677 int c;
|
|
678 int in_quote = 0;
|
|
679 int in_wquote = 0;
|
|
680 int plevel = 0;
|
346
|
681 char **body;
|
327
|
682 char *chptr = *pchptr;
|
346
|
683 int args = list2((int)cheap->ptr,0);
|
|
684 body = (char **)&car(args);
|
327
|
685 for(;;) {
|
346
|
686 *cheap->ptr = c = *chptr++;
|
|
687 cheap = increment_cheap(cheap,body);
|
539
|
688 if (c=='\\') {
|
|
689 if (*chptr=='\n') {
|
|
690 cheap->ptr--;
|
|
691 getline();
|
|
692 chptr = *pchptr;
|
|
693 continue;
|
|
694 }
|
|
695 }
|
327
|
696 if (!c) {
|
|
697 chptr--;
|
|
698 error(MCERR);
|
|
699 *pchptr = chptr;
|
|
700 return reverse0(args);
|
|
701 }
|
|
702 if (in_quote) {
|
|
703 if (c=='\\') {
|
|
704 if (*chptr != '\n') {
|
346
|
705 *cheap->ptr = *chptr++;
|
|
706 cheap = increment_cheap(cheap,body);
|
327
|
707 } else {
|
|
708 getline();
|
539
|
709 chptr = *pchptr;
|
327
|
710 }
|
|
711 } else if (c=='\'') {
|
|
712 in_quote = 0;
|
|
713 }
|
|
714 } else if (in_wquote) {
|
|
715 if (c=='\\') {
|
|
716 if (*chptr !='\n') {
|
346
|
717 *cheap->ptr = *chptr++;
|
|
718 cheap = increment_cheap(cheap,body);
|
327
|
719 } else {
|
346
|
720 *cheap->ptr = '\n';
|
327
|
721 getline();
|
539
|
722 chptr = *pchptr;
|
327
|
723 }
|
|
724 } else if (c=='"') {
|
|
725 in_wquote = 0;
|
|
726 }
|
|
727 } else if (c=='"') {
|
|
728 in_wquote = 1;
|
|
729 } else if (c=='\'') {
|
|
730 in_quote = 1;
|
|
731 } if (plevel==0) {
|
|
732 if (c==',') {
|
346
|
733 cheap->ptr[-1] = 0;
|
|
734 args = list2((int)cheap->ptr,args);
|
|
735 body = (char **)&car(args);
|
327
|
736 } else if (c==')') {
|
346
|
737 cheap->ptr[-1] = 0;
|
327
|
738 break;
|
|
739 } else if (c=='(') {
|
|
740 plevel++;
|
|
741 } else if (c=='\\') {
|
|
742 if (*chptr=='\n') {
|
346
|
743 cheap->ptr--;
|
327
|
744 getline();
|
539
|
745 chptr = *pchptr;
|
327
|
746 }
|
|
747 // } else if (c==' '||c=='\t') {
|
346
|
748 // cheap->ptr--;
|
327
|
749 } else if (c=='\n') {
|
346
|
750 cheap->ptr--;
|
327
|
751 getline();
|
|
752 chptr = *pchptr;
|
|
753 }
|
|
754 } else if (c==')') {
|
|
755 plevel--;
|
|
756 } else if (c=='(') {
|
|
757 plevel++;
|
|
758 } else if (c=='\n') {
|
346
|
759 cheap->ptr--;
|
327
|
760 getline();
|
|
761 chptr = *pchptr;
|
|
762 }
|
|
763 }
|
|
764 *pchptr = chptr;
|
|
765 return reverse0(args);
|
|
766 }
|
|
767
|
|
768 /* output macro expansion result into macrobuf (macropp) */
|
|
769
|
|
770 static int
|
|
771 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
|
|
772 {
|
|
773 int args,sargs,values,evalues;
|
|
774 char *macro;
|
|
775
|
561
|
776 // make argument list
|
327
|
777 sargs = args = cadr(nptr->dsp);
|
346
|
778 values = macro_args(pchptr);
|
327
|
779 if (pchptr==&chptr) {
|
|
780 ch = *chptr++;
|
|
781 }
|
561
|
782 // eval all argument list
|
327
|
783 evalues = 0;
|
|
784 while(values) {
|
|
785 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
|
|
786 values = cadr(values);
|
|
787 }
|
561
|
788 // define all arguments locally
|
|
789 // #define arg0 arg0_value
|
|
790 // #define arg1 arg2_value ....
|
327
|
791 evalues = reverse0(evalues);
|
359
|
792 enter_scope();
|
327
|
793 while(args) {
|
553
|
794 mappend0(reverse0(car(evalues)),¯o);
|
346
|
795 local_define((char *)car(args),macro);
|
327
|
796 args = cadr(args);
|
|
797 evalues = cadr(evalues);
|
|
798 }
|
561
|
799 // process body replacement
|
327
|
800 macro = (char *)car(nptr->dsp);
|
|
801 macrop = macro_eval(macrop,macro,list2((int)macro,history));
|
|
802 args = sargs;
|
561
|
803 // unbind arguments
|
359
|
804 leave_scope();
|
327
|
805 return macrop;
|
|
806 }
|
|
807
|
|
808 static void
|
|
809 local_define(char *macro,char *value)
|
|
810 {
|
359
|
811 NMTBL *nptr0,*nlist;
|
327
|
812 while(*macro==' '||*macro=='\t') macro++;
|
359
|
813 nptr0 = name_space_search(nlist=get_name(macro,0,DEF),MACRO);
|
|
814 nptr0 = make_local_scope(nlist,nptr0,MACRO);
|
|
815 nptr0->nm = value;
|
327
|
816 }
|
|
817
|
363
|
818 /*
|
|
819 Evaluate macro string.
|
|
820 reuslt: list2("replaced string",next)
|
561
|
821 history is necessary to avoid recursion
|
363
|
822 */
|
|
823
|
327
|
824 static int
|
|
825 macro_eval(int macrop,char *body0,int history)
|
|
826 {
|
350
|
827 int c,len;
|
327
|
828 int in_quote = 0;
|
|
829 int in_wquote = 0;
|
|
830 char *macro;
|
|
831 char *body = body0;
|
346
|
832 char **expand;
|
327
|
833 NMTBL *nptrm;
|
346
|
834 macrop = list2((int)cheap->ptr,macrop);
|
|
835 expand = (char **)&car(macrop);
|
327
|
836 for(; (c = *body++) ;) {
|
|
837 if (in_quote) {
|
|
838 if (c=='\\') {
|
346
|
839 *cheap->ptr = c; c = *body++;
|
|
840 cheap = increment_cheap(cheap,expand);
|
327
|
841 } else if (c=='\'') {
|
|
842 in_quote = 0;
|
|
843 }
|
|
844 } else if (in_wquote) {
|
|
845 if (c=='\\') {
|
346
|
846 *cheap->ptr = c; c = *body++;
|
|
847 cheap = increment_cheap(cheap,expand);
|
327
|
848 } else if (c=='"') {
|
|
849 in_wquote = 0;
|
|
850 }
|
|
851 } else if (c=='"') {
|
|
852 in_wquote = 1;
|
|
853 } else if (c=='\'') {
|
|
854 in_quote = 1;
|
|
855 } else if (c=='#' && *body=='#') {
|
540
|
856 mconcat = 1;
|
561
|
857 // name concatenation. flag only. remove and re-evaluate
|
540
|
858 // in the top level. (and skip space)
|
327
|
859 } else if (alpha(c)) {
|
561
|
860 // find a name
|
349
|
861 body--; // ungetc
|
350
|
862 nptrm = get_name(body,&len,NONDEF);
|
|
863 if (!nptrm) {
|
|
864 while((*cheap->ptr = *body++) && len--)
|
|
865 cheap = increment_cheap(cheap,expand);
|
|
866 body--;
|
|
867 continue;
|
|
868 }
|
|
869 body += len;
|
349
|
870 c = *body;
|
347
|
871 nptrm = name_space_search(nptrm,MACRO);
|
327
|
872 macro = (char *)car(nptrm->dsp);
|
561
|
873 // if (check_recurse(macro,history)) goto skip;
|
347
|
874 switch(nptrm->sc) {
|
|
875 case FMACRO:
|
327
|
876 if (c==' '||c=='\t') {
|
|
877 while (c==' '||c=='\t') c=*body++;
|
|
878 body--;
|
|
879 }
|
|
880 if(c!='(') error(MCERR);
|
349
|
881 *cheap->ptr = 0;
|
346
|
882 cheap = increment_cheap(cheap,expand);
|
|
883 body++;
|
327
|
884 macrop = macro_function(macrop,&body,nptrm,
|
|
885 list2((int)macro,history));
|
346
|
886 macrop = list2((int)cheap->ptr,macrop);
|
|
887 expand = (char **)&(car(macrop));
|
347
|
888 break;
|
|
889 case MACRO:
|
|
890 if (neqname(nptrm->nm,macro)) {
|
|
891 if (macro[0]==0) continue;
|
|
892 *cheap->ptr = 0;
|
|
893 cheap = increment_cheap(cheap,expand);
|
|
894 macrop=macro_eval(macrop,macro,list2((int)macro,history));
|
|
895 macrop = list2((int)cheap->ptr,macrop);
|
|
896 expand = (char **)&(car(macrop));
|
|
897 break;
|
|
898 }
|
|
899 default:
|
346
|
900 macro = nptrm->nm;
|
561
|
901 // skip:
|
347
|
902 case LMACRO:
|
349
|
903 while((*cheap->ptr = *macro++)/* && len-- */)
|
346
|
904 cheap = increment_cheap(cheap,expand);
|
327
|
905 }
|
|
906 continue;
|
|
907 }
|
346
|
908 *cheap->ptr = c;
|
|
909 cheap = increment_cheap(cheap,expand);
|
327
|
910 }
|
346
|
911 *cheap->ptr = 0;
|
|
912 cheap = increment_cheap(cheap,expand);
|
327
|
913 return macrop;
|
|
914 }
|
|
915
|
363
|
916 /*
|
|
917 cancat list2("string",next) into cheap.
|
|
918 result overwrited by next cheap allocation
|
|
919 */
|
327
|
920
|
553
|
921 static char *
|
|
922 mappend0(int lists,char **result)
|
327
|
923 {
|
|
924 char *p;
|
346
|
925 *result = cheap->ptr;
|
348
|
926 for(;lists;lists = cadr(lists)) {
|
327
|
927 p = (char *)car(lists);
|
347
|
928 for(;(*cheap->ptr=*p++);cheap = increment_cheap(cheap,0)) {
|
346
|
929 // in_quote + \n case ? should be \n.
|
350
|
930 if (p[-1]=='\n') cheap->ptr[0]=' ';
|
346
|
931 }
|
327
|
932 }
|
347
|
933 cheap = increment_cheap(cheap,0);
|
346
|
934 return *result;
|
327
|
935 }
|
|
936
|
553
|
937 // do not replace \n
|
|
938 extern char *
|
|
939 mappend(int lists,char **result)
|
|
940 {
|
|
941 char *p;
|
|
942 *result = cheap->ptr;
|
|
943 for(;lists;lists = cadr(lists)) {
|
|
944 p = (char *)car(lists);
|
|
945 for(;(*cheap->ptr=*p++);cheap = increment_cheap(cheap,0)) {
|
|
946 // in_quote + \n case ? should be \n.
|
|
947 // if (p[-1]=='\n') cheap->ptr[0]=' ';
|
|
948 }
|
|
949 }
|
|
950 cheap = increment_cheap(cheap,0);
|
|
951 return *result;
|
|
952 }
|
|
953
|
327
|
954 /* end */
|