60
|
1 #define EXTERN /**/
|
|
2
|
|
3 #include "mc.h"
|
|
4
|
65
|
5 static void open(char *);
|
60
|
6 static void print(char *);
|
65
|
7 static void close();
|
66
|
8 static void comment(char *s);
|
60
|
9
|
|
10 Converter c2cbc_converter = {
|
65
|
11 &open,
|
|
12 &print,
|
|
13 &close,
|
66
|
14 &comment,
|
60
|
15 };
|
|
16
|
66
|
17 static FILE *vout;
|
|
18
|
|
19 static void
|
|
20 comment(char *s)
|
|
21 {
|
|
22 }
|
|
23
|
65
|
24 static void cbcstruct_init(void);
|
|
25 static void init(void);
|
|
26 static void unlist(int e1);
|
|
27 static int unsym(int e1);
|
|
28
|
|
29 static FILE *vout;
|
|
30 static FILE *cbc_fp1;
|
|
31
|
|
32 static int while_count=0;
|
|
33 static int for_count=0;
|
|
34 static int while_sign=0;
|
|
35 static int for_sign=0;
|
|
36
|
|
37 #define SPSTACK_MAX 300
|
|
38 int cbc_spstack[SPSTACK_MAX];
|
|
39 static int sp_free=0;
|
|
40
|
|
41 #define FUNBUF_MAX 1000
|
|
42 char *cbc_funstack[FUNBUF_MAX];
|
|
43 static int fun_free=0;
|
|
44
|
|
45 static void
|
|
46 open(char *s)
|
|
47 {
|
|
48 char *p=cheapp;
|
|
49 while((*cheapp++ = *s++)) {
|
|
50 if (*s=='.') {
|
|
51 *cheapp++=*s++; *cheapp++='c';
|
|
52 *cheapp++='b'; *cheapp++='c';
|
|
53 *cheapp++=0;
|
|
54 break;
|
|
55 }
|
|
56 }
|
|
57 vout = fopen(p,"w");
|
|
58 if(!vout) error(-1);
|
|
59
|
|
60 cheapp=p;
|
|
61 while((*cheapp++ = *s++)) {
|
|
62 if (*s=='.') {
|
|
63 *cheapp++='-'; *cheapp++='s';
|
|
64 *cheapp++=*s++; *cheapp++='h';
|
|
65 *cheapp++=0;
|
|
66 break;
|
|
67 }
|
|
68 }
|
|
69 cbc_fp1 = fopen(p,"w");
|
|
70 if(!cbc_fp1) error(-1);
|
|
71
|
|
72 cbcstruct_init();
|
|
73 init();
|
|
74 fprintf(vout,"#include \"%s\"\n",p);
|
|
75 cheapp=p;
|
|
76 }
|
60
|
77
|
|
78 static void
|
|
79 print(char *s)
|
|
80 {
|
|
81 fprintf(vout,"c: %s\n",s);
|
|
82 }
|
65
|
83
|
|
84 static void
|
|
85 close()
|
|
86 {
|
|
87 fclose(vout);
|
|
88 }
|
|
89
|
|
90 static void
|
|
91 cbcstruct_init(void)
|
|
92 {
|
|
93 fprintf(cbc_fp1,"typedef char *stack;\n");
|
|
94 fprintf(cbc_fp1,"EXTERN stack sp;\n");
|
|
95 fprintf(cbc_fp1,"struct cont_save {\n");
|
|
96 fprintf(cbc_fp1," code (*ret)();\n");
|
|
97 fprintf(cbc_fp1,"};\n");
|
|
98 }
|
|
99
|
|
100 static void
|
|
101 init(void)
|
|
102 {
|
|
103 while_count=0;
|
|
104 for_count=0;
|
|
105 while_sign=0;
|
|
106 for_sign=0;
|
|
107 sp_free=0;
|
|
108 fun_free=0;
|
|
109 cbc_funstack[fun_free++]="main";
|
|
110 }
|
|
111
|
|
112 static void
|
|
113 unlist(int e1)
|
|
114 {
|
|
115 int e2,e3;
|
|
116 e2=cadr(e1);
|
|
117 e3=caddr(e1);
|
|
118 switch(car(e1)){
|
|
119 case GT:
|
|
120 unlist(e2);
|
|
121 printf(">");
|
|
122 unlist(e3);
|
|
123 return;
|
|
124 case LT:
|
|
125 unlist(e2);
|
|
126 unsym(LT);
|
|
127 unlist(e3);
|
|
128 return;
|
|
129 case RLVAR: case LVAR: case GVAR:
|
|
130 fprintf(vout,"%s",(char *)e3);
|
|
131 return;
|
|
132 case CONST:
|
|
133 fprintf(vout,"%d",e2);
|
|
134 return;
|
|
135 case POSTINC:
|
|
136 if(e3==1){
|
|
137 unlist(e2);
|
|
138 fprintf(vout,"++");
|
|
139 return;
|
|
140 }
|
|
141 else return;
|
|
142 case CPOSTINC:
|
|
143 unlist(e2);
|
|
144 fprintf(vout,"++");
|
|
145 return;
|
|
146 default:
|
|
147 return;
|
|
148 }
|
|
149 }
|
|
150
|
|
151 static int
|
|
152 unsym(int t) /*タイプをプリントする*/
|
|
153 {
|
|
154 char *symbuf;
|
|
155 switch(t){
|
|
156 case -1: symbuf="int"; break;
|
|
157 case -2: symbuf="char"; break;
|
|
158 case -3: symbuf="unsigned"; break;
|
|
159 case -4: symbuf="*"; break;
|
|
160 case -6: symbuf="struct"; break;
|
|
161 case -10: symbuf="static"; break;
|
|
162 case -34: symbuf="long"; break;
|
|
163 case -35: symbuf="short"; break;
|
|
164 case -36: symbuf="extern"; break;
|
|
165 case -37: symbuf="void"; break;
|
|
166 case CODE: symbuf="code"; break;
|
|
167 case MUL: symbuf="*"; break;
|
|
168 case MUL+AS: symbuf="*="; break;
|
|
169 case BAND: symbuf="&"; break;
|
|
170 case BAND+AS: symbuf="&="; break;
|
|
171 case LAND: symbuf="&&"; break;
|
|
172 case SUB: symbuf="-"; break;
|
|
173 case SUB+AS: symbuf="-="; break;
|
|
174 case DEC: symbuf="--"; break;
|
|
175 case ARROW: symbuf="->"; break;
|
|
176 case LNOT: symbuf="!"; break;
|
|
177 case NEQ: symbuf="!="; break;
|
|
178 case BNOT: symbuf="~"; break;
|
|
179 case ADD: symbuf="+"; break;
|
|
180 case ADD+AS: symbuf="+="; break;
|
|
181 case INC: symbuf="++"; break;
|
|
182 case MOD: symbuf="%"; break;
|
|
183 case MOD+AS: symbuf="%="; break;
|
|
184 case EOR: symbuf="^"; break;
|
|
185 case EOR+AS: symbuf="^="; break;
|
|
186 case BOR: symbuf="|"; break;
|
|
187 case BOR+AS: symbuf="||"; break;
|
|
188 case ASS: symbuf="="; break;
|
|
189 case EQ: symbuf="=="; break;
|
|
190 case RSHIFT: symbuf=">>"; break;
|
|
191 case RSHIFT+AS: symbuf=">>="; break;
|
|
192 case GT: symbuf=">"; break;
|
|
193 case GE: symbuf=">="; break;
|
|
194 case LSHIFT: symbuf="<<"; break;
|
|
195 case LSHIFT+AS: symbuf="<<="; break;
|
|
196 case LT: symbuf="<"; break;
|
|
197 case LE: symbuf="<="; break;
|
|
198 case LPAR: symbuf="("; break;
|
|
199 case RPAR: symbuf=")"; break;
|
|
200 case LBRA: symbuf="["; break;
|
|
201 case RBRA: symbuf="]"; break;
|
|
202 case LC: symbuf="{"; break;
|
|
203 case RC: symbuf="}"; break;
|
|
204 case COMMA: symbuf=","; break;
|
|
205 case COLON: symbuf=":"; break;
|
|
206 case COND: symbuf="?"; break;
|
|
207 case DOTS: symbuf="..."; break;
|
|
208 case PERIOD: symbuf="."; break;
|
|
209 case DIV: symbuf="/"; break;
|
|
210 case DIV+AS: symbuf="/="; break;
|
|
211 default:
|
|
212 return sym;
|
|
213 }
|
|
214 if((mode==GDECL)&&((t==INT)||(t==CHAR)))
|
|
215 fprintf(vout,"code ");
|
|
216 else
|
|
217 fprintf(vout,"%s",symbuf);
|
|
218 if((mode!=GDECL)&&((t==INT)||(t==CHAR)||(t==-3)||(t==-4)))
|
|
219 fprintf(cbc_fp1," %s",symbuf);
|
|
220 return sym;
|
|
221 }
|
|
222
|
|
223
|
|
224 /* end */
|