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