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