source: dev/trunk/abdev/BasicCompiler_Common/Intermediate_Step2.cpp@ 229

Last change on this file since 229 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

File size: 14.0 KB
RevLine 
[206]1#include "stdafx.h"
2
[182]3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
[199]6#include <Compiler.h>
[195]7
[4]8#include "../BasicCompiler_Common/common.h"
9
10
[103]11void AddConstEnum( const NamespaceScopes &namespaceScopes, char *buffer){
[4]12 extern int cp;
13 int i=0,i2;
14
15 if(!(buffer[i]==1&&buffer[i+1]==ESC_ENUM)) return;
16 i+=2;
17
18 //列挙体の名前を取得
19 char temporary[VN_SIZE];
20 for(i2=0;;i++,i2++){
21 if(IsCommandDelimitation(buffer[i])){
22 temporary[i2]=0;
23 break;
24 }
25 if(!IsVariableChar(buffer[i])){
26 SetError(1,NULL,i);
27 break;
28 }
29 temporary[i2]=buffer[i];
30 }
31
32 if(buffer[i]=='\0'){
33 SetError(22,"Enum",cp);
34 return;
35 }
36
37 int NextValue=0;
38 while(1){
39 i++;
40
41 if(buffer[i]==1&&buffer[i+1]==ESC_ENDENUM) break;
42
43 for(i2=0;;i2++,i++){
44 if(IsCommandDelimitation(buffer[i])){
45 temporary[i2]=0;
46 break;
47 }
48 if(buffer[i]=='='){
49 temporary[i2]=0;
50 break;
51 }
52 temporary[i2]=buffer[i];
53 }
54 if(temporary[0]=='\0'){
55 if(buffer[i]=='\0'){
56 SetError(22,"Enum",cp);
57 break;
58 }
59 continue;
60 }
61
62 if(buffer[i]!='='){
63 NextValue++;
64 }
65 else{
66 char temp2[VN_SIZE];
67 for(i++,i2=0;;i2++,i++){
68 if(IsCommandDelimitation(buffer[i])){
69 temp2[i2]=0;
70 break;
71 }
72 temp2[i2]=buffer[i];
73 }
74
75 _int64 i64data;
[75]76 StaticCalculation(true, temp2,DEF_LONG,&i64data,Type());
[4]77 NextValue=(int)i64data;
78 }
79
80 //定数を追加
[206]81 compiler.GetMeta().GetGlobalConsts().Add( namespaceScopes, temporary, NextValue);
[4]82 }
83}
[16]84bool GetConstInfo(void){
[4]85 ////////////////////////////////////////////
86 // Const命令の情報を取得
87 ////////////////////////////////////////////
88
[103]89 int i2;
[4]90 char temporary[1024];
91
[103]92 // 名前空間管理
[199]93 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
[103]94 namespaceScopes.clear();
95
[206]96 //定数に関する情報を初期化
97 compiler.GetMeta().GetGlobalConsts().Clear();
98 compiler.GetMeta().GetGlobalConstMacros().Clear();
[4]99
100 extern char *basbuf;
[103]101 for(int i=0;;i++){
[16]102 if( basbuf[i] == '\0' ) break;
[103]103
104 if( basbuf[i] == 1 && basbuf[i+1] == ESC_NAMESPACE ){
105 for(i+=2,i2=0;;i2++,i++){
106 if( IsCommandDelimitation( basbuf[i] ) ){
107 temporary[i2]=0;
108 break;
109 }
110 temporary[i2]=basbuf[i];
111 }
112 namespaceScopes.push_back( temporary );
113
114 continue;
115 }
116 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENDNAMESPACE ){
117 if( namespaceScopes.size() <= 0 ){
118 SetError(12, "End Namespace", i );
119 }
120 else{
121 namespaceScopes.pop_back();
122 }
123
124 i += 2;
125 continue;
126 }
127
[16]128 if( basbuf[i] == 1 ){
129 if(basbuf[i]==1&&basbuf[i+1]==ESC_CONST){
130 i+=2;
[4]131
[16]132 extern int cp;
133 cp=i; //エラー用
[4]134
135
[16]136 if(basbuf[i]==1&&basbuf[i+1]==ESC_ENUM){
[103]137 AddConstEnum( namespaceScopes, basbuf+i);
[16]138 continue;
139 }
[4]140
[16]141 for(i2=0;;i++,i2++){
142 if(basbuf[i]=='\"'){
[4]143 temporary[i2]=basbuf[i];
[16]144 for(i++,i2++;;i++,i2++){
145 temporary[i2]=basbuf[i];
146 if(basbuf[i]=='\"') break;
147 }
148 continue;
[4]149 }
[16]150 if(IsCommandDelimitation(basbuf[i])){
151 temporary[i2]=0;
152 break;
153 }
154 temporary[i2]=basbuf[i];
[4]155 }
[206]156 AddConst( namespaceScopes, temporary);
[16]157 if(basbuf[i]=='\0') break;
158 }
159 else{
160 int result = JumpStatement( basbuf, i );
161 if( result == -1 ){
162 //エラー
163 return false;
[4]164 }
[16]165 else if( result == 1 ){
166 //ジャンプした場合
167 i--;
168 }
[4]169 }
170 }
171 }
[206]172
173 // イテレータを初期化
174 compiler.GetMeta().GetGlobalConsts().Iterator_Init();
175 compiler.GetMeta().GetGlobalConstMacros().Iterator_Init();
176
[16]177 return true;
[4]178}
179
180char ConstructorDestructorSchedule[MAX_PATH];
[75]181void MakeConstructorAndDestructor(char *buffer,int nowLine,char *ClassName){
[4]182 int i,i2;
183 char temporary[MAX_PATH],*pTemp;
184 BOOL bConstructor,bDestructor;
185
186 ConstructorDestructorSchedule[0]=0;
187 bConstructor=0;
188 bDestructor=0;
189
[75]190 for(i=nowLine;;i++){
[4]191 if(buffer[i]=='\0') break;
192 if(buffer[i]==1&&buffer[i+1]==ESC_ENDCLASS){
193 if((!bConstructor)||(!bDestructor)){
194 pTemp=ConstructorDestructorSchedule;
195
196 lstrcpy(pTemp,"Public:");
197
198 if(!bConstructor){
199 //コンストラクタが無いときは生成する
200 sprintf(pTemp+lstrlen(pTemp),"%c%c%s():%c%c:",
201 1,ESC_SUB,
202 ClassName,
203 1,ESC_ENDSUB);
204 }
205
206 if(!bDestructor){
207 //デストラクタが無いときは生成する
208 sprintf(pTemp+lstrlen(pTemp),"%c%c~%s():%c%c:",
209 1,ESC_SUB,
210 ClassName,
211 1,ESC_ENDSUB);
212 }
213 }
214 break;
215 }
216
217 if(buffer[i]==1&&(buffer[i+1]==ESC_SUB||buffer[i+1]==ESC_FUNCTION)){
218 i+=2;
219 while(IsBlank(buffer[i])) i++;
220 if(buffer[i]=='~'){
221 //デストラクタ
222 bDestructor=1;
223 }
224 else{
225 //コンストラクタかどうかをチェック
226 for(i2=0;;i++,i2++){
227 if(!IsVariableChar(buffer[i])){
228 temporary[i2]=0;
229 break;
230 }
231 temporary[i2]=buffer[i];
232 }
233 if(lstrcmp(temporary,ClassName)==0) bConstructor=1;
234 }
235 }
236 }
237}
[75]238void ChangeCommand(char *buffer,int nowLine,char *Command){
[4]239 int i,i2,IsStr;
240 unsigned _int16 ComNum;
241 char com[8192],pam[8192];
242
[42]243 static int nCountOfNonGlobalScope = 0;
244
[4]245 if(Command[0]==1){
246 switch(Command[1]){
247 case ESC_SELECTCASE:
248 case ESC_CASE:
249 KillStringSpaces(Command+2);
250 break;
251 case ESC_WITH:
252 KillStringSpaces(Command+2);
253 break;
254 case ESC_TYPEDEF:
255 KillStringSpaces(Command+2);
256 break;
257 case ESC_DECLARE:
258 KillStringSpaces(Command+2);
259 break;
260 case ESC_IF:
261 KillStringSpaces(Command+2);
262 break;
263
264 case ESC_CLASS:
265 KillStringSpaces(Command+2);
[92]266 i2 = 2;
267 if( Command[i2] == 1 && Command[i2+1] == ESC_ENUM ){
268 i2 += 2;
269 }
[128]270 else if( memicmp( Command + i2, "Blittable(", 10 ) == 0 ){
271 i2 += 10;
272 i2 = JumpStringInPare(Command,i2)+1;
273 }
[4]274
275 //コンストラクタ、デストラクタを暗黙的に生成
[92]276 MakeConstructorAndDestructor(buffer,nowLine,Command + i2);
[4]277 break;
278 case ESC_INTERFACE:
279 KillStringSpaces(Command+2);
280 break;
281 case ESC_ENDCLASS:
282 if(ConstructorDestructorSchedule[0]){
283 //生成されたコンストラクタ、デストラクタを挿入
284 sprintf(Command,"%s%c%c",ConstructorDestructorSchedule,1,ESC_ENDCLASS);
285 }
286 break;
287
288 case ESC_TYPE:
289 KillStringSpaces(Command+2);
290 break;
[42]291
[43]292 case ESC_CONST:
293 KillStringSpaces(Command+2);
294 if( Command[2] == 1 && Command[3] == ESC_ENUM ){
295 nCountOfNonGlobalScope++;
296 }
297 break;
298
[4]299 case ESC_ENUM:
[42]300 nCountOfNonGlobalScope++;
301 KillStringSpaces(Command+2);
302 break;
303
304 case ESC_ENDENUM:
305 nCountOfNonGlobalScope--;
306 break;
307
[4]308 case ESC_INHERITS:
309 case ESC_VIRTUAL:
310 case ESC_OVERRIDE:
311 case ESC_ABSTRACT:
312 case ESC_SUB:
313 case ESC_FUNCTION:
314 case ESC_MACRO:
315 case ESC_STATIC:
[100]316 case ESC_NAMESPACE:
[107]317 case ESC_IMPORTS:
[4]318 KillStringSpaces(Command+2);
319 break;
320 }
321 return;
322 }
323
[37]324 bool isPare = false;
[4]325 for(i=0;;i++){
326 if(Command[i]==' '||Command[i]=='\t'||Command[i]=='('||Command[i]=='\"'||Command[i]=='@'||Command[i]=='-'){
327 com[i]=0;
328 while(Command[i]==' '||Command[i]=='\t') i++;
[37]329 if( Command[i] == '(' ) isPare = true;
[4]330 break;
331 }
332 if(Command[i]=='='){
333 KillStringSpaces(Command);
334 return;
335 }
336 com[i]=Command[i];
337 if(Command[i]=='\0') break;
338 }
339
340 //マクロによるコマンド
341 i2=1;
[42]342 if( nCountOfNonGlobalScope == 0 ){
343 //グローバル
[75]344 if(lstrcmpi(com,"Open")==0) ComOpen(Command+i,pam,nowLine);
[42]345 else if(lstrcmpi(com,"Close")==0) ComClose(Command+i,pam);
346 else if(lstrcmpi(com,"Field")==0||
347 lstrcmpi(com,"Get")==0||
348 lstrcmpi(com,"Put")==0) ComField(Command+i,pam);
[75]349 else if(lstrcmpi(com,"Line")==0) ComLine(Command+i,pam,nowLine);
350 else if(lstrcmpi(com,"Circle")==0) ComCircle(Command+i,pam,nowLine);
351 else if(lstrcmpi(com,"PSet")==0) ComPSet(Command+i,pam,nowLine);
352 else if(lstrcmpi(com,"Paint")==0) ComPaint(Command+i,pam,nowLine);
[4]353
[42]354 else if(
355 lstrcmpi(com,"EXEC")==0||
356 lstrcmpi(com,"INPUT")==0||
357 lstrcmpi(com,"PRINT")==0||
358 lstrcmpi(com,"RANDOMIZE")==0||
359 ( lstrcmpi(com,"WRITE")==0 && isPare == false )||
360 lstrcmpi(com,"MSGBOX")==0||
361 lstrcmpi(com,"WINDOW")==0||
362 lstrcmpi(com,"DELWND")==0||
363 lstrcmpi(com,"INSMENU")==0||
364 lstrcmpi(com,"CHDIR")==0||
365 lstrcmpi(com,"MKDIR")==0||
366 lstrcmpi(com,"KILL")==0||
367 lstrcmpi(com,"CLS")==0||
368 lstrcmpi(com,"COLOR")==0||
369 lstrcmpi(com,"LOCATE")==0
370 ){
371 KillSpaces(Command+i,pam);
[4]372
[42]373 //大文字に変換
374 CharUpper(com);
[4]375
[42]376 sprintf(Command,"%s(%s)",com,pam);
377 return;
378 }
379
380 else i2=0;
[4]381 }
382 else i2=0;
383 if(i2){
384 //大文字に変換
385 CharUpper(com);
386
387 sprintf(Command,"%s(%s)",com,pam);
388 return;
389 }
390
391
392
393 //コンパイラに搭載されるコマンド
394 if(lstrcmpi(com,"Do")==0){
395 KillSpaces(Command+i,pam);
396 ComNum=COM_DO;
397 }
398 else if(lstrcmpi(com,"goto")==0){
399 KillSpaces(Command+i,pam);
400 ComNum=COM_GOTO;
401 }
402 else if(lstrcmpi(com,"gosub")==0){
403 KillSpaces(Command+i,pam);
404 ComNum=COM_GOSUB;
405 }
406 else if(lstrcmpi(com,"Loop")==0){
407 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')){
408 lstrcpy(pam,"0,");
409 KillSpaces(Command+i+5,pam+2);
410 }
411 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')){
412 lstrcpy(pam,"1,");
413 KillSpaces(Command+i+5,pam+2);
414 }
415 else pam[0]=0;
416 ComNum=COM_LOOP;
417 }
418 else if(lstrcmpi(com,"For")==0){
419 for(i2=0,IsStr=0;;i++,i2++){
420 while(Command[i]==' '||Command[i]=='\t') i++;
421 if(Command[i]=='\"') IsStr^=1;
422 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){
423 pam[i2]=',';
424 break;
425 }
426 pam[i2]=Command[i];
427 if(Command[i]=='\0') break;
428 }
429 if(Command[i]){
430 for(i+=3,i2++,IsStr=0;;i++,i2++){
431 while(Command[i]==' '||Command[i]=='\t') i++;
432 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')){
433 pam[i2]=',';
434 i+=4;
435 continue;
436 }
437 pam[i2]=Command[i];
438 if(Command[i]=='\0') break;
439 }
440 }
441 ComNum=COM_FOR;
442 }
443 else if(lstrcmpi(com,"Next")==0){
444 KillSpaces(Command+i,pam);
445 ComNum=COM_NEXT;
446 }
447 else if(lstrcmpi(com,"Return")==0){
448 KillSpaces(Command+i,pam);
449 ComNum=COM_RETURN;
450 }
451 else if(lstrcmpi(com,"While")==0){
452 KillSpaces(Command+i,pam);
453 ComNum=COM_WHILE;
454 }
455 else if(lstrcmpi(com,"Wend")==0){
456 pam[0]=0;
457 ComNum=COM_WEND;
458 }
459
460 //変数、データ操作
461 else if(lstrcmpi(com,"dim")==0){
462 KillSpaces(Command+i,pam);
463 ComNum=COM_DIM;
464 }
465 else if(lstrcmpi(com,"Delete")==0){
466 KillSpaces(Command+i,pam);
467 ComNum=COM_DELETE;
468 }
[64]469 else if( lstrcmpi( com, "_System_SweepingDelete" ) == 0 ){
470 KillSpaces(Command+i,pam);
471 ComNum=COM_SWEEPINGDELETE;
472 }
[4]473
474 //その他
475 else if(lstrcmpi(com,"Debug")==0){
476 pam[0]=0;
477 ComNum=COM_DEBUG;
478 }
479 else if(lstrcmpi(com,"let")==0){
480 KillSpaces(Command+i,pam);
481 ComNum=COM_LET;
482 }
483 else if(lstrcmpi(com,"rem")==0){
484 Command[0]=0;
485 return;
486 }
487
488 //ポインタ
489 else if(lstrcmpi(com,"SetDouble")==0){
490 KillSpaces(Command+i,pam);
491 ComNum=COM_SETDOUBLE;
492 }
493 else if(lstrcmpi(com,"SetSingle")==0){
494 KillSpaces(Command+i,pam);
495 ComNum=COM_SETSINGLE;
496 }
497 else if(lstrcmpi(com,"SetQWord")==0){
498 KillSpaces(Command+i,pam);
499 ComNum=COM_SETQWORD;
500 }
501 else if(lstrcmpi(com,"SetDWord")==0){
502 KillSpaces(Command+i,pam);
503 ComNum=COM_SETDWORD;
504 }
505 else if(lstrcmpi(com,"SetWord")==0){
506 KillSpaces(Command+i,pam);
507 ComNum=COM_SETWORD;
508 }
509 else if(lstrcmpi(com,"SetByte")==0){
510 KillSpaces(Command+i,pam);
511 ComNum=COM_SETBYTE;
512 }
513
514 else{
515 //その他のコマンド(一般コード)
516 lstrcpy(com,Command);
517 KillSpaces(com,Command);
518 return;
519 }
520
521 i=lstrlen(pam);
522 while(pam[i-1]==' '||pam[i-1]=='\t') i--;
523 pam[i]=0;
524 if(pam[0]) sprintf(Command,"%c%c%s",HIBYTE(ComNum),LOBYTE(ComNum),pam);
525 else sprintf(Command,"%c%c",HIBYTE(ComNum),LOBYTE(ComNum));
526
527 return;
528}
529
530void ChangeCommandToCode(char *buffer){
531 extern HANDLE hHeap;
532 int i,i2,i3,IsStr,CommandBufferSize;
533 char *temporary,*tempBase,temp2[VN_SIZE],*lpCommand;
534
535 tempBase=(char *)HeapAlloc(hHeap,0,(lstrlen(buffer)+1)*2+8192);
536 temporary=tempBase+1;
537 temporary[0]=0;
538 i=0;
539 i3=0;
540
541 CommandBufferSize=512;
542 lpCommand=(char *)HeapAlloc(hHeap,0,CommandBufferSize);
543
544 while(1){
545 i2=0;
546 while(buffer[i]==' '||buffer[i]=='\t') i++;
547 while(buffer[i]>='0'&&buffer[i]<='9'){
548 temp2[i2]=buffer[i];
549 i++;
550 i2++;
551 }
552 temp2[i2]=0;
553 while(buffer[i]==' '||buffer[i]=='\t') i++;
554 for(i2=0,IsStr=0;;i++,i2++){
555 if(i2>=CommandBufferSize){ //バッファ領域が足りなくなった場合はバッファを増量する
556 CommandBufferSize+=512;
557 lpCommand=(char *)HeapReAlloc(hHeap,0,lpCommand,CommandBufferSize);
558 }
559 if(buffer[i]=='\"') IsStr^=1;
560 if(buffer[i]=='\n'||(buffer[i]==':'&&IsStr==0)||buffer[i]=='\0'){
561 lpCommand[i2]=0;
562
563 if(temp2[0]){
564 //行番号ラベル
565 sprintf(temporary+i3,"%c%c%s,",1,ESC_LINENUM,temp2);
566 i3+=lstrlen(temporary+i3);
567
568 temp2[0]=0;
569 }
570
571 //命令コードへ変換
572 if(i2){
573 //エラー用
574 extern int cp;
575 cp=i;
576
577 ChangeCommand(buffer,i,lpCommand);
578
579 lstrcpy(temporary+i3,lpCommand);
580 i3+=lstrlen(temporary+i3);
581 }
582
583 if(!(lpCommand[0]=='\0'&&buffer[i]==':')){
584 temporary[i3++]=buffer[i];
585 temporary[i3]=0;
586 }
587
588 if(buffer[i]=='\n'){
589 i++;
590 break;
591 }
592 else if(buffer[i]==':'){
593 while(buffer[i+1]==' '||buffer[i+1]=='\t') i++;
594 }
595 if(buffer[i]=='\0') break;
596 i2=-1;
597 continue;
598 }
599 lpCommand[i2]=buffer[i];
600 }
601 if(buffer[i]=='\0') break;
602 }
603 HeapDefaultFree(lpCommand);
604 lstrcpy(buffer,temporary);
605
606 HeapDefaultFree(tempBase);
607}
Note: See TracBrowser for help on using the repository browser.