annotate mc-macro.c @ 327:da2e3f2d127d

macro/codegen reorganization
author kono
date Mon, 21 Jun 2004 23:50:34 +0900
parents
children 11f8f2e6bb72
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
327
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
1 /* Micro-C Preprocessor Part */ /* $Id$ */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
2
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
3 #include <stdio.h>
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
4 #include "mc.h"
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
5 #include "mc-parse.h"
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
6 #include "mc-macro.h"
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
7 #include "mc-code.h"
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
8
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
9 extern struct {int fd,ln;char *name0;int inc;FILE *fcb;} *filep,filestack[FILES];
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
10
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
11 int in_macro_if = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
12 char *chinput;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
13
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
14 static char *macropp,macro_buf[MACROSIZE];
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
15 static int mconcat=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
16
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
17 static void macro_define0();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
18 static int macro_args(char **pcheapp,char *maxcheap,char **pchptr);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
19 static int macro_function(int macrop,char **pchptr,NMTBL *nptr,int history);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
20 static void local_define(char *macro,char *value);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
21 static void local_undef(char *macro);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
22 static int macro_eval(int macrop,char *body0,int history);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
23 static char * mappend(int lists);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
24 static int macro_processing();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
25
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
26 static void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
27 gen_source(char *s)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
28 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
29 printf("%s",s);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
30 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
31
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
32 extern void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
33 macro_expansion(NMTBL *nptrm)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
34 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
35 int i = mode;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
36 int macrop = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
37 int slfree = lfree;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
38 mode = STAT;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
39
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
40 macropp = macro_buf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
41 if (nptrm->sc == FMACRO) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
42 macrop=macro_function(macrop,&chptr,nptrm,0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
43 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
44 macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
45 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
46 macropp = macro_buf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
47 mappend(reverse0(macrop));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
48 macropp[-1] ='\n';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
49 *macropp =0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
50 while (mconcat) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
51 // ## re-eval macro
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
52 printf("## %s",macro_buf);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
53 mconcat = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
54 macrop = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
55 macropp = macro_buf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
56 macrop=macro_eval(macrop,macro_buf,0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
57 macropp = macro_buf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
58 mappend(reverse0(macrop));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
59 macropp[-1] ='\n';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
60 *macropp =0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
61 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
62 mconcat = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
63 lfree = slfree;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
64 if (lsrc && !asmf && nptrm->sc==FMACRO) gen_comment(macro_buf);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
65 macropp[-1] =0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
66 if (macro_buf[0]==0) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
67 mode = i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
68 return;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
69 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
70 chptrsave = glist2((int)chptr,chptrsave);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
71 chsave = glist2(ch,chsave);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
72 chptr = macro_buf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
73 ch = *chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
74 mode = i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
75 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
76
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
77 /* file inclusion */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
78
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
79 static char *
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
80 expand_file_name(char *path,char *name,int pos,int lbufsize)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
81 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
82 char *p = name+pos;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
83 int i,j;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
84 j = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
85 for(i=0;path[i];i++,j++); for(i=0;name[i];i++,j++);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
86 if (pos+j+1>lbufsize) { error(FILERR); return ""; }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
87 while((name[pos++] = *path++));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
88 pos--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
89 if (name[pos]!='/') name[pos]='/';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
90 for(i = 0; ((name[pos++] = name[i++])););
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
91 return p;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
92 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
93
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
94 static FILE *
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
95 getfname(void)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
96 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
97 int i,end='"',err=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
98 char *s,*p,**pp,name[LBUFSIZE];
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
99 FILE *fp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
100
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
101 getch();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
102 if(skipspc()=='"') { end = '"';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
103 } else if (ch=='<') { end = '>';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
104 } else { error(INCERR); err=1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
105 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
106 for(i=0;(getch()!=end && ch!='\n');) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
107 if(i<LBUFSIZE-1) name[i++]=ch;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
108 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
109 if(ch=='\n') error(INCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
110 if (err) return filep->fcb;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
111 name[i]=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
112 fp = fopen(name,"r") ;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
113 if (fp) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
114 p = name;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
115 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
116 for(pp=(end=='>'||filep->inc=='>')
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
117 ?l_include_path:include_path;*pp;pp++) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
118 p = expand_file_name(*pp,name,i+1,LBUFSIZE);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
119 if ((fp = fopen(p,"r"))) break ;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
120 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
121 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
122 if(!fp) { error(FILERR); return filep->fcb; }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
123 copy_current_file_dir(s=p);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
124 (filep+1)->name0 = cheapp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
125 (filep+1)->inc = end;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
126 while((*cheapp++ = *s++));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
127 return ( (filep+1)->fcb = fp );
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
128 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
129
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
130 /* line input and conversion */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
131
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
132 static int macro_if_depth ;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
133 static int macro_if_current ;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
134 static int macro_if_skip ;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
135
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
136 static int
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
137 skip_rest_of_line()
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
138 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
139 getch();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
140 do {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
141 while(ch!='\n'&&ch!='\r') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
142 if (!in_comment) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
143 if (ch=='/') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
144 getch();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
145 if (ch=='/') in_comment=2;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
146 else if (ch=='*') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
147 in_comment=1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
148 } else continue;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
149 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
150 } else if (ch=='*') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
151 getch();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
152 if (ch=='/') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
153 in_comment=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
154 return macro_if_skip?0:1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
155 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
156 else continue;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
157 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
158 getch();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
159 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
160 if (in_comment==1) { getline(); getch(); }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
161 } while(in_comment==1);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
162 in_comment=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
163 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
164 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
165
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
166 extern void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
167 getline(void)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
168 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
169 int i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
170 int c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
171
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
172 do {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
173 if (chinput) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
174 if (! *chinput) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
175 chinput=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
176 continue;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
177 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
178 chptr=linebuf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
179 i=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
180 while((*chptr++=c=*chinput++)&&(c!='\n')) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
181 if (++i > LBUFSIZE-2) error(LNERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
182 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
183 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
184 lineno++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
185 glineno++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
186 chptr=linebuf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
187 i=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
188 while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
189 if (++i > LBUFSIZE-2) error(LNERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
190 if (c==EOF) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
191 error(EOFERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
192 --chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
193 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
194 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
195 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
196 *chptr = '\0';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
197 if (lsrc && !asmf && !macro_if_skip) gen_comment(linebuf);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
198 if (*(chptr = linebuf) == '#' && !in_comment && !in_quote) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
199 if (macro_processing()) return;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
200 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
201 } while(!in_quote && (macro_if_skip || linebuf[0] == '#'));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
202 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
203
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
204 /* preprocessor directive */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
205
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
206 /* line continuation \\ */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
207
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
208 extern void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
209 check_macro_eof()
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
210 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
211 int c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
212 for(c=0;c<LBUFSIZE-3&&chptr[c];c++);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
213 if (c>0&&chptr[c-1]=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
214 return;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
215 } else if (c>0&&chptr[c-1]=='\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
216 if (c>0&&chptr[c-2]=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
217 return;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
218 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
219 c--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
220 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
221 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
222 chptr[c] = ';';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
223 chptr[c+1] = '\n';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
224 chptr[c+2] = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
225 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
226
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
227 static void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
228 macro_if()
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
229 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
230 int i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
231 ch= *chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
232 in_macro_if = 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
233 check_macro_eof();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
234 getsym(0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
235 /* i=cexpr(expr(1)); #if allow undefined symbols.. */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
236 i=expr(1);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
237 in_macro_if = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
238 if (car(i)==CONST) i=cadr(i);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
239 else i=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
240 if (ch) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
241 if (chptr[-1]==ch) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
242 /* we are fall into getch(), which lost the last ch */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
243 /* chptr[-1]==ch check is fanatic, but ... */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
244 chptr--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
245 } else error(-1);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
246 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
247 macro_if_depth = macro_if_current;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
248 macro_if_skip = !i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
249 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
250
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
251 static int
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
252 macro_processing()
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
253 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
254 int i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
255 int c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
256 int mode_save;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
257
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
258 ++chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
259 while (*chptr==' '||*chptr=='\t') ++chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
260 if (macroeq("ifdef") || macroeq("ifndef")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
261 c = (chptr[-4]=='n');
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
262 macro_if_current++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
263 if (!macro_if_skip) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
264 mode_save = mode; mode = IFDEF;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
265 ch= *chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
266 i = getsym(0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
267 mode = mode_save;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
268 macro_if_depth = macro_if_current;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
269 macro_if_skip = (!i)^c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
270 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
271 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
272 } else if (macroeq("elif")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
273 if (macro_if_current==0) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
274 error(MCERR); /* extra #else */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
275 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
276 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
277 if (macro_if_current == macro_if_depth) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
278 if (!macro_if_skip || macro_if_skip==2) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
279 macro_if_skip=2;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
280 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
281 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
282 macro_if();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
283 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
284 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
285 } else if (macroeq("if")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
286 macro_if_current++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
287 if (!macro_if_skip) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
288 macro_if();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
289 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
290 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
291 } else if (macroeq("else")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
292 if (macro_if_current==0) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
293 error(MCERR); /* extra #else */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
294 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
295 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
296 if (macro_if_current == macro_if_depth) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
297 if (macro_if_skip==2) ;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
298 else if (macro_if_skip) macro_if_skip=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
299 else macro_if_skip=1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
300 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
301 return skip_rest_of_line();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
302 } else if (macroeq("endif")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
303 if (macro_if_current == macro_if_depth) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
304 macro_if_skip = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
305 macro_if_depth = --macro_if_current;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
306 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
307 if (macro_if_current<=0) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
308 error(MCERR); /* extra #if */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
309 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
310 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
311 macro_if_current--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
312 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
313 return skip_rest_of_line();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
314 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
315 if (macro_if_skip) return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
316 if (macroeq("define")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
317 ch= *chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
318 macro_define0();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
319 *(chptr = linebuf) = '\0';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
320 } else if (macroeq("undef")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
321 i=mode;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
322 mode=IFDEF;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
323 ch= *chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
324 if (getsym(0)) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
325 if (nptr->sc == MACRO) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
326 nptr->sc = EMPTY;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
327 } else if (nptr->sc == FMACRO) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
328 nptr->sc = EMPTY;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
329 /* we cannot reclaim it's arg */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
330 } else error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
331 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
332 mode=i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
333 } else if (macroeq("include")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
334 if(filep+1 >= filestack + FILES) error(FILERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
335 if ( ((filep+1)->fcb=getfname()) == NULL) error(FILERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
336 (filep+1)->ln=lineno;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
337 lineno=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
338 ++filep;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
339 *(chptr = linebuf) = '\0';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
340 #if ASM_CODE
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
341 } else if (macroeq("asm")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
342 if (asmf) error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
343 asmf = 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
344 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
345 while (asmf) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
346 gen_source(linebuf);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
347 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
348 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
349 } else if (macroeq("endasm")) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
350 if (!asmf) error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
351 asmf = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
352 #endif
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
353 } else if (macroeq(" "))
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
354 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
355 else error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
356 return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
357 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
358
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
359 extern int
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
360 macroeq(char *s)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
361 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
362 char *p;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
363
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
364 for (p = chptr; *s;) if (*s++ != *p++) return 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
365 chptr = p;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
366 return 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
367 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
368
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
369 /* macro interpreter */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
370
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
371 extern void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
372 macro_define(char *macro)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
373 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
374 char *chptr_save;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
375 int chsave;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
376
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
377 chptr_save = chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
378 chsave = ch;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
379 chptr = macro;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
380 ch= *chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
381 macro_define0();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
382 chptr = chptr_save;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
383 ch = chsave;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
384 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
385
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
386 static void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
387 macro_define0()
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
388 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
389 int i,args,c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
390 char *scheapp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
391
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
392 i=mode;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
393 mode=MDECL;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
394 // ch= *chptr; ??
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
395 // fprintf(stderr,"macro def: ch %c *chptr %c\n",ch,*chptr);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
396 getsym(0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
397 // fprintf(stderr,"macro def: %s =>",name);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
398 if (nptr->sc != EMPTY) { /* override existing macro */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
399 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
400 args = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
401 if (ch=='(') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
402 nptr->sc = FMACRO;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
403 args = macro_args(&cheapp,cheap+CHEAPSIZE,&chptr);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
404 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
405 nptr->sc = MACRO;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
406 nptr->ty = -1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
407 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
408 // equal is allowed for -Dhoge=aho option
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
409 if (ch=='=') chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
410 while((c=*chptr)==' '||c=='\t') chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
411 nptr->dsp = list2((int)cheapp,args); /* macro body */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
412 scheapp = cheapp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
413 while ((*cheapp++ = c = *chptr++)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
414 && c != '\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
415 if (c=='/'&&chptr[0]=='/') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
416 cheapp--; while(*chptr++); break;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
417 } else if (c=='/'&&chptr[0]=='*') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
418 cheapp--; chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
419 while((c = *chptr++)) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
420 if (c=='*'&&chptr[0]=='/') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
421 c = *chptr++; break;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
422 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
423 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
424 if (!c) break;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
425 } else if (c=='\\' && (*chptr=='\n'||*chptr==0)) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
426 chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
427 cheapp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
428 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
429 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
430 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
431 *cheapp++ = '\0';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
432 while(cheapp>scheapp&&(*cheapp=='\n'||*cheapp==0)) cheapp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
433 *++cheapp = '\0'; cheapp++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
434 if (cheapp >= cheap+CHEAPSIZE) /* too late? */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
435 error(STRERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
436 // fprintf(stderr,"%s\n",(char *)car(nptr->dsp));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
437 mode=i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
438 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
439
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
440 // create function macro argument list
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
441 // return list2((char*)arg,next)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
442
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
443 static int
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
444 macro_args(char **pcheapp,char *maxcheap,char **pchptr)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
445 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
446 int c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
447 int in_quote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
448 int in_wquote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
449 int plevel = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
450 char *cheapp = *pcheapp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
451 char *chptr = *pchptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
452 int args = list2((int)cheapp,0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
453 for(;;) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
454 *cheapp++ = c = *chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
455 if (cheapp >= maxcheap) error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
456 if (!c) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
457 chptr--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
458 error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
459 *pchptr = chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
460 *pcheapp = cheapp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
461 return reverse0(args);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
462 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
463 if (in_quote) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
464 if (c=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
465 if (*chptr != '\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
466 *cheapp++ = *chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
467 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
468 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
469 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
470 } else if (c=='\'') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
471 in_quote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
472 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
473 } else if (in_wquote) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
474 if (c=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
475 if (*chptr !='\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
476 *cheapp++ = *chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
477 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
478 *cheapp = '\n';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
479 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
480 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
481 } else if (c=='"') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
482 in_wquote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
483 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
484 } else if (c=='"') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
485 in_wquote = 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
486 } else if (c=='\'') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
487 in_quote = 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
488 } if (plevel==0) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
489 if (c==',') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
490 cheapp[-1] = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
491 args = list2((int)cheapp,args);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
492 } else if (c==')') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
493 cheapp[-1] = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
494 break;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
495 } else if (c=='(') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
496 plevel++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
497 } else if (c=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
498 if (*chptr=='\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
499 cheapp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
500 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
501 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
502 // } else if (c==' '||c=='\t') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
503 // cheapp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
504 } else if (c=='\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
505 cheapp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
506 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
507 chptr = *pchptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
508 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
509 } else if (c==')') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
510 plevel--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
511 } else if (c=='(') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
512 plevel++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
513 } else if (c=='\n') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
514 cheapp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
515 getline();
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
516 chptr = *pchptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
517 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
518 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
519 *pchptr = chptr;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
520 *pcheapp = cheapp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
521 return reverse0(args);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
522 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
523
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
524 /* output macro expansion result into macrobuf (macropp) */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
525
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
526 static int
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
527 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
528 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
529 int args,sargs,values,evalues;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
530 char *macro;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
531
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
532 sargs = args = cadr(nptr->dsp);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
533 values = macro_args(&macropp,macro_buf+MACROSIZE,pchptr);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
534 if (pchptr==&chptr) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
535 ch = *chptr++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
536 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
537 evalues = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
538 while(values) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
539 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
540 values = cadr(values);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
541 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
542 evalues = reverse0(evalues);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
543 while(args) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
544 local_define((char *)car(args),mappend(reverse0(car(evalues))));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
545 /* fprintf(stderr,"%s: %s => %s\n",nptr->nm,(char *)car(args),(char *)car(msearch0((char *)car(args))->dsp)); */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
546 args = cadr(args);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
547 evalues = cadr(evalues);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
548 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
549 macro = (char *)car(nptr->dsp);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
550 macrop = macro_eval(macrop,macro,list2((int)macro,history));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
551 /* fprintf(stderr,"%s: result %s => %s\n",nptr->nm,macro,(char *)car(macrop)); */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
552 args = sargs;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
553 while(args) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
554 local_undef((char *)car(args));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
555 args = cadr(args);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
556 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
557 return macrop;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
558 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
559
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
560 static void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
561 local_define(char *macro,char *value)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
562 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
563 NMTBL *nptr0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
564 while(*macro==' '||*macro=='\t') macro++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
565 nptr0 = msearch0(macro);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
566 /* save nptr previous contents in a list */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
567 nptr0->ty=list3(nptr0->sc,nptr0->ty,nptr0->dsp);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
568 /* set new value */
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
569 nptr0->sc=LMACRO;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
570 nptr0->dsp=list2((int)value,0);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
571 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
572
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
573 static void
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
574 local_undef(char *macro)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
575 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
576 NMTBL *nptr0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
577 int save;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
578 nptr0 = msearch0(macro);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
579 save = nptr0->ty;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
580 nptr0->sc=car(save);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
581 nptr0->dsp=caddr(save);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
582 nptr0->ty=cadr(save);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
583 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
584
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
585 static int
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
586 macro_eval(int macrop,char *body0,int history)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
587 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
588 int c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
589 int in_quote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
590 int in_wquote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
591 char *macro;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
592 char *body = body0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
593 int i;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
594 NMTBL *nptrm;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
595 macrop = list2((int)macropp,macrop);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
596 for(; (c = *body++) ;) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
597 if (macropp+1>macro_buf+MACROSIZE) error(STRERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
598 if (in_quote) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
599 if (c=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
600 *macropp++ = c; c = *body++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
601 } else if (c=='\'') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
602 in_quote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
603 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
604 } else if (in_wquote) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
605 if (c=='\\') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
606 *macropp++ = c; c = *body++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
607 } else if (c=='"') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
608 in_wquote = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
609 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
610 } else if (c=='"') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
611 in_wquote = 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
612 } else if (c=='\'') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
613 in_quote = 1;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
614 } else if (c=='#' && *body=='#') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
615 // name concatenation. skip ## and re-eval macro line.
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
616 mconcat = 1; body++; continue;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
617 } else if (alpha(c)) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
618 i = 0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
619 do { namebuf[i++] = c; c=*body++;} while (alpha(c)||digit(c));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
620 body--; // ungetc
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
621 namebuf[i]=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
622 nptrm = msearch0(namebuf);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
623 macro = (char *)car(nptrm->dsp);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
624 if (nptrm->sc==LMACRO) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
625 while((*macropp++ = *macro++));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
626 macropp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
627 } else if (nptrm->sc==MACRO && neqname(namebuf,macro)) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
628 if (macro[0]==0) continue;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
629 *macropp++=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
630 macrop=macro_eval(macrop,macro,list2((int)macro,history));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
631 macrop = list2((int)macropp,macrop);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
632 } else if (nptrm->sc==FMACRO) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
633 if (c==' '||c=='\t') {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
634 while (c==' '||c=='\t') c=*body++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
635 body--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
636 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
637 if(c!='(') error(MCERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
638 *macropp++=0; body++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
639 macrop = macro_function(macrop,&body,nptrm,
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
640 list2((int)macro,history));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
641 macrop = list2((int)macropp,macrop);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
642 } else {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
643 macro = namebuf;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
644 while((*macropp++ = *macro++));
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
645 macropp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
646 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
647 continue;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
648 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
649 *macropp++ = c;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
650 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
651 *macropp++=0;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
652 return macrop;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
653 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
654
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
655
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
656 static char *
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
657 mappend(int lists)
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
658 {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
659 char *p;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
660 char *result = macropp;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
661 while(lists) {
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
662 if (macropp>macro_buf+MACROSIZE) error(STRERR);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
663 p = (char *)car(lists);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
664 while((*macropp++=*p++)) if (p[-1]=='\n') macropp[-1]=' ';
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
665 macropp--;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
666 lists = cadr(lists);
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
667 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
668 macropp++;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
669 return result;
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
670 }
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
671
da2e3f2d127d macro/codegen reorganization
kono
parents:
diff changeset
672 /* end */