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

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

UserProc::SetParamsAndReturnTypeメソッドをリファクタリング
LexicalAnalysis.hのインクルードを除去した

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