source: dev/BasicCompiler_Common/Intermediate_Step2.cpp@ 6

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