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

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

コンパイラ組み込みテンプレートエンジンを実装。
静的リンクライブラリ、デバッグ情報の内部形式をテキストからバイナリに変更した。

File size: 14.1 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
6#include <Compiler.h>
7
8#include "../BasicCompiler_Common/common.h"
9
10
11void AddConstEnum( const NamespaceScopes &namespaceScopes, char *buffer){
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;
76 StaticCalculation(true, temp2,DEF_LONG,&i64data,Type());
77 NextValue=(int)i64data;
78 }
79
80 //定数を追加
81 compiler.GetObjectModule().meta.GetGlobalConsts().Add( namespaceScopes, temporary, NextValue);
82 }
83}
84bool GetConstInfo(void){
85 ////////////////////////////////////////////
86 // Const命令の情報を取得
87 ////////////////////////////////////////////
88
89 int i2;
90 char temporary[1024];
91
92 // 名前空間管理
93 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
94 namespaceScopes.clear();
95
96 extern char *basbuf;
97 for(int i=0;;i++){
98 if( basbuf[i] == '\0' ) break;
99
100 if( basbuf[i] == 1 && basbuf[i+1] == ESC_NAMESPACE ){
101 for(i+=2,i2=0;;i2++,i++){
102 if( IsCommandDelimitation( basbuf[i] ) ){
103 temporary[i2]=0;
104 break;
105 }
106 temporary[i2]=basbuf[i];
107 }
108 namespaceScopes.push_back( temporary );
109
110 continue;
111 }
112 else if( basbuf[i] == 1 && basbuf[i+1] == ESC_ENDNAMESPACE ){
113 if( namespaceScopes.size() <= 0 ){
114 SetError(12, "End Namespace", i );
115 }
116 else{
117 namespaceScopes.pop_back();
118 }
119
120 i += 2;
121 continue;
122 }
123
124 if( basbuf[i] == 1 ){
125 if(basbuf[i]==1&&basbuf[i+1]==ESC_CONST){
126 i+=2;
127
128 extern int cp;
129 cp=i; //エラー用
130
131
132 if(basbuf[i]==1&&basbuf[i+1]==ESC_ENUM){
133 AddConstEnum( namespaceScopes, basbuf+i);
134 continue;
135 }
136
137 for(i2=0;;i++,i2++){
138 if(basbuf[i]=='\"'){
139 temporary[i2]=basbuf[i];
140 for(i++,i2++;;i++,i2++){
141 temporary[i2]=basbuf[i];
142 if(basbuf[i]=='\"') break;
143 }
144 continue;
145 }
146 if(IsCommandDelimitation(basbuf[i])){
147 temporary[i2]=0;
148 break;
149 }
150 temporary[i2]=basbuf[i];
151 }
152 AddConst( namespaceScopes, temporary);
153 if(basbuf[i]=='\0') break;
154 }
155 else{
156 int result = JumpStatement( basbuf, i );
157 if( result == -1 ){
158 //エラー
159 return false;
160 }
161 else if( result == 1 ){
162 //ジャンプした場合
163 i--;
164 }
165 }
166 }
167 }
168
169 // イテレータを初期化
170 compiler.GetObjectModule().meta.GetGlobalConsts().Iterator_Init();
171 compiler.GetObjectModule().meta.GetGlobalConstMacros().Iterator_Init();
172
173 return true;
174}
175
176char ConstructorDestructorSchedule[MAX_PATH];
177void MakeConstructorAndDestructor(char *buffer,int nowLine,char *ClassName){
178 int i,i2;
179 char temporary[MAX_PATH],*pTemp;
180 BOOL bConstructor,bDestructor;
181
182 ConstructorDestructorSchedule[0]=0;
183 bConstructor=0;
184 bDestructor=0;
185
186 for(i=nowLine;;i++){
187 if(buffer[i]=='\0') break;
188 if(buffer[i]==1&&buffer[i+1]==ESC_ENDCLASS){
189 if((!bConstructor)||(!bDestructor)){
190 pTemp=ConstructorDestructorSchedule;
191
192 lstrcpy(pTemp,"Public:");
193
194 if(!bConstructor){
195 //コンストラクタが無いときは生成する
196 sprintf(pTemp+lstrlen(pTemp),"%c%c%s():%c%c:",
197 1,ESC_SUB,
198 ClassName,
199 1,ESC_ENDSUB);
200 }
201
202 if(!bDestructor){
203 //デストラクタが無いときは生成する
204 sprintf(pTemp+lstrlen(pTemp),"%c%c~%s():%c%c:",
205 1,ESC_SUB,
206 ClassName,
207 1,ESC_ENDSUB);
208 }
209 }
210 break;
211 }
212
213 if(buffer[i]==1&&(buffer[i+1]==ESC_SUB||buffer[i+1]==ESC_FUNCTION)){
214 i+=2;
215 while(IsBlank(buffer[i])) i++;
216 if(buffer[i]=='~'){
217 //デストラクタ
218 bDestructor=1;
219 }
220 else{
221 //コンストラクタかどうかをチェック
222 for(i2=0;;i++,i2++){
223 if(!IsVariableChar(buffer[i])){
224 temporary[i2]=0;
225 break;
226 }
227 temporary[i2]=buffer[i];
228 }
229 if(lstrcmp(temporary,ClassName)==0) bConstructor=1;
230 }
231 }
232 }
233}
234void ChangeCommand(char *buffer,int nowLine,char *Command){
235 int i,i2,IsStr;
236 unsigned _int16 ComNum;
237 char com[8192],pam[8192];
238
239 static int nCountOfNonGlobalScope = 0;
240
241 if(Command[0]==1){
242 switch(Command[1]){
243 case ESC_SELECTCASE:
244 case ESC_CASE:
245 KillStringSpaces(Command+2);
246 break;
247 case ESC_WITH:
248 KillStringSpaces(Command+2);
249 break;
250 case ESC_TYPEDEF:
251 KillStringSpaces(Command+2);
252 break;
253 case ESC_DECLARE:
254 KillStringSpaces(Command+2);
255 break;
256 case ESC_IF:
257 KillStringSpaces(Command+2);
258 break;
259
260 case ESC_CLASS:
261 KillStringSpaces(Command+2);
262 i2 = 2;
263 if( Command[i2] == 1 && Command[i2+1] == ESC_ENUM )
264 {
265 i2 += 2;
266 }
267 else if( Command[i2] == 1 && Command[i2+1] == ESC_DELEGATE )
268 {
269 i2 += 2;
270 }
271 else if( memicmp( Command + i2, "Blittable(", 10 ) == 0 )
272 {
273 i2 += 10;
274 i2 = JumpStringInPare(Command,i2)+1;
275 }
276
277 // クラス名を取得
278 char className[VN_SIZE];
279 GetIdentifierToken( className, Command, i2 );
280
281 //コンストラクタ、デストラクタを暗黙的に生成
282 MakeConstructorAndDestructor(buffer,nowLine,className);
283 break;
284 case ESC_INTERFACE:
285 KillStringSpaces(Command+2);
286 break;
287 case ESC_ENDCLASS:
288 if(ConstructorDestructorSchedule[0]){
289 //生成されたコンストラクタ、デストラクタを挿入
290 sprintf(Command,"%s%c%c",ConstructorDestructorSchedule,1,ESC_ENDCLASS);
291 }
292 break;
293
294 case ESC_TYPE:
295 KillStringSpaces(Command+2);
296 break;
297
298 case ESC_CONST:
299 KillStringSpaces(Command+2);
300 if( Command[2] == 1 && Command[3] == ESC_ENUM ){
301 nCountOfNonGlobalScope++;
302 }
303 break;
304
305 case ESC_ENUM:
306 nCountOfNonGlobalScope++;
307 KillStringSpaces(Command+2);
308 break;
309
310 case ESC_ENDENUM:
311 nCountOfNonGlobalScope--;
312 break;
313
314 case ESC_INHERITS:
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 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,"Next")==0){
451 KillSpaces(Command+i,pam);
452 ComNum=COM_NEXT;
453 }
454 else if(lstrcmpi(com,"Return")==0){
455 KillSpaces(Command+i,pam);
456 ComNum=COM_RETURN;
457 }
458 else if(lstrcmpi(com,"While")==0){
459 KillSpaces(Command+i,pam);
460 ComNum=COM_WHILE;
461 }
462 else if(lstrcmpi(com,"Wend")==0){
463 pam[0]=0;
464 ComNum=COM_WEND;
465 }
466
467 //変数、データ操作
468 else if(lstrcmpi(com,"dim")==0){
469 KillSpaces(Command+i,pam);
470 ComNum=COM_DIM;
471 }
472 else if(lstrcmpi(com,"Delete")==0){
473 KillSpaces(Command+i,pam);
474 ComNum=COM_DELETE;
475 }
476 else if( lstrcmpi( com, "_System_SweepingDelete" ) == 0 ){
477 KillSpaces(Command+i,pam);
478 ComNum=COM_SWEEPINGDELETE;
479 }
480
481 //その他
482 else if(lstrcmpi(com,"Debug")==0){
483 pam[0]=0;
484 ComNum=COM_DEBUG;
485 }
486 else if(lstrcmpi(com,"let")==0){
487 KillSpaces(Command+i,pam);
488 ComNum=COM_LET;
489 }
490 else if(lstrcmpi(com,"rem")==0){
491 Command[0]=0;
492 return;
493 }
494
495 //ポインタ
496 else if(lstrcmpi(com,"SetDouble")==0){
497 KillSpaces(Command+i,pam);
498 ComNum=COM_SETDOUBLE;
499 }
500 else if(lstrcmpi(com,"SetSingle")==0){
501 KillSpaces(Command+i,pam);
502 ComNum=COM_SETSINGLE;
503 }
504 else if(lstrcmpi(com,"SetQWord")==0){
505 KillSpaces(Command+i,pam);
506 ComNum=COM_SETQWORD;
507 }
508 else if(lstrcmpi(com,"SetDWord")==0){
509 KillSpaces(Command+i,pam);
510 ComNum=COM_SETDWORD;
511 }
512 else if(lstrcmpi(com,"SetWord")==0){
513 KillSpaces(Command+i,pam);
514 ComNum=COM_SETWORD;
515 }
516 else if(lstrcmpi(com,"SetByte")==0){
517 KillSpaces(Command+i,pam);
518 ComNum=COM_SETBYTE;
519 }
520
521 else{
522 //その他のコマンド(一般コード)
523 lstrcpy(com,Command);
524 KillSpaces(com,Command);
525 return;
526 }
527
528 i=lstrlen(pam);
529 while(pam[i-1]==' '||pam[i-1]=='\t') i--;
530 pam[i]=0;
531 if(pam[0]) sprintf(Command,"%c%c%s",HIBYTE(ComNum),LOBYTE(ComNum),pam);
532 else sprintf(Command,"%c%c",HIBYTE(ComNum),LOBYTE(ComNum));
533
534 return;
535}
536
537void ChangeCommandToCode(char *buffer){
538 extern HANDLE hHeap;
539 int i,i2,i3,IsStr,CommandBufferSize;
540 char *temporary,*tempBase,temp2[VN_SIZE],*lpCommand;
541
542 tempBase=(char *)HeapAlloc(hHeap,0,(lstrlen(buffer)+1)*2+8192);
543 temporary=tempBase+1;
544 temporary[0]=0;
545 i=0;
546 i3=0;
547
548 CommandBufferSize=512;
549 lpCommand=(char *)HeapAlloc(hHeap,0,CommandBufferSize);
550
551 while(1){
552 i2=0;
553 while(buffer[i]==' '||buffer[i]=='\t') i++;
554 while(buffer[i]>='0'&&buffer[i]<='9'){
555 temp2[i2]=buffer[i];
556 i++;
557 i2++;
558 }
559 temp2[i2]=0;
560 while(buffer[i]==' '||buffer[i]=='\t') i++;
561 for(i2=0,IsStr=0;;i++,i2++){
562 if(i2>=CommandBufferSize){ //バッファ領域が足りなくなった場合はバッファを増量する
563 CommandBufferSize+=512;
564 lpCommand=(char *)HeapReAlloc(hHeap,0,lpCommand,CommandBufferSize);
565 }
566 if(buffer[i]=='\"') IsStr^=1;
567 if(buffer[i]=='\n'||(buffer[i]==':'&&IsStr==0)||buffer[i]=='\0'){
568 lpCommand[i2]=0;
569
570 if(temp2[0]){
571 //行番号ラベル
572 sprintf(temporary+i3,"%c%c%s,",1,ESC_LINENUM,temp2);
573 i3+=lstrlen(temporary+i3);
574
575 temp2[0]=0;
576 }
577
578 //命令コードへ変換
579 if(i2){
580 //エラー用
581 extern int cp;
582 cp=i;
583
584 ChangeCommand(buffer,i,lpCommand);
585
586 lstrcpy(temporary+i3,lpCommand);
587 i3+=lstrlen(temporary+i3);
588 }
589
590 if(!(lpCommand[0]=='\0'&&buffer[i]==':')){
591 temporary[i3++]=buffer[i];
592 temporary[i3]=0;
593 }
594
595 if(buffer[i]=='\n'){
596 i++;
597 break;
598 }
599 else if(buffer[i]==':'){
600 while(buffer[i+1]==' '||buffer[i+1]=='\t') i++;
601 }
602 if(buffer[i]=='\0') break;
603 i2=-1;
604 continue;
605 }
606 lpCommand[i2]=buffer[i];
607 }
608 if(buffer[i]=='\0') break;
609 }
610 HeapDefaultFree(lpCommand);
611 lstrcpy(buffer,temporary);
612
613 HeapDefaultFree(tempBase);
614}
Note: See TracBrowser for help on using the repository browser.