Mercurial > hg > CbC > old > device
comparison mc-tree.c @ 67:254a0c576114
argument type list
author | kono |
---|---|
date | Mon, 24 Feb 2003 00:59:30 +0900 |
parents | 1ec39b34ed98 |
children | 0266905063b5 |
comparison
equal
deleted
inserted
replaced
66:0b068058dd67 | 67:254a0c576114 |
---|---|
1 /* Micro-C tree print routine */ | 1 /* Micro-C tree print routine */ |
2 /* $Id$ */ | 2 /* $Id$ */ |
3 | 3 |
4 #define EXTERN /**/ | 4 #define EXTERN /**/ |
5 #include "mc.h" | 5 #include "mc.h" |
6 extern void tree_print(int e); | |
7 extern void tree_parse(int e); | |
8 extern void tree_print_t(int e,int t); | |
9 | 6 |
10 typedef | 7 typedef |
11 struct tree_node { | 8 struct tree_node { |
12 int tree_type; | 9 int tree_type; |
13 char *tree_name; | 10 char *tree_name; |
14 char *tree_args; | 11 char *tree_args; |
15 } tree_node_type; | 12 } tree_node_type; |
16 | 13 |
14 extern void tree_print(int e); | |
15 extern void tree_parse(int e); | |
16 extern void tree_print_t(int e,int t); | |
17 static tree_node_type * find_node(int e); | |
18 extern void type_print(int type,NMTBL *n,FILE *out); | |
19 | |
20 /* ascendant order for binary search */ | |
21 | |
17 static | 22 static |
18 tree_node_type tree_nodes[] = { | 23 tree_node_type tree_nodes[] = { |
19 {(-1),"INT",""}, | 24 {(-45),"...",""}, |
20 {(-2),"CHAR",""}, | 25 {(-44),"lmacro",""}, |
21 {(-3),"UNSIGNED",""}, | 26 {(-43),"fmacro",""}, |
22 {(-4),"POINTER","t"}, | 27 {(-42),"konst",""}, |
23 {(-5),"ARRAY","tv"}, | 28 {(-41),"defined",""}, |
24 {(-6),"STRUCT","vt"}, | 29 {(-40),"environment",""}, |
25 {(-7),"UNION",""}, | 30 {(-39),"code",""}, |
26 {(-8),"FUNCTION","t"}, | 31 {(-38),"register",""}, |
27 {(-9),"EMPTY",""}, | 32 {(-37),"void",""}, |
28 {(-10),"STATIC",""}, | 33 {(-36),"extern",""}, |
29 {(-11),"GOTO",""}, | 34 {(-35),"short",""}, |
30 {(-12),"RETURN",""}, | 35 {(-34),"long",""}, |
31 {(-13),"BREAK",""}, | 36 {(-33),"type",""}, |
32 {(-14),"CONTINUE",""}, | 37 {(-32),"sizeof",""}, |
33 {(-15),"IF",""}, | 38 {(-31),"typedef",""}, |
34 {(-16),"ELSE",""}, | 39 {(-30),"flabel",""}, |
35 {(-17),"FOR",""}, | 40 {(-29),"blabel",""}, |
36 {(-18),"DO",""}, | 41 {(-28),"macro",""}, |
37 {(-19),"WHILE",""}, | 42 {(-27),"string",""}, |
38 {(-20),"SWITCH",""}, | 43 {(-26),"ident",""}, |
39 {(-21),"CASE",""}, | 44 {(-25),"field",""}, |
40 {(-22),"DEFAULT",""}, | 45 {(-24),"tag",""}, |
41 {(-23),"RESERVE",""}, | 46 {(-23),"reserve",""}, |
42 {(-24),"TAG",""}, | 47 {(-22),"default",""}, |
43 {(-25),"FIELD",""}, | 48 {(-21),"case",""}, |
44 {(-26),"IDENT",""}, | 49 {(-20),"switch",""}, |
45 {(-27),"STRING",""}, | 50 {(-19),"while",""}, |
46 {(-28),"MACRO",""}, | 51 {(-18),"do",""}, |
47 {(-29),"BLABEL",""}, | 52 {(-17),"for",""}, |
48 {(-30),"FLABEL",""}, | 53 {(-16),"else",""}, |
49 {(-31),"TYPEDEF",""}, | 54 {(-15),"if",""}, |
50 {(-32),"SIZEOF",""}, | 55 {(-14),"continue",""}, |
51 {(-33),"TYPE",""}, | 56 {(-13),"break",""}, |
52 {(-34),"LONG",""}, | 57 {(-12),"return",""}, |
53 {(-35),"SHORT",""}, | 58 {(-11),"goto",""}, |
54 {(-36),"EXTERN",""}, | 59 {(-10),"static",""}, |
55 {(-37),"VOID",""}, | 60 {(-9),"empty",""}, |
56 {(-38),"REGISTER",""}, | 61 {(-8),"function","t"}, |
57 {(-39),"CODE",""}, | 62 {(-7),"union",""}, |
58 {(-40),"ENVIRONMENT",""}, | 63 {(-6),"struct","vt"}, |
59 {(-41),"DEFINED",""}, | 64 {(-5),"array","tv"}, |
60 {(-42),"KONST",""}, | 65 {(-4),"pointer","t"}, |
61 {(-43),"FMACRO",""}, | 66 {(-3),"unsigned",""}, |
62 {(-44),"LMACRO",""}, | 67 {(-2),"char",""}, |
63 | 68 {(-1),"int",""}, |
64 {1,"GVAR","vs"}, | 69 |
65 {2,"RGVAR","vs"}, | 70 {1,"gvar","vs"}, |
66 {3,"CRGVAR","vs"}, | 71 {2,"rgvar","vs"}, |
67 {4,"LVAR","v"}, | 72 {3,"crgvar","vs"}, |
68 {5,"RLVAR","v"}, | 73 {4,"lvar","v"}, |
69 {6,"CRLVAR","v"}, | 74 {5,"rlvar","v"}, |
70 {7,"CONST","v"}, | 75 {6,"crlvar","v"}, |
71 {8,"FNAME","n"}, | 76 {7,"const","v"}, |
72 {9,"INDIRECT","e"}, | 77 {8,"fname","n"}, |
73 {10,"RINDIRECT","e"}, | 78 {9,"*","e"}, |
74 {11,"CRINDIRECT","e"}, | 79 {10,"rindirect","e"}, |
75 {12,"ADDRESS","e"}, | 80 {11,"crindirect","e"}, |
76 {13,"MINUS","e"}, | 81 {12,"&","e"}, |
77 {14,"LNOT","e"}, | 82 {13,"-","e"}, |
78 {15,"BNOT","e"}, | 83 {14,"!","e"}, |
79 {16,"INC",""}, | 84 {15,"~","e"}, |
80 {17,"POSTINC","e"}, | 85 {16,"++",""}, |
81 {18,"PREINC","e"}, | 86 {17,"postinc","e"}, |
82 {19,"CPOSTINC","e"}, | 87 {18,"preinc","e"}, |
83 {20,"CPREINC","e"}, | 88 {19,"cpostinc","e"}, |
84 {21,"DEC",""}, | 89 {20,"cpreinc","e"}, |
85 {22,"CPOSTDEC","e"}, | 90 {21,"--",""}, |
86 {23,"CPREDEC","e"}, | 91 {22,"cpostdec","e"}, |
87 {24,"MUL","ee"}, | 92 {23,"cpredec","e"}, |
88 {25,"UMUL","ee"}, | 93 {24,"*","ee"}, |
89 {26,"DIV","ee"}, | 94 {25,"*","ee"}, |
90 {27,"UDIV","ee"}, | 95 {26,"/","ee"}, |
91 {28,"MOD","ee"}, | 96 {27,"/","ee"}, |
92 {29,"UMOD","ee"}, | 97 {28,"%","ee"}, |
93 {30,"ADD","ee"}, | 98 {29,"%","ee"}, |
94 {31,"SUB","ee"}, | 99 {30,"+","ee"}, |
95 {32,"RSHIFT","ee"}, | 100 {31,"-","ee"}, |
96 {33,"URSHIFT","ee"}, | 101 {32,">>","ee"}, |
97 {34,"LSHIFT","ee"}, | 102 {33,">>","ee"}, |
98 {35,"ULSHIFT","ee"}, | 103 {34,"<<","ee"}, |
99 {36,"GT","ee"}, | 104 {35,"<<","ee"}, |
100 {37,"UGT","ee"}, | 105 {36,"gt","ee"}, |
101 {38,"GE","ee"}, | 106 {37,"ugt","ee"}, |
102 {39,"UGE","ee"}, | 107 {38,"ge","ee"}, |
103 {40,"LT","ee"}, | 108 {39,"uge","ee"}, |
104 {41,"ULT","ee"}, | 109 {40,"lt","ee"}, |
105 {42,"LE","ee"}, | 110 {41,"ult","ee"}, |
106 {43,"ULE","ee"}, | 111 {42,"le","ee"}, |
107 {44,"EQ","ee"}, | 112 {43,"ule","ee"}, |
108 {45,"NEQ","ee"}, | 113 {44,"==","ee"}, |
109 {46,"BAND","ee"}, | 114 {45,"!=","ee"}, |
110 {47,"EOR","ee"}, | 115 {46,"&","ee"}, |
111 {48,"BOR","ee"}, | 116 {47,"^","ee"}, |
112 {49,"LAND","ee"}, | 117 {48,"|","ee"}, |
113 {50,"LOR","ee"}, | 118 {49,"&&","ee"}, |
114 {51,"COND","eee"}, | 119 {50,"||","ee"}, |
115 {52,"ASS","ee"}, | 120 {51,"cond","eee"}, |
116 {53,"CASS","ee"}, | 121 {52,"ass","ee"}, |
117 {54,"ASSOP","eev"}, | 122 {53,"cass","ee"}, |
118 {55,"CASSOP","eev"}, | 123 {54,"assop","eev"}, |
119 {56,"COMMA","ee"}, | 124 {55,"cassop","eev"}, |
120 {57,"LPAR",""}, | 125 {56,",","ee"}, |
121 {58,"RPAR",""}, | 126 {57,"(",""}, |
122 {59,"LBRA",""}, | 127 {58,")",""}, |
123 {60,"RBRA",""}, | 128 {59,"[",""}, |
124 {61,"LC",""}, | 129 {60,"]",""}, |
125 {62,"RC",""}, | 130 {61,"{",""}, |
126 {63,"COLON","ee"}, | 131 {62,"}",""}, |
127 {64,"SM",""}, | 132 {63,":","ee"}, |
128 {65,"PERIOD",""}, | 133 {64,";",""}, |
129 {66,"ARROW",""}, | 134 {65,".",""}, |
130 {67,"CNAME",""}, | 135 {66,"->",""}, |
131 {68,"SASS",""}, | 136 {67,"cname",""}, |
132 {69,"DOTS",""}, | 137 {68,"sass",""}, |
133 {100,"AS",""} | 138 {69,"rstruct",""}, |
139 {100,"=",""}, | |
140 {124,"*=","ee"}, | |
141 {125,"*=","ee"}, | |
142 {126,"/=","ee"}, | |
143 {127,"/=","ee"}, | |
144 {128,"%=","ee"}, | |
145 {129,"%=","ee"}, | |
146 {130,"+=","ee"}, | |
147 {131,"-=","ee"}, | |
148 {132,">>=","ee"}, | |
149 {133,">>=","ee"}, | |
150 {134,"<<=","ee"}, | |
151 {135,"<<=","ee"}, | |
152 {146,"&=","ee"}, | |
153 {147,"^=","ee"}, | |
154 {148,"|=","ee"}, | |
134 }; | 155 }; |
135 | 156 |
136 void | 157 void |
137 tree_print_t(int e,int t) | 158 tree_print_t(int e,int t) |
138 { | 159 { |
162 tree_node_type *t; | 183 tree_node_type *t; |
163 int i,j; | 184 int i,j; |
164 char *s; | 185 char *s; |
165 | 186 |
166 if(e<0) { | 187 if(e<0) { |
167 for(t=tree_nodes;t->tree_type!=100;t++) { | 188 t=find_node(e); |
168 if(t->tree_type==e) { | 189 if(t->tree_type==e) { |
169 for(j=0;j<tree_level;j++) putchar(' '); | 190 for(j=0;j<tree_level;j++) putchar(' '); |
170 printf("list(%s)",t->tree_name); | 191 printf("list(%s)",t->tree_name); |
171 break; | |
172 } | |
173 } | 192 } |
174 } else { | 193 } else { |
175 i = car(e); | 194 i = car(e); |
176 for(t=tree_nodes;t->tree_type!=100;t++) { | 195 t=find_node(e); |
177 if(t->tree_type==i) { | 196 if(t->tree_type==i) { |
178 tree_level++; | 197 tree_level++; |
179 for(j=0;j<tree_level;j++) putchar(' '); | 198 for(j=0;j<tree_level;j++) putchar(' '); |
180 printf("list(%s",t->tree_name); | 199 printf("list(%s",t->tree_name); |
181 for(i=1,s=t->tree_args;*s;s++,i++) { | 200 for(i=1,s=t->tree_args;*s;s++,i++) { |
182 switch(*s) { | 201 switch(*s) { |
183 case 'e': | 202 case 'e': |
184 case 't': | 203 case 't': |
185 printf(",\n*"); | 204 printf(",\n*"); |
186 tree_parse(heap[e+i]); break; | 205 tree_parse(heap[e+i]); break; |
187 case 'v': | 206 case 'v': |
188 printf(",%d",heap[e+i]); break; | 207 printf(",%d",heap[e+i]); break; |
189 case 'n': | 208 case 'n': |
190 printf(",%s",((NMTBL *)heap[e+i])->nm); break; | 209 printf(",%s",((NMTBL *)heap[e+i])->nm); break; |
191 case 's': | 210 case 's': |
192 printf(",%s",(char *)heap[e+i]); break; | 211 printf(",%s",(char *)heap[e+i]); break; |
193 case 'i': | 212 case 'i': |
194 printf(",%d",heap[e+i]); break; | 213 printf(",%d",heap[e+i]); break; |
195 } | |
196 } | 214 } |
197 tree_level--; | |
198 printf(")"); | |
199 break; | |
200 } | 215 } |
201 } | 216 tree_level--; |
202 } | 217 printf(")"); |
203 } | 218 } |
219 } | |
220 } | |
221 | |
222 tree_node_type * | |
223 find_node(int e) { | |
224 int e1,e2; | |
225 int first=0; | |
226 int last=sizeof(tree_nodes)/sizeof(tree_node_type); | |
227 e2=-1; | |
228 while (first!=last) { | |
229 e1 = (first+last)/2; | |
230 if(e2==e1) | |
231 return 0; | |
232 e2=e1; | |
233 if (tree_nodes[e1].tree_type>e) | |
234 last = e1; | |
235 else if (tree_nodes[e1].tree_type==e) | |
236 break; | |
237 else if (tree_nodes[e1].tree_type<e) | |
238 first = e1; | |
239 } | |
240 return &tree_nodes[e1]; | |
241 } | |
242 | |
243 void struct_type_print(int type,FILE *out) | |
244 { | |
245 NMTBL *n; | |
246 int tags; | |
247 if((n=(NMTBL*)cadddr(type))) { | |
248 fprintf(out,"%s ",n->nm); | |
249 return; | |
250 } | |
251 if((tags=caddr(type))) { | |
252 fprintf(out,"{"); | |
253 while(tags) { | |
254 n=(NMTBL*)caddr(tags); | |
255 type_print(car(tags),n,out); | |
256 fprintf(out,";"); | |
257 tags = cadr(tags); | |
258 } | |
259 fprintf(out,"}"); | |
260 } | |
261 } | |
262 | |
263 void function_type_print(int type,NMTBL *n,FILE *out) | |
264 { | |
265 int args; | |
266 type_print(cadr(type),0,out); | |
267 if(n) fprintf(out," %s",n->nm); | |
268 fprintf(out,"("); | |
269 if((args=caddr(type))) { | |
270 while (args) { | |
271 type_print(car(args),0,out); | |
272 args=cadr(args); | |
273 if (args) fprintf(out,","); | |
274 } | |
275 } | |
276 fprintf(out,")"); | |
277 } | |
278 | |
279 void type_print(int type,NMTBL *n,FILE *out) | |
280 { | |
281 int t; | |
282 tree_node_type *tn; | |
283 if (type<0) { | |
284 t=type; | |
285 if (!(tn=find_node(t))) { error(-1); return; } | |
286 fprintf(out,"%s ",tn->tree_name); | |
287 } else if ((t=car(type))) { | |
288 if (!(tn=find_node(t))) { error(-1); return; } | |
289 if(t==STRUCT||t==UNION) { | |
290 fprintf(out,"%s ",tn->tree_name); | |
291 struct_type_print(type,out); | |
292 } else if(t==CODE) { | |
293 fprintf(out,"%s ",tn->tree_name); | |
294 function_type_print(type,n,out); | |
295 return; | |
296 } else if(t==FUNCTION) { | |
297 function_type_print(type,n,out); | |
298 return; | |
299 } else if(t==ARRAY) { | |
300 type_print(cadr(type),n,out); | |
301 fprintf(out,"[%d]",caddr(type)); | |
302 return; | |
303 } else if(t==POINTER) { | |
304 t=cadr(type); | |
305 if(car(t)==FUNCTION) { | |
306 fprintf(out,"("); | |
307 type_print(t,0,out); | |
308 fprintf(out,")"); | |
309 } else | |
310 type_print(t,0,out); | |
311 fprintf(out,"*"); | |
312 } | |
313 } | |
314 if(n) fprintf(out,"%s",n->nm); | |
315 } | |
316 | |
317 /* end */ |