annotate mc-macro.c @ 501:a63eb2319d11

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