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