source: dev/trunk/abdev/BasicCompiler_Common/src/Source.cpp@ 293

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

ジェネリクスのベースを実装

File size: 20.9 KB
RevLine 
[266]1#include "stdafx.h"
2
3#include <boost/foreach.hpp>
4
5#include <jenga/include/common/Directory.h>
6
7#include <jenga/include/smoothie/Smoothie.h>
8#include <jenga/include/smoothie/BasicFixed.h>
9#include <jenga/include/smoothie/LexicalAnalysis.h>
10#include <jenga/include/smoothie/SmoothieException.h>
11
12#include <Source.h>
13#include <Compiler.h>
14
15
16const string BasicSource::generateDirectiveName = "#generate";
17
18
19class CDefine{
20 vector<string> names;
21public:
22 CDefine( int majorVer );
23 ~CDefine();
24 void Free();
25 void Init( int majorVer );
26
27 BOOL add(char *name);
28 BOOL undef(char *name);
29 BOOL check(char *name);
30 void preprocessor_ifdef(char *buffer,bool isNot);
31 void DirectiveIfdef(char *buffer);
32};
33// TODO: バージョン番号の識別子defineが未完成
34CDefine objDefine(0);
35
36
37//////////////////////////////////////
38// #requireの管理
39//////////////////////////////////////
40class CRequireFiles{
41 char **ppFilePath;
42 int count;
43public:
44 CRequireFiles(){
45 ppFilePath = (char **)malloc( 1 );
46 count = 0;
47 }
48 ~CRequireFiles(){
49 for( int i = 0; i < count; i++ ){
50 free( ppFilePath[i] );
51 }
52 free( ppFilePath );
53 }
54 void clear(){
55 for( int i = 0; i < count; i++ ){
56 free( ppFilePath[i] );
57 }
58 free( ppFilePath );
59
60 ppFilePath = (char **)malloc( 1 );
61 count = 0;
62 }
63 bool IsIncluded( const char *includeFilePath ){
64 // '/' → '\\'
65 char tempPath[MAX_PATH];
66 lstrcpy( tempPath, includeFilePath );
67 for( int i=0; tempPath[i]; i++ ){
68 if( tempPath[i] == '/' ){
69 tempPath[i] = '\\';
70 }
71 }
72
73 for( int i=0; i<count; i++ ){
74 if( lstrcmpi( ppFilePath[i], tempPath ) == 0 ){
75 return true;
76 }
77 }
78 return false;
79 }
80 void Add( const char *includeFilePath ){
81 // '/' → '\\'
82 char tempPath[MAX_PATH];
83 lstrcpy( tempPath, includeFilePath );
84 for( int i=0; tempPath[i]; i++ ){
85 if( tempPath[i] == '/' ){
86 tempPath[i] = '\\';
87 }
88 }
89
90 //既に読み込まれているとき
91 if( IsIncluded( tempPath ) ) return;
92
93 //追加
94 ppFilePath = (char **)realloc(ppFilePath, ( count + 1 ) * sizeof(char *) );
95 ppFilePath[count] = (char *)malloc( lstrlen(tempPath) + 1 );
96 lstrcpy( ppFilePath[count], tempPath );
97 count++;
98 }
99};
100CRequireFiles requireFiles;
101
102
103//////////////////////////////////////
104// #define間するクラス
105//////////////////////////////////////
106
107CDefine::CDefine( int majorVer ){
108 Init( majorVer );
109}
110CDefine::~CDefine(){
111}
112void CDefine::Init( int majorVer ){
113 names.clear();
114
115 extern BOOL bDebugCompile;
116 if(bDebugCompile) add("_DEBUG");
117
118#ifdef _AMD64_
119 add("_WIN64");
120#endif
121
122 if( compiler.IsDll() ){
123 add("_DLL");
124 }
125
126 if( Smoothie::IsUnicode() ){
127 add( "UNICODE" );
128 }
129
130 char temporary[255];
131 sprintf(temporary,"_AB_VER%d",majorVer);
132 add(temporary);
133}
134BOOL CDefine::add(char *name){
135 extern HANDLE hHeap;
136
137 //重複チェック
138 if(check(name)) return 0;
139
140 //追加
141 names.push_back( name );
142
143 return 1;
144}
145BOOL CDefine::undef(char *name){
146 vector<string>::iterator i = names.begin();
147 BOOST_FOREACH( const string &temp, names ){
148 if( temp == name ){
149 names.erase( i );
150 return 1;
151 }
152 i++;
153 }
154
155 return 0;
156}
157BOOL CDefine::check(char *name){
158
159 //重複チェック
160 BOOST_FOREACH( const string &temp, names ){
161 if( temp == name ){
162 return 1;
163 }
164 }
165 return 0;
166}
167
168int Search_endif(char *buffer,int i, int *pLine = 0){
169 for(;;i++){
170 if(buffer[i]=='\0') break;
171
172 if( buffer[i] == '\n' ){
173 if( pLine ){
174 (*pLine)++;
175 }
176 }
177
178 if(buffer[i-1]=='\n'){
179 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
180 i=Search_endif(buffer,i+6, pLine);
181 if(buffer[i]=='\0') break;
182 continue;
183 }
184 else if(memicmp(buffer+i,"#endif",6)==0){
185 break;
186 }
187 }
188 }
189 return i;
190}
191
192void CDefine::preprocessor_ifdef(char *buffer,bool isNot){
193 int i,i2,i3;
194 char temporary[VN_SIZE];
195
196 if(isNot) i=lstrlen("#ifndef");
197 else i=lstrlen("#ifdef");
198 while(buffer[i]==' '||buffer[i]=='\t') i++;
199
200 for(i2=0;;i++,i2++){
201 if(buffer[i]=='\n'||buffer[i]=='\0'){
202 temporary[i2]=0;
203 break;
204 }
205 temporary[i2]=buffer[i];
206 }
207
208 int sw=0;
209 if(check(temporary)) sw=1;
210
211 if(isNot){
212 //#ifndefのとき(反対にする)
213 if(sw) sw=0;
214 else sw=1;
215 }
216
217 //#ifdefの行を消去
218 Text::SlideString(buffer+i,-i);
219 i=0;
220
221 BOOL bElse=0;
222 if(sw){
223 //TRUEのとき
224
225 //#else、#endifを探索
226 for(;;i++){
227 if(buffer[i]=='\0') break;
228
229 if(i==0||buffer[i-1]=='\n'){
230 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
231 i=Search_endif(buffer,i+6);
232 if(buffer[i]=='\0') break;
233 continue;
234 }
235 else if(memicmp(buffer+i,"#else",5)==0){
236 i2=5;
237 bElse=1;
238 break;
239 }
240 else if(memicmp(buffer+i,"#endif",6)==0){
241 i2=6;
242 bElse=0;
243 break;
244 }
245 }
246 }
247
248 //行を消去
249 Text::SlideString(buffer+i+i2,-i2);
250
251 if(bElse){
252 //#elseがある場合はその区間を消去
253
254 for(i2=i,i3=0;;i2++){
255 if(buffer[i2]=='\0') break;
256
257 if(buffer[i2]=='\n') i3++;
258
259 if(i2==0||buffer[i2-1]=='\n'){
260 if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){
261 i2=Search_endif(buffer,i2+6, &i3 );
262 if(buffer[i2]=='\0') break;
263 continue;
264 }
265 if(memicmp(buffer+i2,"#endif",6)==0){
266 i2+=6;
267 break;
268 }
269 }
270 }
271
272 //ソースコード区間を消去し、改行コードを挿入
273 Text::SlideString(buffer+i2,i-i2+i3);
274 memset(buffer+i,'\n',i3);
275 }
276 }
277 else{
278 //FALSEのとき
279
280 //#else、#endifを探索
281 for(i2=i,i3=0;;i2++){
282 if(buffer[i2]=='\0') break;
283
284 if(buffer[i2]=='\n') i3++;
285
286 if(i2==0||buffer[i2-1]=='\n'){
287 if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){
288 i2=Search_endif(buffer,i2+6, &i3 );
289 if(buffer[i2]=='\0') break;
290 continue;
291 }
292 else if(memicmp(buffer+i2,"#else",5)==0){
293 i2+=5;
294 bElse=1;
295 break;
296 }
297 else if(memicmp(buffer+i2,"#endif",6)==0){
298 i2+=6;
299 bElse=0;
300 break;
301 }
302 }
303 }
304
305 //ソースコード区間を消去し、改行コードを挿入
306 Text::SlideString(buffer+i2,i-i2+i3);
307 memset(buffer+i,'\n',i3);
308
309 if(bElse){
310 //#endifを探索
311 for(;;i++){
312 if(buffer[i]=='\0') break;
313
314 if(i==0||buffer[i-1]=='\n'){
315 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){
316 i=Search_endif(buffer,i+6);
317 if(buffer[i]=='\0') break;
318 continue;
319 }
320 else if(memicmp(buffer+i,"#endif",6)==0){
321 i2=6;
322 bElse=0;
323 break;
324 }
325 }
326 }
327
328 //行を消去
329 Text::SlideString(buffer+i+i2,-i2);
330 }
331 }
332}
333
334
335void CDefine::DirectiveIfdef(char *buffer){
336 int i,i2,i3,sw;
337 char temporary[VN_SIZE];
338
339 for(i=0;;i++){
340 if(buffer[i]=='\0') break;
341
342 if(i==0||(i>=1&&buffer[i-1]=='\n')){
343 sw=0;
344 if(memicmp(buffer+i,"#define",7)==0){
345 i2=i+7;
346 while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
347
348 for(i3=0;;i2++,i3++){
349 if(buffer[i2]=='\n'||buffer[i2]=='\0'){
350 temporary[i3]=0;
351 break;
352 }
353 temporary[i3]=buffer[i2];
354 }
355
356 add(temporary);
357
358 i2-=i;
359
360 //ディレクティブを消去
361 Text::SlideString(buffer+i+i2,-i2);
362 }
363 if(memicmp(buffer+i,"#undef",6)==0){
364 i2=i+7;
365 while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
366
367 for(i3=0;;i2++,i3++){
368 if(buffer[i2]=='\n'||buffer[i2]=='\0'){
369 temporary[i3]=0;
370 break;
371 }
372 temporary[i3]=buffer[i2];
373 }
374
375 undef(temporary);
376
377 i2-=i;
378
379 //ディレクティブを消去
380 Text::SlideString(buffer+i+i2,-i2);
381 }
382 else if(memicmp(buffer+i,"#ifdef",6)==0){
383 preprocessor_ifdef(buffer+i,false);
384 continue;
385 }
386 else if(memicmp(buffer+i,"#ifndef",7)==0){
387 preprocessor_ifdef(buffer+i,true);
388 continue;
389 }
390 else continue;
391 }
392 }
393}
394
395
396
397
398bool Text::ReadFile( const string &filePath ){
399 //ファイルオープン
400 HANDLE hFile=CreateFile(filePath.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
401 if(hFile==INVALID_HANDLE_VALUE){
402 return false;
403 }
404
405 length = GetFileSize( hFile, NULL );
406
407 buffer = (char *)realloc( buffer, length + 1 );
408
409 //読み込み
410 DWORD dwAccBytes;
411 ::ReadFile(hFile,buffer,length,&dwAccBytes,0);
412 buffer[dwAccBytes]=0;
413
414 //ファイルクローズ
415 CloseHandle(hFile);
416
417 return true;
418}
419
420void BasicSource::ChangeReturnLineChar(){
421 int i,i2;
422
423#ifdef _DEBUG
424 //改行コードの整合性チェック
425 for( i=0; ; i++ ){
426 if( buffer[i] == '\0' ){
427 break;
428 }
429 if( buffer[i]!='\r' && buffer[i+1]=='\n'
430 || buffer[i]=='\r' && buffer[i+1]!='\n' ){
431 char temporary[255];
432 strncpy( temporary, buffer + i-100, 130 );
433 temporary[130] = 0;
434 for(int i2=0; ;i2++){
435 if(temporary[i2]=='\r') temporary[i2]='A';
436 if(temporary[i2]=='\n') temporary[i2]='B';
437 if(temporary[i2]=='\0') break;
438 }
439
440 extern HWND hOwnerEditor;
441 MessageBox( hOwnerEditor, temporary, "改行コードの整合性チェック", MB_OK | MB_ICONEXCLAMATION );
442 }
443 }
444#endif
445
446 //改行コードのCRLFをLFに変換
447 for(i=0,i2=0;;i++,i2++){
448 if(buffer[i]=='\r'&&buffer[i+1]=='\n') i++;
449 buffer[i2]=buffer[i];
450 if(buffer[i]=='\0') break;
451 }
452
453 length = i;
454}
455
456void BasicSource::RemoveComments(){
457 int i,i2,i3,IsStr;
458 char *temporary;
459 temporary=(char *)GlobalAlloc(GMEM_FIXED,lstrlen(buffer)+1);
460 for(i=0,i2=0,i3=0,IsStr=0;;i++,i2++){
461 if(buffer[i]=='\"') IsStr^=1;
462 if(buffer[i]=='\n'||buffer[i]=='\0'){
463 i2--;
464 while(temporary[i2]==' '||temporary[i2]=='\t') i2--;
465 i2++;
466
467 if(i3){
468 //複数行に渡る注釈文の中に改行が存在するとき
469 memset(temporary+i2,'\n',i3);
470 i2+=i3;
471 i3=0;
472 }
473 }
474 if(buffer[i]=='\''&&IsStr==0){
475 //注釈文
476 i2--;
477 while(temporary[i2]==' '||temporary[i2]=='\t') i2--;
478 i2++;
479 while(buffer[i]!='\n'&&buffer[i]!='\0') i++;
480 }
481 if(buffer[i]=='/'&&buffer[i+1]=='*'&&IsStr==0){
482 //注釈文(複数行)
483 i+=2;
484 i3=0;
485 while(!(buffer[i]=='*'&&buffer[i+1]=='/')){
486 if(buffer[i]=='\n') i3++;
487 if(buffer[i]=='\0') break;
488 i++;
489 }
490 if(buffer[i]){
491 i+=2;
492 }
493 i--;
494 i2--;
495 continue;
496 }
497 temporary[i2]=buffer[i];
498 if(buffer[i]=='\0') break;
499 }
500 lstrcpy(buffer,temporary);
501 GlobalFree(temporary);
502}
503
504bool BasicSource::ReadFile_InIncludeDirective( const string &filePath ){
505 if( !Text::ReadFile( filePath ) ){
506 return false;
507 }
508
509 // 改行コードをCRLFからLFに変換
510 ChangeReturnLineChar();
511
512 // コメントを削除
513 RemoveComments();
514
515 // #ifdefディレクティブを処理
516 objDefine.DirectiveIfdef( buffer );
517
518 // アンダーバーによる改行を正規表現に戻す
519 RemoveReturnLineUnderbar();
520
521 // ダミー改行をセット
522 Realloc( length + 2 );
523 Text::SlideString( buffer, 2 );
524 buffer[0] = '\n';
525 buffer[1] = '\n';
526
527 return true;
528}
529
530void BasicSource::DirectiveIncludeOrRequire(){
531 extern HANDLE hHeap;
532 extern char szIncludeDir[MAX_PATH];
533 extern char BasicCurDir[MAX_PATH];
534 int i,i2,i3,sw1,LineNum,FileLayer[255],layer,LastFileByte[255];
535 char temporary[MAX_PATH],temp2[MAX_PATH+255],*LayerDir[255];
536
537 layer=0;
538 FileLayer[layer]=0;
539 LastFileByte[layer]=GetLength();
540 LineNum=0;
541
[279]542 if( includedFilesRelation.GetLineCounts() != 0 )
543 {
544 Jenga::Throw( "インクルードファイル構造の初期値が不正" );
545 }
546
547 // メインソースコード
548 extern char SourceFileName[MAX_PATH];
549 FileLayer[layer] = includedFilesRelation.AddFile( SourceFileName );
550
[266]551 //参照ディレクトリ
552 LayerDir[0]=(char *)malloc(lstrlen(BasicCurDir)+1);
553 lstrcpy(LayerDir[0],BasicCurDir);
554
555 for(i=0;;i++){
556 if(buffer[i]=='\0'){
557 break;
558 }
559 if(buffer[i]=='\n'){
[279]560 includedFilesRelation.AddLine( FileLayer[layer] );
[266]561 }
562 if(i>LastFileByte[layer]){
563 free(LayerDir[layer]);
564 LayerDir[layer]=0;
565 layer--;
566 }
567 if((buffer[i-1]=='\n'||i==0)&&buffer[i]=='#'){
568 bool isRequire = false;
569 if(memcmp( buffer + i + 1, "include", 7 ) == 0
570 || memcmp( buffer + i + 1, "require", 7 ) == 0){
571
572 //#requireの場合
573 if( buffer[i + 1] == 'r' ) isRequire = true;
574
575 i2=i+8;
576 while(buffer[i2]==' '||buffer[i2]=='\t') i2++;
577
578 if(buffer[i2]=='\"') sw1=0;
579 else if(buffer[i2]=='<') sw1=1;
580 i2++;
581
582 for(i3=0;;i2++,i3++){
583 if((buffer[i2]=='\"'&&sw1==0)||(buffer[i2]=='>'&&sw1==1)||buffer[i2]=='\n'||buffer[i2]=='\0'){
584 temporary[i3]=0;
585 break;
586 }
587 temporary[i3]=buffer[i2];
588 }
589 while(buffer[i2]!='\n'&&buffer[i2]!='\0') i2++;
590
591 if(sw1){
592 sprintf(temp2,"%s%s",szIncludeDir,temporary);
593 lstrcpy(temporary,temp2);
594 }
595 else{
596 Jenga::Common::Directory dir( LayerDir[layer] );
597 lstrcpy( temporary, dir.GetFullPath( temporary ).c_str() );
598 }
599 }
600 else if(memcmp(buffer+i+1,"prompt",6)==0){
601 i2=i+7;
602 sprintf(temporary,"%sbasic\\prompt.sbp",szIncludeDir);
603 }
604 else if(memcmp(buffer+i+1,"N88BASIC",8)==0){
605 i2=i+9;
606 sprintf(temporary,"%sbasic\\prompt.sbp",szIncludeDir);
607 }
608 else if(memcmp(buffer+i+1,"console",7)==0){
609 //サブシステム タイプをCUIに変更
610 extern unsigned short TypeOfSubSystem;
611 TypeOfSubSystem=IMAGE_SUBSYSTEM_WINDOWS_CUI;
612
613 i2=i+8;
614 sprintf(temporary,"%sbasic\\dos_console.sbp",szIncludeDir);
615 }
616 else continue;
617
618 if(i){
619 //ディレクティブが消えるため、一行減ってしまうのを防ぐ(basic.sbpを除く)
620 Text::SlideString(buffer+i2,1);
621 buffer[i2]='\n';
622 for(i3=0;i3<=layer;i3++) LastFileByte[i3]++;
623 }
624
625 layer++;
[279]626 FileLayer[layer] = includedFilesRelation.AddFile( temporary );
[266]627
628 //#requireの場合では、既に読み込まれているファイルは読み込まないようにする
629 bool isFake = false;
630 if( isRequire ){
631 if( requireFiles.IsIncluded( temporary ) ){
632 //既に読み込まれているとき
633 isFake = true;
634 }
635 }
636
637 BasicSource source;
638
639 if( isFake ){
640 //既に読み込まれているときは空データ
641 source.SetBuffer( "" );
642 }
643 else{
644 //取り込まれたファイルを収集する
645 requireFiles.Add( temporary );
646
647 //インクルードファイルを読み込む
648 if( !source.ReadFile_InIncludeDirective( temporary ) ){
649 sprintf(temp2,"インクルードファイル \"%s\" をオープンできません",temporary);
650 SmoothieException::Throw(-1,temp2,i);
651 break;
652 }
653 }
654
655 i3=lstrlen(buffer)+source.GetLength();
656 Realloc( i3 );
657 Text::SlideString(buffer+i2,source.GetLength()+(i-i2));
658 memcpy(buffer+i,source.GetBuffer(),source.GetLength());
659
660 //新しい参照ディレクトリをセット
661 char temp4[MAX_PATH];
662 _splitpath(temporary,temp2,temp4,0,0);
663 lstrcat(temp2,temp4);
664 LayerDir[layer]=(char *)malloc(lstrlen(temp2)+1);
665 lstrcpy(LayerDir[layer],temp2);
666
667 //ファイル範囲をスライド
668 LastFileByte[layer]=i+source.GetLength()-1;
669 for(i3=0;i3<layer;i3++) LastFileByte[i3]+=source.GetLength()+(i-i2);
670
671 i--;
672 }
673 }
674
675 free(LayerDir[0]);
676
677 length = lstrlen(buffer);
678}
679
680int KillReturnCode_InParameter(char *buffer,int *pRnum,char cBeginPare,char cEndPare){
681 int i,i2,i3,IsStr;
682
683 //カッコ'('直下の改行
684 while(buffer[0]=='\n'){
685 Text::SlideString(buffer+1,-1);
686 (*pRnum)++;
687 }
688
689 for(i=0,IsStr=0;;i++){
690 if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){
691 i++;
692 continue;
693 }
694
695 if(buffer[i]=='\"') IsStr^=1;
696
697 if(buffer[i]=='\0') break; //エラー
698 if(buffer[i]=='\n'){
699 i2=0;
700 i3=0;
701 while(buffer[i+i2]=='\n'){
702 i2++;
703 i3++;
704 while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++;
705 }
706 while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++;
707
708 if(buffer[i+i2]==cEndPare){
709 Text::SlideString(buffer+i+i2,-i2);
710 (*pRnum)+=i3;
711 break;
712 }
713
714 //エラー
715 break;
716 }
717
718 if(buffer[i]==cBeginPare&&IsStr==0){
719 i++;
720 i2=KillReturnCode_InParameter(buffer+i,pRnum,cBeginPare,cEndPare);
721 i+=i2;
722 if(buffer[i]!=cEndPare) break;
723 continue;
724 }
725 if(buffer[i]==cEndPare&&IsStr==0) break;
726
727 if(buffer[i]==','&&buffer[i+1]=='\n'&&IsStr==0){
728 i++;
729 while(buffer[i]=='\n'){
730 Text::SlideString(buffer+i+1,-1);
731 (*pRnum)++;
732 }
733 i--;
734 }
735 }
736 return i;
737}
738void BasicSource::RemoveReturnLineUnderbar(){
739 int i,i2;
740
741 //アンダーバーによる改行
742 for(i=0;;i++){
743 i2=0;
744 while(buffer[i]=='_'&&buffer[i+1]=='\n'){
745 i2++;
746 Text::SlideString(buffer+i+2,-2);
747 while(buffer[i]=='\n'){
748 Text::SlideString(buffer+i+1,-1);
749 i2++;
750 }
751 for(;;i++){
752 if(buffer[i]=='_'&&buffer[i+1]=='\n') break;
753 if(buffer[i]=='\n'||buffer[i]=='\0'){
754 Text::SlideString(buffer+i,i2);
755 memset(buffer+i,'\n',i2);
756 break;
757 }
758 }
759 }
760 if(buffer[i]=='\0') break;
761 }
762
763 //カッコ内パラメータの改行
764 int IsStr,rnum;
765 for(i=0,IsStr=0,rnum=0;;i++){
766 if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){
767 i++;
768 continue;
769 }
770 if(buffer[i]=='\0') break;
771 if(buffer[i]=='\n'){
772 if(rnum){
773 Text::SlideString(buffer+i+1,rnum);
774 memset(buffer+i+1,'\n',rnum);
775 rnum=0;
776 }
777 }
778 if(buffer[i]=='\"') IsStr^=1;
779 if(buffer[i]=='('&&IsStr==0){
780 i++;
781 i2=KillReturnCode_InParameter(buffer+i,&rnum,'(',')');
782 i+=i2;
783 if(buffer[i]!=')') break;
784 }
785 if(buffer[i]=='['&&IsStr==0){
786 i++;
787 i2=KillReturnCode_InParameter(buffer+i,&rnum,'[',']');
788 i+=i2;
789 if(buffer[i]!=']') break;
790 }
791 }
792
793 length = lstrlen(buffer);
794}
795
796void BasicSource::SetBuffer( const char *buffer ){
797 this->buffer = (char *)calloc( lstrlen(buffer) + 1, 1 );
798 lstrcpy( this->buffer, buffer );
799 length = lstrlen(buffer);
800
801 // ダミー改行をセット
802 Realloc( length + 2 );
803 Text::SlideString( this->buffer, 2 );
804 this->buffer[0] = '\n';
805 this->buffer[1] = '\n';
806}
807
808bool BasicSource::ReadFile( const string &filePath ){
809 if( !Text::ReadFile( filePath ) ){
810 return false;
811 }
812
813 // 改行コードをCRLFからLFに変換
814 ChangeReturnLineChar();
815
816 // basic.sbpをインクルード
[290]817 const char *headCode = "#include <basic.sbp>\n";
818 //const char *headCode = "\n";
[266]819 Realloc( length + lstrlen(headCode) );
820 Text::SlideString( buffer, lstrlen(headCode) );
821 memcpy( buffer, headCode, lstrlen(headCode) );
822
823 // #defineと#requireを初期化
824 // TODO: バージョン番号の識別子defineが未完成
825 objDefine.Init(0);
826 requireFiles.clear();
827
828 // コメントを削除
829 RemoveComments();
830
831 // #ifdefディレクティブを処理
832 objDefine.DirectiveIfdef( buffer );
833
834 //最終行には文字を含ませないようにする
835 Realloc( length + 1 );
836 lstrcat( buffer, "\n" );
837
838 // #include / #require ディレクティブを処理
839 DirectiveIncludeOrRequire();
840
841 // アンダーバーによる改行を正規表現に戻す
842 RemoveReturnLineUnderbar();
843
844 // ダミー改行をセット
845 Realloc( length + 2 );
846 Text::SlideString( buffer, 2 );
847 buffer[0] = '\n';
848 buffer[1] = '\n';
849
850 extern char *basbuf;
851 basbuf = GetBuffer();
852
853 return true;
854}
855
856bool BasicSource::Generate( const string &genName, const char *genBuffer ){
857 const int genBufferLength = lstrlen( genBuffer );
858
859#ifdef _DEBUG
860 // 挿入ソースに改行コードが含まれていないかを検査する
861 for( int i=0; genBuffer[i] ; i++ ){
862 if( genBuffer[i] == '\n' ){
863 SetError();
864 break;
865 }
866 }
867#endif
868
869 bool isFound = false;
870
871 for( int i=0; ; i++ ){
872 if( i == 0 || buffer[i] == '\n' ){
873 if( buffer[i] == '\n' ){
874 i++;
875 }
876 while( IsBlank( buffer[i] ) ){
877 i++;
878 }
879
880 int startIndex = i;
881
882 if( memicmp( buffer + i, generateDirectiveName.c_str(), generateDirectiveName.size() ) == 0 ){
883 i += (int)generateDirectiveName.size();
884 while( IsBlank( buffer[i] ) ){
885 i++;
886 }
887
888 char temporary[VN_SIZE];
889 for( int i2=0; ; i++, i2++ ){
890 if( buffer[i] == '\n' ){
891 temporary[i2] = 0;
892 break;
893 }
894 temporary[i2] = buffer[i];
895 }
896 if( genName == temporary ){
897 // 一致
898
899 int endIndex = i;
900
901 int lengthOffset = genBufferLength - ( endIndex - startIndex );
902
903 Realloc( length + lengthOffset );
904 Text::SlideString( buffer + endIndex, lengthOffset );
905 memcpy( buffer + startIndex, genBuffer, genBufferLength );
906
907 isFound = true;
908
909 break;
910 }
911 }
912 }
913 }
914
915 return isFound;
916}
917
918void BasicSource::Addition( const char *buffer ){
919 Realloc( length + lstrlen(buffer) );
920 lstrcat( this->buffer, buffer );
921}
[279]922
[280]923bool BasicSource::GetLineInfo( int sourceCodePos, int &line, std::string &filePath ) const
[279]924{
925 int i2,i3,i4,i5;
926
[280]927 const char *buffer = GetBuffer();
[279]928 int i = sourceCodePos;
929
930 if(buffer[i]=='\n') i--;
931 for(i3=0,i2=0;i3<i;i3++){
932 if(buffer[i3]=='\n') i2++;
933 if(buffer[i3]=='\0') return 0;
934 }
935
936 if( includedFilesRelation.GetLineCounts() < i2 )
937 {
[285]938 //Jenga::Throw( "BasicSource::GetLineInfoメソッドで不正な行の情報を取得しようとした" );
[279]939
940 //ファイル・行番号を特定できなかった場合
941 line = -1;
942 filePath = "";
943 return false;
944 }
945
946 i4=0;
947 while( includedFilesRelation.GetFileNumber( i2 ) != includedFilesRelation.GetFileNumber( i4 ) )
948 {
949 i4++;
950 }
951 for(i3=0,i5=0;i5<i4;i3++){
952 if(buffer[i3]=='\n') i5++;
953 if(buffer[i3]=='\0') return 0;
954 }
955 for(i5=0;i4<i2;i3++){
956 if(buffer[i3]=='\n'){
957 i4++;
958 i5++;
959 if( includedFilesRelation.GetFileNumber( i2 ) < includedFilesRelation.GetFileNumber( i4 ) )
960 {
961 for( ;includedFilesRelation.GetFileNumber( i2 ) != includedFilesRelation.GetFileNumber( i4 ); i3++ ){
962 if(buffer[i3]=='\n') i4++;
963 }
964 }
965 }
966 if(buffer[i3]=='\0') return 0;
967 }
968
969 //行番号をセット
970 line = i5;
971
972 //ファイル名をセット
973 filePath = includedFilesRelation.GetFilePath( i2 );
974
975 return 1;
976}
Note: See TracBrowser for help on using the repository browser.