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