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