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);
|
527
|
22 extern 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);
|
509
|
261 parse = list3(ST_COMMENT,parse,(int)p);
|
|
262 // should contain file name
|
455
|
263 c = 0;
|
|
264 while((*cheap->ptr = num[c++]))
|
|
265 cheap = increment_cheap(cheap,&p);
|
|
266 while((c = *cheap->ptr = linebuf[i++])) {
|
|
267 cheap = increment_cheap(cheap,&p);
|
|
268 if (c=='\n') {
|
|
269 *cheap->ptr = 0;
|
|
270 cheap = increment_cheap(cheap,&p);
|
|
271 p = cheap->ptr;
|
509
|
272 // parse = list3(ST_COMMENT,parse,(int)p);
|
455
|
273 sprintf(num,"%d: ",lineno);
|
|
274 c = 0;
|
|
275 while((*cheap->ptr = num[c++]))
|
|
276 cheap = increment_cheap(cheap,&p);
|
|
277 }
|
|
278 }
|
|
279 }
|
|
280 }
|
327
|
281 if (*(chptr = linebuf) == '#' && !in_comment && !in_quote) {
|
|
282 if (macro_processing()) return;
|
|
283 }
|
|
284 } while(!in_quote && (macro_if_skip || linebuf[0] == '#'));
|
|
285 }
|
|
286
|
|
287 /* preprocessor directive */
|
|
288
|
|
289 /* line continuation \\ */
|
|
290
|
|
291 extern void
|
|
292 check_macro_eof()
|
|
293 {
|
|
294 int c;
|
346
|
295 // can't be in macro expansion
|
327
|
296 for(c=0;c<LBUFSIZE-3&&chptr[c];c++);
|
|
297 if (c>0&&chptr[c-1]=='\\') {
|
|
298 return;
|
|
299 } else if (c>0&&chptr[c-1]=='\n') {
|
|
300 if (c>0&&chptr[c-2]=='\\') {
|
|
301 return;
|
|
302 } else {
|
|
303 c--;
|
|
304 }
|
|
305 }
|
|
306 chptr[c] = ';';
|
|
307 chptr[c+1] = '\n';
|
|
308 chptr[c+2] = 0;
|
|
309 }
|
|
310
|
363
|
311 /* #if hoge case */
|
|
312
|
327
|
313 static void
|
|
314 macro_if()
|
|
315 {
|
|
316 int i;
|
|
317 ch= *chptr;
|
|
318 in_macro_if = 1;
|
|
319 check_macro_eof();
|
|
320 getsym(0);
|
|
321 /* i=cexpr(expr(1)); #if allow undefined symbols.. */
|
|
322 i=expr(1);
|
|
323 in_macro_if = 0;
|
|
324 if (car(i)==CONST) i=cadr(i);
|
|
325 else i=0;
|
|
326 if (ch) {
|
|
327 if (chptr[-1]==ch) {
|
|
328 /* we are fall into getch(), which lost the last ch */
|
|
329 /* chptr[-1]==ch check is fanatic, but ... */
|
|
330 chptr--;
|
|
331 } else error(-1);
|
|
332 }
|
|
333 macro_if_depth = macro_if_current;
|
|
334 macro_if_skip = !i;
|
|
335 }
|
|
336
|
|
337 static int
|
|
338 macro_processing()
|
|
339 {
|
|
340 int i;
|
|
341 int c;
|
|
342 int mode_save;
|
469
|
343 int next;
|
327
|
344
|
|
345 ++chptr;
|
|
346 while (*chptr==' '||*chptr=='\t') ++chptr;
|
361
|
347 switch(chptr[0]*chptr[1]) {
|
|
348 case 'i'*'f':
|
|
349 if ((macroeq("ifdef") || macroeq("ifndef"))) {
|
|
350 c = (chptr[-4]=='n');
|
|
351 macro_if_current++;
|
|
352 if (!macro_if_skip) {
|
|
353 mode_save = mode; mode = IFDEF;
|
|
354 ch= *chptr;
|
|
355 i = getsym(0);
|
|
356 mode = mode_save;
|
|
357 macro_if_depth = macro_if_current;
|
|
358 macro_if_skip = (!i)^c;
|
|
359 }
|
327
|
360 return 0;
|
361
|
361 } else if (macroeq("if")) {
|
|
362 macro_if_current++;
|
|
363 if (!macro_if_skip) {
|
|
364 macro_if();
|
327
|
365 }
|
|
366 return 0;
|
|
367 }
|
361
|
368 break;
|
|
369 case 'e'*'l':
|
|
370 if (macroeq("elif")) {
|
|
371 if (macro_if_current==0) {
|
|
372 error(MCERR); /* extra #else */
|
|
373 return 0;
|
|
374 }
|
|
375 if (macro_if_current == macro_if_depth) {
|
|
376 if (!macro_if_skip || macro_if_skip==2) {
|
|
377 macro_if_skip=2;
|
|
378 return 0;
|
|
379 }
|
|
380 macro_if();
|
|
381 }
|
|
382 return 0;
|
|
383 } else if (macroeq("else")) {
|
|
384 if (macro_if_current==0) {
|
|
385 error(MCERR); /* extra #else */
|
327
|
386 return 0;
|
|
387 }
|
361
|
388 if (macro_if_current == macro_if_depth) {
|
|
389 if (macro_if_skip==2) ;
|
|
390 else if (macro_if_skip) macro_if_skip=0;
|
|
391 else macro_if_skip=1;
|
|
392 }
|
|
393 return skip_rest_of_line();
|
327
|
394 }
|
361
|
395 break;
|
|
396 case 'e'*'n':
|
|
397 if (macroeq("endif")) {
|
|
398 if (macro_if_current == macro_if_depth) {
|
|
399 macro_if_skip = 0;
|
|
400 macro_if_depth = --macro_if_current;
|
|
401 } else {
|
|
402 if (macro_if_current<=0) {
|
|
403 error(MCERR); /* extra #if */
|
|
404 return 0;
|
|
405 }
|
|
406 macro_if_current--;
|
|
407 }
|
|
408 return skip_rest_of_line();
|
|
409 }
|
327
|
410 }
|
|
411 if (macro_if_skip) return 0;
|
361
|
412 switch(chptr[0]) {
|
|
413 case 'd':
|
|
414 if (macroeq("define")) {
|
|
415 ch= *chptr;
|
|
416 macro_define0();
|
|
417 *(chptr = linebuf) = '\0';
|
|
418 return 0;
|
|
419 }
|
|
420 break;
|
|
421 case 'u':
|
|
422 if (macroeq("undef")) {
|
|
423 i=mode;
|
|
424 mode=IFDEF;
|
|
425 ch= *chptr;
|
|
426 if (getsym(0)) {
|
|
427 if (nptr->sc == MACRO) {
|
|
428 nptr->sc = EMPTY;
|
|
429 } else if (nptr->sc == FMACRO) {
|
|
430 nptr->sc = EMPTY;
|
|
431 /* we cannot reclaim it's arg */
|
|
432 } else error(MCERR);
|
|
433 }
|
|
434 mode=i;
|
|
435 return 0;
|
327
|
436 }
|
361
|
437 break;
|
|
438 case 'i':
|
469
|
439 next = 1;
|
|
440 if (macroeq("include_next")|| (next=0, macroeq("include"))) {
|
361
|
441 if(filep+1 >= filestack + FILES) error(FILERR);
|
469
|
442 if ( ((filep+1)->fcb=getfname(next)) == NULL) error(FILERR);
|
361
|
443 (filep+1)->ln=lineno;
|
|
444 lineno=0;
|
|
445 ++filep;
|
|
446 *(chptr = linebuf) = '\0';
|
|
447 return 0;
|
|
448 }
|
|
449 break;
|
327
|
450 #if ASM_CODE
|
361
|
451 case 'a':
|
|
452 if (c=='a'&¯oeq("asm")) {
|
|
453 if (asmf) error(MCERR);
|
|
454 asmf = 1;
|
327
|
455 getline();
|
361
|
456 while (asmf) {
|
|
457 gen_source(linebuf);
|
|
458 getline();
|
|
459 }
|
|
460 return 0;
|
327
|
461 }
|
361
|
462 break;
|
|
463 case 'e':
|
|
464 if (macroeq("endasm")) {
|
|
465 if (!asmf) error(MCERR);
|
|
466 asmf = 0;
|
|
467 return 0;
|
|
468 }
|
|
469 break;
|
327
|
470 #endif
|
361
|
471 case ' ': case '\t':
|
327
|
472 getline();
|
361
|
473 return 0;
|
|
474 }
|
|
475 error(MCERR);
|
327
|
476 return 0;
|
|
477 }
|
|
478
|
|
479 extern int
|
|
480 macroeq(char *s)
|
|
481 {
|
|
482 char *p;
|
|
483
|
|
484 for (p = chptr; *s;) if (*s++ != *p++) return 0;
|
|
485 chptr = p;
|
|
486 return 1;
|
|
487 }
|
|
488
|
|
489 /* macro interpreter */
|
|
490
|
|
491 extern void
|
|
492 macro_define(char *macro)
|
|
493 {
|
|
494 char *chptr_save;
|
|
495 int chsave;
|
|
496
|
|
497 chptr_save = chptr;
|
|
498 chsave = ch;
|
|
499 chptr = macro;
|
|
500 ch= *chptr++;
|
|
501 macro_define0();
|
|
502 chptr = chptr_save;
|
|
503 ch = chsave;
|
|
504 }
|
|
505
|
363
|
506 /* macro define from chptr */
|
|
507
|
327
|
508 static void
|
|
509 macro_define0()
|
|
510 {
|
|
511 int i,args,c;
|
346
|
512 char **body;
|
327
|
513
|
|
514 i=mode;
|
|
515 mode=MDECL;
|
|
516 // ch= *chptr; ??
|
|
517 // fprintf(stderr,"macro def: ch %c *chptr %c\n",ch,*chptr);
|
|
518 getsym(0);
|
|
519 // fprintf(stderr,"macro def: %s =>",name);
|
|
520 if (nptr->sc != EMPTY) { /* override existing macro */
|
|
521 }
|
|
522 args = 0;
|
|
523 if (ch=='(') {
|
|
524 nptr->sc = FMACRO;
|
346
|
525 args = macro_args(&chptr);
|
327
|
526 } else {
|
|
527 nptr->sc = MACRO;
|
|
528 nptr->ty = -1;
|
|
529 }
|
|
530 // equal is allowed for -Dhoge=aho option
|
534
|
531 // if (ch=='=') chptr++;
|
327
|
532 while((c=*chptr)==' '||c=='\t') chptr++;
|
346
|
533 nptr->dsp = list2((int)cheap->ptr,args); /* macro body */
|
|
534 body = (char **)&car(nptr->dsp);
|
|
535 while ((*cheap->ptr = c = *chptr++)
|
327
|
536 && c != '\n') {
|
346
|
537 cheap = increment_cheap(cheap,body);
|
327
|
538 if (c=='/'&&chptr[0]=='/') {
|
351
|
539 cheap->ptr--;
|
|
540 *cheap->ptr = '\0';
|
|
541 while(*chptr++); break;
|
327
|
542 } else if (c=='/'&&chptr[0]=='*') {
|
346
|
543 cheap->ptr--; chptr++;
|
327
|
544 while((c = *chptr++)) {
|
|
545 if (c=='*'&&chptr[0]=='/') {
|
|
546 c = *chptr++; break;
|
|
547 }
|
|
548 }
|
|
549 if (!c) break;
|
|
550 } else if (c=='\\' && (*chptr=='\n'||*chptr==0)) {
|
|
551 chptr++;
|
346
|
552 cheap->ptr--;
|
327
|
553 getline();
|
|
554 }
|
|
555 }
|
346
|
556 if (c=='\n') {
|
|
557 *cheap->ptr = '\0';
|
|
558 }
|
|
559 cheap = increment_cheap(cheap,body);
|
327
|
560 // fprintf(stderr,"%s\n",(char *)car(nptr->dsp));
|
|
561 mode=i;
|
|
562 }
|
|
563
|
|
564 // create function macro argument list
|
|
565 // return list2((char*)arg,next)
|
|
566
|
|
567 static int
|
346
|
568 macro_args(char **pchptr)
|
327
|
569 {
|
|
570 int c;
|
|
571 int in_quote = 0;
|
|
572 int in_wquote = 0;
|
|
573 int plevel = 0;
|
346
|
574 char **body;
|
327
|
575 char *chptr = *pchptr;
|
346
|
576 int args = list2((int)cheap->ptr,0);
|
|
577 body = (char **)&car(args);
|
327
|
578 for(;;) {
|
346
|
579 *cheap->ptr = c = *chptr++;
|
|
580 cheap = increment_cheap(cheap,body);
|
327
|
581 if (!c) {
|
|
582 chptr--;
|
|
583 error(MCERR);
|
|
584 *pchptr = chptr;
|
|
585 return reverse0(args);
|
|
586 }
|
|
587 if (in_quote) {
|
|
588 if (c=='\\') {
|
|
589 if (*chptr != '\n') {
|
346
|
590 *cheap->ptr = *chptr++;
|
|
591 cheap = increment_cheap(cheap,body);
|
327
|
592 } else {
|
|
593 getline();
|
|
594 }
|
|
595 } else if (c=='\'') {
|
|
596 in_quote = 0;
|
|
597 }
|
|
598 } else if (in_wquote) {
|
|
599 if (c=='\\') {
|
|
600 if (*chptr !='\n') {
|
346
|
601 *cheap->ptr = *chptr++;
|
|
602 cheap = increment_cheap(cheap,body);
|
327
|
603 } else {
|
346
|
604 *cheap->ptr = '\n';
|
327
|
605 getline();
|
|
606 }
|
|
607 } else if (c=='"') {
|
|
608 in_wquote = 0;
|
|
609 }
|
|
610 } else if (c=='"') {
|
|
611 in_wquote = 1;
|
|
612 } else if (c=='\'') {
|
|
613 in_quote = 1;
|
|
614 } if (plevel==0) {
|
|
615 if (c==',') {
|
346
|
616 cheap->ptr[-1] = 0;
|
|
617 args = list2((int)cheap->ptr,args);
|
|
618 body = (char **)&car(args);
|
327
|
619 } else if (c==')') {
|
346
|
620 cheap->ptr[-1] = 0;
|
327
|
621 break;
|
|
622 } else if (c=='(') {
|
|
623 plevel++;
|
|
624 } else if (c=='\\') {
|
|
625 if (*chptr=='\n') {
|
346
|
626 cheap->ptr--;
|
327
|
627 getline();
|
|
628 }
|
|
629 // } else if (c==' '||c=='\t') {
|
346
|
630 // cheap->ptr--;
|
327
|
631 } else if (c=='\n') {
|
346
|
632 cheap->ptr--;
|
327
|
633 getline();
|
|
634 chptr = *pchptr;
|
|
635 }
|
|
636 } else if (c==')') {
|
|
637 plevel--;
|
|
638 } else if (c=='(') {
|
|
639 plevel++;
|
|
640 } else if (c=='\n') {
|
346
|
641 cheap->ptr--;
|
327
|
642 getline();
|
|
643 chptr = *pchptr;
|
|
644 }
|
|
645 }
|
|
646 *pchptr = chptr;
|
|
647 return reverse0(args);
|
|
648 }
|
|
649
|
|
650 /* output macro expansion result into macrobuf (macropp) */
|
|
651
|
|
652 static int
|
|
653 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
|
|
654 {
|
|
655 int args,sargs,values,evalues;
|
|
656 char *macro;
|
|
657
|
|
658 sargs = args = cadr(nptr->dsp);
|
346
|
659 values = macro_args(pchptr);
|
327
|
660 if (pchptr==&chptr) {
|
|
661 ch = *chptr++;
|
|
662 }
|
|
663 evalues = 0;
|
|
664 while(values) {
|
|
665 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
|
|
666 values = cadr(values);
|
|
667 }
|
|
668 evalues = reverse0(evalues);
|
359
|
669 enter_scope();
|
327
|
670 while(args) {
|
346
|
671 mappend(reverse0(car(evalues)),¯o);
|
|
672 local_define((char *)car(args),macro);
|
327
|
673 args = cadr(args);
|
|
674 evalues = cadr(evalues);
|
|
675 }
|
|
676 macro = (char *)car(nptr->dsp);
|
|
677 macrop = macro_eval(macrop,macro,list2((int)macro,history));
|
|
678 args = sargs;
|
359
|
679 leave_scope();
|
327
|
680 return macrop;
|
|
681 }
|
|
682
|
|
683 static void
|
|
684 local_define(char *macro,char *value)
|
|
685 {
|
359
|
686 NMTBL *nptr0,*nlist;
|
327
|
687 while(*macro==' '||*macro=='\t') macro++;
|
359
|
688 nptr0 = name_space_search(nlist=get_name(macro,0,DEF),MACRO);
|
|
689 nptr0 = make_local_scope(nlist,nptr0,MACRO);
|
|
690 nptr0->nm = value;
|
327
|
691 }
|
|
692
|
363
|
693 /*
|
|
694 Evaluate macro string.
|
|
695 reuslt: list2("replaced string",next)
|
|
696 */
|
|
697
|
327
|
698 static int
|
|
699 macro_eval(int macrop,char *body0,int history)
|
|
700 {
|
350
|
701 int c,len;
|
327
|
702 int in_quote = 0;
|
|
703 int in_wquote = 0;
|
|
704 char *macro;
|
|
705 char *body = body0;
|
346
|
706 char **expand;
|
327
|
707 NMTBL *nptrm;
|
346
|
708 macrop = list2((int)cheap->ptr,macrop);
|
|
709 expand = (char **)&car(macrop);
|
327
|
710 for(; (c = *body++) ;) {
|
|
711 if (in_quote) {
|
|
712 if (c=='\\') {
|
346
|
713 *cheap->ptr = c; c = *body++;
|
|
714 cheap = increment_cheap(cheap,expand);
|
327
|
715 } else if (c=='\'') {
|
|
716 in_quote = 0;
|
|
717 }
|
|
718 } else if (in_wquote) {
|
|
719 if (c=='\\') {
|
346
|
720 *cheap->ptr = c; c = *body++;
|
|
721 cheap = increment_cheap(cheap,expand);
|
327
|
722 } else if (c=='"') {
|
|
723 in_wquote = 0;
|
|
724 }
|
|
725 } else if (c=='"') {
|
|
726 in_wquote = 1;
|
|
727 } else if (c=='\'') {
|
|
728 in_quote = 1;
|
|
729 } else if (c=='#' && *body=='#') {
|
|
730 // name concatenation. skip ## and re-eval macro line.
|
|
731 mconcat = 1; body++; continue;
|
|
732 } else if (alpha(c)) {
|
349
|
733 body--; // ungetc
|
350
|
734 nptrm = get_name(body,&len,NONDEF);
|
|
735 if (!nptrm) {
|
|
736 while((*cheap->ptr = *body++) && len--)
|
|
737 cheap = increment_cheap(cheap,expand);
|
|
738 body--;
|
|
739 continue;
|
|
740 }
|
|
741 body += len;
|
349
|
742 c = *body;
|
347
|
743 nptrm = name_space_search(nptrm,MACRO);
|
327
|
744 macro = (char *)car(nptrm->dsp);
|
347
|
745 switch(nptrm->sc) {
|
|
746 case FMACRO:
|
327
|
747 if (c==' '||c=='\t') {
|
|
748 while (c==' '||c=='\t') c=*body++;
|
|
749 body--;
|
|
750 }
|
|
751 if(c!='(') error(MCERR);
|
349
|
752 *cheap->ptr = 0;
|
346
|
753 cheap = increment_cheap(cheap,expand);
|
|
754 body++;
|
327
|
755 macrop = macro_function(macrop,&body,nptrm,
|
|
756 list2((int)macro,history));
|
346
|
757 macrop = list2((int)cheap->ptr,macrop);
|
|
758 expand = (char **)&(car(macrop));
|
347
|
759 break;
|
|
760 case MACRO:
|
|
761 if (neqname(nptrm->nm,macro)) {
|
|
762 if (macro[0]==0) continue;
|
|
763 *cheap->ptr = 0;
|
|
764 cheap = increment_cheap(cheap,expand);
|
|
765 macrop=macro_eval(macrop,macro,list2((int)macro,history));
|
|
766 macrop = list2((int)cheap->ptr,macrop);
|
|
767 expand = (char **)&(car(macrop));
|
|
768 break;
|
|
769 }
|
|
770 default:
|
346
|
771 macro = nptrm->nm;
|
347
|
772 case LMACRO:
|
349
|
773 while((*cheap->ptr = *macro++)/* && len-- */)
|
346
|
774 cheap = increment_cheap(cheap,expand);
|
327
|
775 }
|
|
776 continue;
|
|
777 }
|
346
|
778 *cheap->ptr = c;
|
|
779 cheap = increment_cheap(cheap,expand);
|
327
|
780 }
|
346
|
781 *cheap->ptr = 0;
|
|
782 cheap = increment_cheap(cheap,expand);
|
327
|
783 return macrop;
|
|
784 }
|
|
785
|
363
|
786 /*
|
|
787 cancat list2("string",next) into cheap.
|
|
788 result overwrited by next cheap allocation
|
|
789 */
|
327
|
790
|
527
|
791 extern char *
|
346
|
792 mappend(int lists,char **result)
|
327
|
793 {
|
|
794 char *p;
|
346
|
795 *result = cheap->ptr;
|
348
|
796 for(;lists;lists = cadr(lists)) {
|
327
|
797 p = (char *)car(lists);
|
347
|
798 for(;(*cheap->ptr=*p++);cheap = increment_cheap(cheap,0)) {
|
346
|
799 // in_quote + \n case ? should be \n.
|
350
|
800 if (p[-1]=='\n') cheap->ptr[0]=' ';
|
346
|
801 }
|
327
|
802 }
|
347
|
803 cheap = increment_cheap(cheap,0);
|
346
|
804 return *result;
|
327
|
805 }
|
|
806
|
|
807 /* end */
|