source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/Intermediate_Step2.cpp@ 497

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

Messenger/ErrorMessengerクラスを導入。SetError関数によるエラー生成を廃止した。

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