source: dev/branches/x64_compiler_on_32/abdev/BasicCompiler_Common/Intermediate_Step2.cpp

Last change on this file was 751, checked in by イグトランス (egtra), 16 years ago

性能がよいというdlmallocを導入。

File size: 11.3 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5#include "../BasicCompiler_Common/common.h"
6
7
8char ConstructorDestructorSchedule[MAX_PATH];
9void MakeConstructorAndDestructor(char *buffer,int nowLine,char *ClassName){
10 int i,i2;
11 char temporary[MAX_PATH],*pTemp;
12 BOOL bConstructor,bDestructor;
13
14 ConstructorDestructorSchedule[0]=0;
15 bConstructor=0;
16 bDestructor=0;
17
18 for(i=nowLine;;i++){
19 if(buffer[i]=='\0') break;
20 if(buffer[i]==1&&buffer[i+1]==ESC_ENDCLASS){
21 if((!bConstructor)||(!bDestructor)){
22 pTemp=ConstructorDestructorSchedule;
23
24 lstrcpy(pTemp,"Public:");
25
26 if(!bConstructor){
27 //コンストラクタが無いときは生成する
28 sprintf(pTemp+lstrlen(pTemp),"%c%c%s():%c%c:",
29 1,ESC_SUB,
30 ClassName,
31 1,ESC_ENDSUB);
32 }
33
34 if(!bDestructor){
35 //デストラクタが無いときは生成する
36 sprintf(pTemp+lstrlen(pTemp),"%c%c~%s():%c%c:",
37 1,ESC_SUB,
38 ClassName,
39 1,ESC_ENDSUB);
40 }
41 }
42 break;
43 }
44
45 if(buffer[i]==1&&(buffer[i+1]==ESC_SUB||buffer[i+1]==ESC_FUNCTION)){
46 i+=2;
47 while(IsBlank(buffer[i])) i++;
48 if(buffer[i]=='~'){
49 //デストラクタ
50 bDestructor=1;
51 }
52 else{
53 //コンストラクタかどうかをチェック
54 for(i2=0;;i++,i2++){
55 if(!IsVariableChar(buffer[i])){
56 temporary[i2]=0;
57 break;
58 }
59 temporary[i2]=buffer[i];
60 }
61 if(lstrcmp(temporary,ClassName)==0) bConstructor=1;
62 }
63 }
64 }
65}
66void ChangeCommand(char *buffer,int nowLine,char *Command){
67 int i,i2,IsStr;
68 unsigned _int16 ComNum;
69 char com[8192],pam[8192];
70
71 static int nCountOfNonGlobalScope = 0;
72
73 if(Command[0]==1){
74 switch(Command[1]){
75 case ESC_SELECTCASE:
76 case ESC_CASE:
77 KillStringSpaces(Command+2);
78 break;
79 case ESC_WITH:
80 KillStringSpaces(Command+2);
81 break;
82 case ESC_TYPEDEF:
83 KillStringSpaces(Command+2);
84 break;
85 case ESC_DECLARE:
86 KillStringSpaces(Command+2);
87 break;
88 case ESC_IF:
89 KillStringSpaces(Command+2);
90 break;
91
92 case ESC_CLASS:
93 KillStringSpaces(Command+2);
94 i2 = 2;
95 if( Command[i2] == 1 && Command[i2+1] == ESC_ENUM )
96 {
97 i2 += 2;
98 }
99 else if( Command[i2] == 1 && Command[i2+1] == ESC_DELEGATE )
100 {
101 i2 += 2;
102 }
103 else if( memicmp( Command + i2, "Blittable(", 10 ) == 0 )
104 {
105 i2 += 10;
106 i2 = JumpStringInPare(Command,i2)+1;
107 }
108
109 // クラス名を取得
110 char className[VN_SIZE];
111 GetIdentifierToken( className, Command, i2 );
112
113 //コンストラクタ、デストラクタを暗黙的に生成
114 MakeConstructorAndDestructor(buffer,nowLine,className);
115 break;
116 case ESC_INTERFACE:
117 KillStringSpaces(Command+2);
118 break;
119 case ESC_ENDCLASS:
120 if(ConstructorDestructorSchedule[0]){
121 //生成されたコンストラクタ、デストラクタを挿入
122 sprintf(Command,"%s%c%c",ConstructorDestructorSchedule,1,ESC_ENDCLASS);
123 }
124 break;
125
126 case ESC_TYPE:
127 KillStringSpaces(Command+2);
128 break;
129
130 case ESC_CONST:
131 KillStringSpaces(Command+2);
132 if( Command[2] == 1 && Command[3] == ESC_ENUM ){
133 nCountOfNonGlobalScope++;
134 }
135 break;
136
137 case ESC_ENUM:
138 nCountOfNonGlobalScope++;
139 KillStringSpaces(Command+2);
140 break;
141
142 case ESC_ENDENUM:
143 nCountOfNonGlobalScope--;
144 break;
145
146 case ESC_INHERITS:
147 case ESC_IMPLEMENTS:
148 case ESC_VIRTUAL:
149 case ESC_OVERRIDE:
150 case ESC_ABSTRACT:
151 case ESC_SUB:
152 case ESC_FUNCTION:
153 case ESC_MACRO:
154 case ESC_STATIC:
155 case ESC_NAMESPACE:
156 case ESC_IMPORTS:
157 case ESC_DELEGATE:
158 case ESC_CATCH:
159 case ESC_THROW:
160 KillStringSpaces(Command+2);
161 break;
162 }
163 return;
164 }
165
166 bool isPare = false;
167 for(i=0;;i++){
168 if(Command[i]==' '||Command[i]=='\t'||Command[i]=='('||Command[i]=='\"'||Command[i]=='@'||Command[i]=='-'){
169 com[i]=0;
170 while(Command[i]==' '||Command[i]=='\t') i++;
171 if( Command[i] == '(' ) isPare = true;
172 break;
173 }
174 if(Command[i]=='='){
175 KillStringSpaces(Command);
176 return;
177 }
178 com[i]=Command[i];
179 if(Command[i]=='\0') break;
180 }
181
182 //マクロによるコマンド
183 i2=1;
184 if( nCountOfNonGlobalScope == 0 ){
185 //グローバル
186 if(lstrcmpi(com,"Open")==0) ComOpen(Command+i,pam,nowLine);
187 else if(lstrcmpi(com,"Close")==0) ComClose(Command+i,pam);
188 else if(lstrcmpi(com,"Field")==0||
189 lstrcmpi(com,"Get")==0||
190 lstrcmpi(com,"Put")==0) ComField(Command+i,pam);
191 else if(lstrcmpi(com,"Line")==0) ComLine(Command+i,pam,nowLine);
192 else if(lstrcmpi(com,"Circle")==0) ComCircle(Command+i,pam,nowLine);
193 else if(lstrcmpi(com,"PSet")==0) ComPSet(Command+i,pam,nowLine);
194 else if(lstrcmpi(com,"Paint")==0) ComPaint(Command+i,pam,nowLine);
195
196 else if(
197 lstrcmpi(com,"EXEC")==0||
198 lstrcmpi(com,"INPUT")==0||
199 lstrcmpi(com,"PRINT")==0||
200 lstrcmpi(com,"RANDOMIZE")==0||
201 ( lstrcmpi(com,"WRITE")==0 && isPare == false )||
202 lstrcmpi(com,"MSGBOX")==0||
203 lstrcmpi(com,"WINDOW")==0||
204 lstrcmpi(com,"DELWND")==0||
205 lstrcmpi(com,"INSMENU")==0||
206 lstrcmpi(com,"CHDIR")==0||
207 lstrcmpi(com,"MKDIR")==0||
208 lstrcmpi(com,"KILL")==0||
209 lstrcmpi(com,"CLS")==0||
210 lstrcmpi(com,"COLOR")==0||
211 lstrcmpi(com,"LOCATE")==0
212 ){
213 KillSpaces(Command+i,pam);
214
215 //大文字に変換
216 CharUpper(com);
217
218 sprintf(Command,"%s(%s)",com,pam);
219 return;
220 }
221
222 else i2=0;
223 }
224 else i2=0;
225 if(i2){
226 //大文字に変換
227 CharUpper(com);
228
229 sprintf(Command,"%s(%s)",com,pam);
230 return;
231 }
232
233
234
235 //コンパイラに搭載されるコマンド
236 if(lstrcmpi(com,"Do")==0){
237 KillSpaces(Command+i,pam);
238 ComNum=COM_DO;
239 }
240 else if(lstrcmpi(com,"goto")==0){
241 KillSpaces(Command+i,pam);
242 ComNum=COM_GOTO;
243 }
244 else if(lstrcmpi(com,"gosub")==0){
245 KillSpaces(Command+i,pam);
246 ComNum=COM_GOSUB;
247 }
248 else if(lstrcmpi(com,"Loop")==0){
249 if((Command[i]=='w'||Command[i]=='W')&&(Command[i+1]=='h'||Command[i+1]=='H')&&(Command[i+2]=='i'||Command[i+2]=='I')&&(Command[i+3]=='l'||Command[i+3]=='L')&&(Command[i+4]=='e'||Command[i+4]=='E')){
250 lstrcpy(pam,"0,");
251 KillSpaces(Command+i+5,pam+2);
252 }
253 else if((Command[i]=='u'||Command[i]=='U')&&(Command[i+1]=='n'||Command[i+1]=='N')&&(Command[i+2]=='t'||Command[i+2]=='T')&&(Command[i+3]=='i'||Command[i+3]=='I')&&(Command[i+4]=='l'||Command[i+4]=='L')){
254 lstrcpy(pam,"1,");
255 KillSpaces(Command+i+5,pam+2);
256 }
257 else pam[0]=0;
258 ComNum=COM_LOOP;
259 }
260 else if(lstrcmpi(com,"For")==0){
261 for(i2=0,IsStr=0;;i++,i2++){
262 while(Command[i]==' '||Command[i]=='\t') i++;
263 if(Command[i]=='\"') IsStr^=1;
264 if((Command[i-1]==' '||Command[i-1]=='\t')&&(Command[i]=='t'||Command[i]=='T')&&(Command[i+1]=='o'||Command[i+1]=='O')&&(Command[i+2]==' '||Command[i+2]=='\t')&&IsStr==0){
265 pam[i2]=',';
266 break;
267 }
268 pam[i2]=Command[i];
269 if(Command[i]=='\0') break;
270 }
271 if(Command[i]){
272 for(i+=3,i2++,IsStr=0;;i++,i2++){
273 while(Command[i]==' '||Command[i]=='\t') i++;
274 if((Command[i-1]==' '||Command[i-1]=='\t')&&(Command[i]=='s'||Command[i]=='S')&&(Command[i+1]=='t'||Command[i+1]=='T')&&(Command[i+2]=='e'||Command[i+2]=='E')&&(Command[i+3]=='p'||Command[i+3]=='P')&&(Command[i+4]==' '||Command[i+4]=='\t')){
275 pam[i2]=',';
276 i+=4;
277 continue;
278 }
279 pam[i2]=Command[i];
280 if(Command[i]=='\0') break;
281 }
282 }
283 ComNum=COM_FOR;
284 }
285 else if(lstrcmpi(com,"Foreach")==0)
286 {
287 for(i2=0,IsStr=0;;i++,i2++)
288 {
289 while(Command[i]==' '||Command[i]=='\t') i++;
290 if(Command[i]=='\"') IsStr^=1;
291 if((Command[i-1]==' '||Command[i-1]=='\t')&&(Command[i]=='i'||Command[i]=='I')&&(Command[i+1]=='n'||Command[i+1]=='N')&&(Command[i+2]==' '||Command[i+2]=='\t')&&IsStr==0){
292 pam[i2++] = 1;
293 pam[i2] = ESC_IN;
294 break;
295 }
296 pam[i2]=Command[i];
297 if(Command[i]=='\0') break;
298 }
299 if(Command[i])
300 {
301 lstrcpy( pam + i2 + 1, Command + i + 3 );
302 }
303 ComNum=COM_FOREACH;
304 }
305 else if(lstrcmpi(com,"Next")==0){
306 KillSpaces(Command+i,pam);
307 ComNum=COM_NEXT;
308 }
309 else if(lstrcmpi(com,"Return")==0){
310 KillSpaces(Command+i,pam);
311 ComNum=COM_RETURN;
312 }
313 else if(lstrcmpi(com,"While")==0){
314 KillSpaces(Command+i,pam);
315 ComNum=COM_WHILE;
316 }
317 else if(lstrcmpi(com,"Wend")==0){
318 pam[0]=0;
319 ComNum=COM_WEND;
320 }
321
322 //変数、データ操作
323 else if(lstrcmpi(com,"dim")==0){
324 KillSpaces(Command+i,pam);
325 ComNum=COM_DIM;
326 }
327 else if(lstrcmpi(com,"Delete")==0){
328 KillSpaces(Command+i,pam);
329 ComNum=COM_DELETE;
330 }
331 else if( lstrcmpi( com, "_System_SweepingDelete" ) == 0 ){
332 KillSpaces(Command+i,pam);
333 ComNum=COM_SWEEPINGDELETE;
334 }
335
336 //その他
337 else if(lstrcmpi(com,"Debug")==0){
338 pam[0]=0;
339 ComNum=COM_DEBUG;
340 }
341 else if(lstrcmpi(com,"let")==0){
342 KillSpaces(Command+i,pam);
343 ComNum=COM_LET;
344 }
345 else if(lstrcmpi(com,"rem")==0){
346 Command[0]=0;
347 return;
348 }
349
350 //ポインタ
351 else if(lstrcmpi(com,"SetDouble")==0){
352 KillSpaces(Command+i,pam);
353 ComNum=COM_SETDOUBLE;
354 }
355 else if(lstrcmpi(com,"SetSingle")==0){
356 KillSpaces(Command+i,pam);
357 ComNum=COM_SETSINGLE;
358 }
359 else if(lstrcmpi(com,"SetQWord")==0){
360 KillSpaces(Command+i,pam);
361 ComNum=COM_SETQWORD;
362 }
363 else if(lstrcmpi(com,"SetDWord")==0){
364 KillSpaces(Command+i,pam);
365 ComNum=COM_SETDWORD;
366 }
367 else if(lstrcmpi(com,"SetWord")==0){
368 KillSpaces(Command+i,pam);
369 ComNum=COM_SETWORD;
370 }
371 else if(lstrcmpi(com,"SetByte")==0){
372 KillSpaces(Command+i,pam);
373 ComNum=COM_SETBYTE;
374 }
375
376 else{
377 //その他のコマンド(一般コード)
378 lstrcpy(com,Command);
379 KillSpaces(com,Command);
380 return;
381 }
382
383 i=lstrlen(pam);
384 while(pam[i-1]==' '||pam[i-1]=='\t') i--;
385 pam[i]=0;
386 if(pam[0]) sprintf(Command,"%c%c%s",HIBYTE(ComNum),LOBYTE(ComNum),pam);
387 else sprintf(Command,"%c%c",HIBYTE(ComNum),LOBYTE(ComNum));
388
389 return;
390}
391
392void ChangeCommandToCode(char *buffer){
393 extern HANDLE hHeap;
394 int i,i2,i3,IsStr,CommandBufferSize;
395 char *temporary,*tempBase,temp2[VN_SIZE],*lpCommand;
396
397 tempBase=(char *)malloc((lstrlen(buffer)+1)*2+8192);
398 temporary=tempBase+1;
399 temporary[0]=0;
400 i=0;
401 i3=0;
402
403 CommandBufferSize=512;
404
405 lpCommand=(char *)malloc(CommandBufferSize);
406
407 while(1){
408 i2=0;
409 while(buffer[i]==' '||buffer[i]=='\t') i++;
410 while(buffer[i]>='0'&&buffer[i]<='9'){
411 temp2[i2]=buffer[i];
412 i++;
413 i2++;
414 }
415 temp2[i2]=0;
416 while(buffer[i]==' '||buffer[i]=='\t') i++;
417 for(i2=0,IsStr=0;;i++,i2++){
418 if(i2>=CommandBufferSize){ //バッファ領域が足りなくなった場合はバッファを増量する
419 CommandBufferSize+=512;
420 lpCommand=(char *)realloc(lpCommand,CommandBufferSize);
421 }
422 if(buffer[i]=='\"') IsStr^=1;
423 if(buffer[i]=='\n'||(buffer[i]==':'&&IsStr==0)||buffer[i]=='\0'){
424 lpCommand[i2]=0;
425
426 if(temp2[0]){
427 //行番号ラベル
428 sprintf(temporary+i3,"%c%c%s,",1,ESC_LINENUM,temp2);
429 i3+=lstrlen(temporary+i3);
430
431 temp2[0]=0;
432 }
433
434 //命令コードへ変換
435 if(i2){
436 //エラー用
437 extern int cp;
438 cp=i;
439
440 ChangeCommand(buffer,i,lpCommand);
441
442 lstrcpy(temporary+i3,lpCommand);
443 i3+=lstrlen(temporary+i3);
444 }
445
446 if(!(lpCommand[0]=='\0'&&buffer[i]==':')){
447 temporary[i3++]=buffer[i];
448 temporary[i3]=0;
449 }
450
451 if(buffer[i]=='\n'){
452 i++;
453 break;
454 }
455 else if(buffer[i]==':'){
456 while(buffer[i+1]==' '||buffer[i+1]=='\t') i++;
457 }
458 if(buffer[i]=='\0') break;
459 i2=-1;
460 continue;
461 }
462 lpCommand[i2]=buffer[i];
463 }
464 if(buffer[i]=='\0') break;
465 }
466 free(lpCommand);
467 lstrcpy(buffer,temporary);
468
469 free(tempBase);
470}
Note: See TracBrowser for help on using the repository browser.