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