source: dev/BasicCompiler_Common/Intermediate_Step2.cpp@ 16

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

グローバル領域外のConst定義を初期段階で収集しないようにしました。

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