Changeset 88 in dev for BasicCompiler_Common
- Timestamp:
- Apr 2, 2007, 12:10:02 AM (18 years ago)
- Location:
- BasicCompiler_Common
- Files:
-
- 15 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
BasicCompiler_Common/BasicCompiler.h
r78 r88 83 83 84 84 char *basbuf; 85 char *pBaseBuffer;86 85 87 86 char BasicSystemDir[MAX_PATH]; //BasicCompiler.exeが存在するディレクトリ -
BasicCompiler_Common/DebugMiddleFile.cpp
r87 r88 60 60 61 61 extern char *basbuf; 62 i3=lstrlen(basbuf);63 64 62 BufferSize=lstrlen(basbuf)+65535; 65 63 buffer=(char *)HeapAlloc(hHeap,0,BufferSize); … … 138 136 // TypeDef情報 139 137 ////////////////// 140 *(long *)(buffer+i2)=(int) House::Meta::typeDefs.size();141 i2+=sizeof(long); 142 for(i3=0;i3<(int) House::Meta::typeDefs.size();i3++){143 lstrcpy(buffer+i2, House::Meta::typeDefs[i3].GetNewName().c_str() );144 i2+=lstrlen(buffer+i2)+1; 145 146 lstrcpy(buffer+i2, House::Meta::typeDefs[i3].GetBaseName().c_str() );138 *(long *)(buffer+i2)=(int)Smoothie::Meta::typeDefs.size(); 139 i2+=sizeof(long); 140 for(i3=0;i3<(int)Smoothie::Meta::typeDefs.size();i3++){ 141 lstrcpy(buffer+i2,Smoothie::Meta::typeDefs[i3].GetNewName().c_str() ); 142 i2+=lstrlen(buffer+i2)+1; 143 144 lstrcpy(buffer+i2,Smoothie::Meta::typeDefs[i3].GetBaseName().c_str() ); 147 145 i2+=lstrlen(buffer+i2)+1; 148 146 … … 466 464 //ソースコード 467 465 i2++; 468 pBaseBuffer=(char *)HeapAlloc(hHeap,0,lstrlen(buffer+i2)+3); 469 basbuf=pBaseBuffer+2; 470 lstrcpy(basbuf,buffer+i2); 466 source.SetBuffer( buffer + i2 ); 471 467 i2+=lstrlen(buffer+i2)+1; 472 extern char *basbuf;473 basbuf=this->basbuf;474 468 475 469 //コードと行番号の関係 … … 505 499 506 500 //初期化 507 House::Meta::typeDefs.clear();501 Smoothie::Meta::typeDefs.clear(); 508 502 509 503 //個数を取得 … … 514 508 i2+=lstrlen(buffer+i2)+1; 515 509 516 House::Meta::typeDefs.push_back( TypeDef( temp5, buffer+i2 ) );510 Smoothie::Meta::typeDefs.push_back( TypeDef( temp5, buffer+i2 ) ); 517 511 518 512 i2+=lstrlen(buffer+i2)+1; … … 934 928 935 929 //ソースコード 936 extern char *pBaseBuffer; 937 extern char *basbuf; 938 pBaseBuffer=this->pBaseBuffer; 939 basbuf=this->basbuf; 930 Smoothie::Lexical::source = source; 940 931 941 932 //コードと行番号の関係 … … 991 982 DeleteConstInfo(ppConstHash); 992 983 993 //ソースコードを解放994 HeapDefaultFree(pBaseBuffer);995 996 984 //コードと行番号の関係を解放 997 985 HeapDefaultFree(pLineInfo); -
BasicCompiler_Common/DebugSection.h
r78 r88 25 25 26 26 //ソースコード 27 char *pBaseBuffer; 28 char *basbuf; 27 BasicSource source; 29 28 30 29 //コードと行番号の関係 -
BasicCompiler_Common/Enum.cpp
r75 r88 15 15 16 16 17 CEnumParent::CEnumParent(c har *buffer,int nowLine){17 CEnumParent::CEnumParent(const char *buffer,int nowLine){ 18 18 ppobj_EnumMember=(CEnumMember **)HeapAlloc(hHeap,0,1); 19 19 iEnumMemberNum=0; … … 108 108 iEnumParentNum=0; 109 109 110 extern char *basbuf; 110 const char *source = Smoothie::Lexical::source.GetBuffer(); 111 111 112 int i; 112 113 for(i=0;;i++){ 113 if( basbuf[i]=='\0') break;114 115 if( basbuf[i]==1&&basbuf[i+1]==ESC_ENUM){114 if(source[i]=='\0') break; 115 116 if(source[i]==1&&source[i+1]==ESC_ENUM){ 116 117 if(i>=2){ 117 if( basbuf[i-2]==1&&basbuf[i-1]==ESC_CONST) continue;118 if(source[i-2]==1&&source[i-1]==ESC_CONST) continue; 118 119 } 119 120 ppobj_EnumParent=(CEnumParent **)HeapReAlloc(hHeap,0,ppobj_EnumParent,(iEnumParentNum+1)*sizeof(CEnumParent *)); 120 ppobj_EnumParent[iEnumParentNum]=new CEnumParent( basbuf+i,i);121 ppobj_EnumParent[iEnumParentNum]=new CEnumParent(source+i,i); 121 122 iEnumParentNum++; 122 123 } -
BasicCompiler_Common/Enum.h
r75 r88 17 17 public: 18 18 19 CEnumParent(c har *buffer,int nowLine);19 CEnumParent(const char *buffer,int nowLine); 20 20 ~CEnumParent(); 21 21 -
BasicCompiler_Common/Intermediate_Step1.cpp
r79 r88 78 78 GlobalFree(temporary); 79 79 } 80 /* 81 TODO: 消す 80 82 int KillReturnCode_InParameter(char *buffer,int *pRnum,char cBeginPare,char cEndPare){ 81 83 int i,i2,i3,IsStr; … … 190 192 } 191 193 } 192 } 194 }*/ 193 195 void CheckParenthesis(char *buffer){ 194 196 int i,IsStr,PareNum,sw; -
BasicCompiler_Common/MakeExe.cpp
r78 r88 45 45 ChangeCommandToCode(temp); 46 46 47 //新しいソースコードバッファの容量48 extern char *basbuf;49 int NewSize;50 NewSize=lstrlen(basbuf)+lstrlen(temp);51 NewSize*=2;52 NewSize+=255;53 54 47 //最後尾に貼り付け 55 extern char *pBaseBuffer; 56 pBaseBuffer=(char *)HeapReAlloc(hHeap,0,pBaseBuffer,NewSize); 57 basbuf=pBaseBuffer+2; 58 lstrcat(basbuf,temp); 48 Smoothie::Lexical::source.Addition( temp ); 59 49 60 50 HeapDefaultFree(temp); … … 64 54 extern HANDLE hHeap; 65 55 extern char *basbuf; 66 extern char *pBaseBuffer;67 56 extern int ErrorNum; 68 57 extern BOOL bStopCompile; … … 87 76 //プログラムをファイルから読み込む 88 77 extern char SourceFileName[MAX_PATH]; 89 pBaseBuffer=OpenBasicFile(SourceFileName); 90 if(!pBaseBuffer){ 78 if( !Smoothie::Lexical::source.ReadFile( SourceFileName ) ){ 91 79 SetError(201,SourceFileName,-1); 92 80 goto EndCompile; 93 81 } 94 basbuf=pBaseBuffer+2;95 82 96 83 //イメージベースの設定 … … 111 98 CompileMessage(STRING_COMPILE_OPTIMIZING); 112 99 100 /* 101 TODO: 消す 102 BasicSource::ReadFile内で処理されている 103 113 104 //アンダーバーによる改行を正規表現に戻す 114 105 KillReturnCode(basbuf); 106 */ 115 107 116 108 //カッコを相互チェック(ダブルクォートチェックチェックを含む) … … 263 255 HeapDefaultFree(IncludeFileInfo.ppFileNames[i2]); 264 256 HeapDefaultFree(IncludeFileInfo.ppFileNames); 265 266 if(pBaseBuffer) HeapDefaultFree(pBaseBuffer);267 257 } 268 258 int MainThread(DWORD dummy){ -
BasicCompiler_Common/Subroutine.cpp
r87 r88 180 180 GetVarType(fullCallName,type,false); 181 181 182 ProcPointer *pProcPtr = House::Meta::procPointers[type.GetIndex()];182 ProcPointer *pProcPtr = Smoothie::Meta::procPointers[type.GetIndex()]; 183 183 resultType = pProcPtr->ReturnType(); 184 184 … … 781 781 pProcPointer->SetParamsAndReturnType( paramStr.c_str(), nowLine ); 782 782 783 House::Meta::procPointers.push_back( pProcPointer );784 785 return (int) House::Meta::procPointers.size()-1;783 Smoothie::Meta::procPointers.push_back( pProcPointer ); 784 785 return (int)Smoothie::Meta::procPointers.size()-1; 786 786 } 787 787 void DeleteProcPtrInfo(void){ 788 BOOST_FOREACH( ProcPointer *pProcPointer, House::Meta::procPointers ){788 BOOST_FOREACH( ProcPointer *pProcPointer, Smoothie::Meta::procPointers ){ 789 789 delete pProcPointer; 790 790 } 791 791 792 House::Meta::procPointers.clear();793 } 792 Smoothie::Meta::procPointers.clear(); 793 } -
BasicCompiler_Common/Type.cpp
r87 r88 89 89 // TypeDefされた型 90 90 //////////////////// 91 int i= House::Meta::typeDefs.GetIndex( typeName.c_str() );91 int i=Smoothie::Meta::typeDefs.GetIndex( typeName.c_str() ); 92 92 if(i!=-1){ 93 type = House::Meta::typeDefs[i].GetBaseType();93 type = Smoothie::Meta::typeDefs[i].GetBaseType(); 94 94 return true; 95 95 } … … 463 463 } 464 464 else{ 465 if( House::Meta::procPointers[index]->ReturnType().IsNull() ){465 if( Smoothie::Meta::procPointers[index]->ReturnType().IsNull() ){ 466 466 return "*Sub"; 467 467 } -
BasicCompiler_Common/TypeDef.cpp
r87 r88 126 126 cp = nowLine; 127 127 128 House::Meta::typeDefs.Add(temporary,pTemp);128 Smoothie::Meta::typeDefs.Add(temporary,pTemp); 129 129 } 130 130 … … 164 164 if(basbuf[i]=='\0') break; 165 165 } 166 House::Meta::typeDefs.Add(temporary,"Long");166 Smoothie::Meta::typeDefs.Add(temporary,"Long"); 167 167 } 168 168 } -
BasicCompiler_Common/VariableOpe.cpp
r87 r88 220 220 if(lpIndex==-1) lstrcpy(name,"VoidPtr"); 221 221 else{ 222 if( House::Meta::procPointers[lpIndex]->ReturnType().IsNull() )222 if( Smoothie::Meta::procPointers[lpIndex]->ReturnType().IsNull() ) 223 223 lstrcpy(name,"*Sub"); 224 224 else lstrcpy(name,"*Function"); -
BasicCompiler_Common/common.h
r85 r88 163 163 164 164 // コンパイラが必要とするデータハウス 165 #include < House.h>165 #include <Smoothie.h> 166 166 167 167 -
BasicCompiler_Common/include/Smoothie.h
r87 r88 4 4 #include "../TypeDef.h" 5 5 6 class House{6 class Smoothie{ 7 7 public: 8 8 -
BasicCompiler_Common/include/Source.h
r87 r88 16 16 public: 17 17 18 enum ReturnLineChar{19 CR,20 LF,21 CRLF,22 };23 24 18 Text(){ 25 19 buffer = (char *)calloc( 1, 1 ); … … 31 25 32 26 bool ReadFile( const string &filePath ); 33 34 void ChangeReturnLineChar();35 36 char *Buffer(){37 return buffer;38 }39 int Length(){40 return length;41 }42 27 }; 43 28 44 29 class BasicSource : public Text 45 30 { 31 void Realloc( int newLength ){ 32 buffer = (char *)realloc( buffer, newLength + 255 ); 33 34 length = newLength; 35 36 extern char *basbuf; 37 basbuf = buffer + 2; 38 } 39 40 void IncludeFiles(); 41 42 void ChangeReturnLineChar(); 43 44 void RemoveComments(); 45 46 bool ReadFile_InIncludeDirective( const string &filePath ); 47 void DirectiveIncludeOrRequire(); 48 49 void RemoveReturnLineUnderbar(); 50 46 51 public: 47 52 BasicSource(){} 48 53 ~BasicSource(){} 49 54 50 void RemoveComments(); 55 char *GetBuffer(){ 56 return buffer+2; 57 } 58 int GetLength(){ 59 return length-2; 60 } 51 61 52 void DirectiveIfdef(); 53 void DirectiveIncludeOrRequire(); 62 void SetBuffer( const char *buffer ); 54 63 55 void RemoveReturnLineUnderbar(); 56 57 void FormatDefStatement(); 58 void FormatIfStatement(); 64 bool ReadFile( const string &filePath ); 59 65 60 66 bool Generate( const string &genName, const char *buffer ); 67 68 void Addition( const char *buffer ); 69 70 void operator = ( const BasicSource &source ){ 71 Realloc( source.length ); 72 lstrcpy( buffer, source.buffer ); 73 } 61 74 }; -
BasicCompiler_Common/preprocessor.cpp
r76 r88 7 7 #endif 8 8 9 9 /* 10 10 11 11 ////////////////////////////////////// … … 616 616 return base; 617 617 } 618 */ -
BasicCompiler_Common/src/Smoothie.cpp
r87 r88 1 1 #include <..\common.h> 2 2 3 TypeDefCollection House::Meta::typeDefs; 4 vector<ProcPointer *> House::Meta::procPointers; 3 BasicSource Smoothie::Lexical::source; 4 5 TypeDefCollection Smoothie::Meta::typeDefs; 6 vector<ProcPointer *> Smoothie::Meta::procPointers; -
BasicCompiler_Common/src/Source.cpp
r87 r88 1 1 #include <Source.h> 2 2 3 #include "../common.h" 4 #include <windows.h> 5 6 7 8 class CDefine{ 9 vector<string> names; 10 public: 11 CDefine(); 12 ~CDefine(); 13 void Free(); 14 void Init(); 15 16 BOOL add(char *name); 17 BOOL undef(char *name); 18 BOOL check(char *name); 19 void preprocessor_ifdef(char *buffer,bool isNot); 20 void DirectiveIfdef(char *buffer); 21 }; 22 CDefine objDefine; 23 24 25 ////////////////////////////////////// 26 // #requireの管理 27 ////////////////////////////////////// 28 class CRequireFiles{ 29 char **ppFilePath; 30 int count; 31 public: 32 CRequireFiles(){ 33 ppFilePath = (char **)malloc( 1 ); 34 count = 0; 35 } 36 ~CRequireFiles(){ 37 for( int i = 0; i < count; i++ ){ 38 free( ppFilePath[i] ); 39 } 40 free( ppFilePath ); 41 } 42 void clear(){ 43 for( int i = 0; i < count; i++ ){ 44 free( ppFilePath[i] ); 45 } 46 free( ppFilePath ); 47 48 ppFilePath = (char **)malloc( 1 ); 49 count = 0; 50 } 51 bool IsIncluded( const char *includeFilePath ){ 52 // '/' → '\\' 53 char tempPath[MAX_PATH]; 54 lstrcpy( tempPath, includeFilePath ); 55 for( int i=0; tempPath[i]; i++ ){ 56 if( tempPath[i] == '/' ){ 57 tempPath[i] = '\\'; 58 } 59 } 60 61 for( int i=0; i<count; i++ ){ 62 if( lstrcmpi( ppFilePath[i], tempPath ) == 0 ){ 63 return true; 64 } 65 } 66 return false; 67 } 68 void Add( const char *includeFilePath ){ 69 // '/' → '\\' 70 char tempPath[MAX_PATH]; 71 lstrcpy( tempPath, includeFilePath ); 72 for( int i=0; tempPath[i]; i++ ){ 73 if( tempPath[i] == '/' ){ 74 tempPath[i] = '\\'; 75 } 76 } 77 78 //既に読み込まれているとき 79 if( IsIncluded( tempPath ) ) return; 80 81 //追加 82 ppFilePath = (char **)realloc(ppFilePath, ( count + 1 ) * sizeof(char *) ); 83 ppFilePath[count] = (char *)malloc( lstrlen(tempPath) + 1 ); 84 lstrcpy( ppFilePath[count], tempPath ); 85 count++; 86 } 87 }; 88 CRequireFiles requireFiles; 89 90 91 ////////////////////////////////////// 92 // #define間するクラス 93 ////////////////////////////////////// 94 95 CDefine::CDefine(){ 96 Init(); 97 } 98 CDefine::~CDefine(){ 99 } 100 void CDefine::Init(){ 101 names.clear(); 102 103 extern BOOL bDebugCompile; 104 if(bDebugCompile) add("_DEBUG"); 105 106 #ifdef _AMD64_ 107 add("_WIN64"); 108 #endif 109 110 extern BOOL bDll; 111 if( bDll ){ 112 add("_DLL"); 113 } 114 115 extern bool isUnicode; 116 if( isUnicode ){ 117 add( "UNICODE" ); 118 } 119 120 char temporary[255]; 121 sprintf(temporary,"_AB_VER%d",MAJOR_VER); 122 add(temporary); 123 } 124 BOOL CDefine::add(char *name){ 125 extern HANDLE hHeap; 126 127 //重複チェック 128 if(check(name)) return 0; 129 130 //追加 131 names.push_back( name ); 132 133 return 1; 134 } 135 BOOL CDefine::undef(char *name){ 136 vector<string>::iterator i = names.begin(); 137 foreach( const string &temp, names ){ 138 if( temp == name ){ 139 names.erase( i ); 140 return 1; 141 } 142 i++; 143 } 144 145 return 0; 146 } 147 BOOL CDefine::check(char *name){ 148 149 //重複チェック 150 foreach( const string &temp, names ){ 151 if( temp == name ){ 152 return 1; 153 } 154 } 155 return 0; 156 } 157 158 int Search_endif(char *buffer,int i, int *pLine = 0){ 159 for(;;i++){ 160 if(buffer[i]=='\0') break; 161 162 if( buffer[i] == '\n' ){ 163 if( pLine ){ 164 (*pLine)++; 165 } 166 } 167 168 if(buffer[i-1]=='\n'){ 169 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){ 170 i=Search_endif(buffer,i+6, pLine); 171 if(buffer[i]=='\0') break; 172 continue; 173 } 174 else if(memicmp(buffer+i,"#endif",6)==0){ 175 break; 176 } 177 } 178 } 179 return i; 180 } 181 182 void CDefine::preprocessor_ifdef(char *buffer,bool isNot){ 183 int i,i2,i3; 184 char temporary[VN_SIZE]; 185 186 if(isNot) i=lstrlen("#ifndef"); 187 else i=lstrlen("#ifdef"); 188 while(buffer[i]==' '||buffer[i]=='\t') i++; 189 190 for(i2=0;;i++,i2++){ 191 if(buffer[i]=='\n'||buffer[i]=='\0'){ 192 temporary[i2]=0; 193 break; 194 } 195 temporary[i2]=buffer[i]; 196 } 197 198 int sw=0; 199 if(check(temporary)) sw=1; 200 201 if(isNot){ 202 //#ifndefのとき(反対にする) 203 if(sw) sw=0; 204 else sw=1; 205 } 206 207 //#ifdefの行を消去 208 SlideString(buffer+i,-i); 209 i=0; 210 211 BOOL bElse=0; 212 if(sw){ 213 //TRUEのとき 214 215 //#else、#endifを探索 216 for(;;i++){ 217 if(buffer[i]=='\0') break; 218 219 if(i==0||buffer[i-1]=='\n'){ 220 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){ 221 i=Search_endif(buffer,i+6); 222 if(buffer[i]=='\0') break; 223 continue; 224 } 225 else if(memicmp(buffer+i,"#else",5)==0){ 226 i2=5; 227 bElse=1; 228 break; 229 } 230 else if(memicmp(buffer+i,"#endif",6)==0){ 231 i2=6; 232 bElse=0; 233 break; 234 } 235 } 236 } 237 238 //行を消去 239 SlideString(buffer+i+i2,-i2); 240 241 if(bElse){ 242 //#elseがある場合はその区間を消去 243 244 for(i2=i,i3=0;;i2++){ 245 if(buffer[i2]=='\0') break; 246 247 if(buffer[i2]=='\n') i3++; 248 249 if(i2==0||buffer[i2-1]=='\n'){ 250 if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){ 251 i2=Search_endif(buffer,i2+6, &i3 ); 252 if(buffer[i2]=='\0') break; 253 continue; 254 } 255 if(memicmp(buffer+i2,"#endif",6)==0){ 256 i2+=6; 257 break; 258 } 259 } 260 } 261 262 //ソースコード区間を消去し、改行コードを挿入 263 SlideString(buffer+i2,i-i2+i3); 264 memset(buffer+i,'\n',i3); 265 } 266 } 267 else{ 268 //FALSEのとき 269 270 //#else、#endifを探索 271 for(i2=i,i3=0;;i2++){ 272 if(buffer[i2]=='\0') break; 273 274 if(buffer[i2]=='\n') i3++; 275 276 if(i2==0||buffer[i2-1]=='\n'){ 277 if(memicmp(buffer+i2,"#ifdef",6)==0||memicmp(buffer+i2,"#ifndef",7)==0){ 278 i2=Search_endif(buffer,i2+6, &i3 ); 279 if(buffer[i2]=='\0') break; 280 continue; 281 } 282 else if(memicmp(buffer+i2,"#else",5)==0){ 283 i2+=5; 284 bElse=1; 285 break; 286 } 287 else if(memicmp(buffer+i2,"#endif",6)==0){ 288 i2+=6; 289 bElse=0; 290 break; 291 } 292 } 293 } 294 295 //ソースコード区間を消去し、改行コードを挿入 296 SlideString(buffer+i2,i-i2+i3); 297 memset(buffer+i,'\n',i3); 298 299 if(bElse){ 300 //#endifを探索 301 for(;;i++){ 302 if(buffer[i]=='\0') break; 303 304 if(i==0||buffer[i-1]=='\n'){ 305 if(memicmp(buffer+i,"#ifdef",6)==0||memicmp(buffer+i,"#ifndef",7)==0){ 306 i=Search_endif(buffer,i+6); 307 if(buffer[i]=='\0') break; 308 continue; 309 } 310 else if(memicmp(buffer+i,"#endif",6)==0){ 311 i2=6; 312 bElse=0; 313 break; 314 } 315 } 316 } 317 318 //行を消去 319 SlideString(buffer+i+i2,-i2); 320 } 321 } 322 } 323 324 325 void CDefine::DirectiveIfdef(char *buffer){ 326 int i,i2,i3,sw; 327 char temporary[VN_SIZE]; 328 329 for(i=0;;i++){ 330 if(buffer[i]=='\0') break; 331 332 if(i==0||(i>=1&&buffer[i-1]=='\n')){ 333 sw=0; 334 if(memicmp(buffer+i,"#define",7)==0){ 335 i2=i+7; 336 while(buffer[i2]==' '||buffer[i2]=='\t') i2++; 337 338 for(i3=0;;i2++,i3++){ 339 if(buffer[i2]=='\n'||buffer[i2]=='\0'){ 340 temporary[i3]=0; 341 break; 342 } 343 temporary[i3]=buffer[i2]; 344 } 345 346 add(temporary); 347 348 i2-=i; 349 350 //ディレクティブを消去 351 SlideString(buffer+i+i2,-i2); 352 } 353 if(memicmp(buffer+i,"#undef",6)==0){ 354 i2=i+7; 355 while(buffer[i2]==' '||buffer[i2]=='\t') i2++; 356 357 for(i3=0;;i2++,i3++){ 358 if(buffer[i2]=='\n'||buffer[i2]=='\0'){ 359 temporary[i3]=0; 360 break; 361 } 362 temporary[i3]=buffer[i2]; 363 } 364 365 undef(temporary); 366 367 i2-=i; 368 369 //ディレクティブを消去 370 SlideString(buffer+i+i2,-i2); 371 } 372 else if(memicmp(buffer+i,"#ifdef",6)==0){ 373 preprocessor_ifdef(buffer+i,false); 374 continue; 375 } 376 else if(memicmp(buffer+i,"#ifndef",7)==0){ 377 preprocessor_ifdef(buffer+i,true); 378 continue; 379 } 380 else continue; 381 } 382 } 383 } 384 385 386 387 3 388 bool Text::ReadFile( const string &filePath ){ 389 //ファイルオープン 390 HANDLE hFile=CreateFile(filePath.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); 391 if(hFile==INVALID_HANDLE_VALUE){ 392 return false; 393 } 394 395 length = GetFileSize( hFile, NULL ); 396 397 buffer = (char *)realloc( buffer, length + 1 ); 398 399 //読み込み 400 DWORD dwAccBytes; 401 ::ReadFile(hFile,buffer,length,&dwAccBytes,0); 402 buffer[dwAccBytes]=0; 403 404 //ファイルクローズ 405 CloseHandle(hFile); 406 4 407 return true; 5 408 } 6 409 7 void Text::ChangeReturnLineChar(){ 8 } 9 410 void BasicSource::ChangeReturnLineChar(){ 411 #ifdef _DEBUG 412 //改行コードの整合性チェック 413 for( int i=0; ; i++ ){ 414 if( buffer[i] == '\0' ){ 415 break; 416 } 417 if( buffer[i]!='\r' && buffer[i+1]=='\n' 418 || buffer[i]=='\r' && buffer[i+1]!='\n' ){ 419 char temporary[255]; 420 strncpy( temporary, buffer + i-100, 130 ); 421 temporary[130] = 0; 422 for(int i2=0; ;i2++){ 423 if(temporary[i2]=='\r') temporary[i2]='A'; 424 if(temporary[i2]=='\n') temporary[i2]='B'; 425 if(temporary[i2]=='\0') break; 426 } 427 428 extern HWND hOwnerEditor; 429 MessageBox( hOwnerEditor, temporary, "改行コードの整合性チェック", MB_OK | MB_ICONEXCLAMATION ); 430 } 431 } 432 #endif 433 434 //改行コードのCRLFをLFに変換 435 int i,i2; 436 for(i=0,i2=0;;i++,i2++){ 437 if(buffer[i]=='\r'&&buffer[i+1]=='\n') i++; 438 buffer[i2]=buffer[i]; 439 if(buffer[i]=='\0') break; 440 } 441 442 length = i; 443 } 10 444 11 445 void BasicSource::RemoveComments(){ 12 } 13 14 void BasicSource::DirectiveIfdef(){ 15 } 446 int i,i2,i3,IsStr; 447 char *temporary; 448 temporary=(char *)GlobalAlloc(GMEM_FIXED,lstrlen(buffer)+1); 449 for(i=0,i2=0,i3=0,IsStr=0;;i++,i2++){ 450 if(buffer[i]=='\"') IsStr^=1; 451 if(buffer[i]=='\n'||buffer[i]=='\0'){ 452 i2--; 453 while(temporary[i2]==' '||temporary[i2]=='\t') i2--; 454 i2++; 455 456 if(i3){ 457 //複数行に渡る注釈文の中に改行が存在するとき 458 memset(temporary+i2,'\n',i3); 459 i2+=i3; 460 i3=0; 461 } 462 } 463 if(buffer[i]=='\''&&IsStr==0){ 464 //注釈文 465 i2--; 466 while(temporary[i2]==' '||temporary[i2]=='\t') i2--; 467 i2++; 468 while(buffer[i]!='\n'&&buffer[i]!='\0') i++; 469 } 470 if(buffer[i]=='/'&&buffer[i+1]=='*'&&IsStr==0){ 471 //注釈文(複数行) 472 i+=2; 473 i3=0; 474 while(!(buffer[i]=='*'&&buffer[i+1]=='/')){ 475 if(buffer[i]=='\n') i3++; 476 if(buffer[i]=='\0') break; 477 i++; 478 } 479 if(buffer[i]){ 480 i+=2; 481 } 482 i--; 483 i2--; 484 continue; 485 } 486 temporary[i2]=buffer[i]; 487 if(buffer[i]=='\0') break; 488 } 489 lstrcpy(buffer,temporary); 490 GlobalFree(temporary); 491 } 492 493 bool BasicSource::ReadFile_InIncludeDirective( const string &filePath ){ 494 if( !Text::ReadFile( filePath ) ){ 495 return false; 496 } 497 498 // 改行コードをCRLFからLFに変換 499 ChangeReturnLineChar(); 500 501 // コメントを削除 502 RemoveComments(); 503 504 // #ifdefディレクティブを処理 505 objDefine.DirectiveIfdef( buffer ); 506 507 // アンダーバーによる改行を正規表現に戻す 508 RemoveReturnLineUnderbar(); 509 510 // ダミー改行をセット 511 Realloc( length + 2 ); 512 SlideString( buffer, 2 ); 513 buffer[0] = '\n'; 514 buffer[1] = '\n'; 515 516 return true; 517 } 518 16 519 void BasicSource::DirectiveIncludeOrRequire(){ 17 } 18 520 extern HANDLE hHeap; 521 extern char szIncludeDir[MAX_PATH]; 522 extern char BasicCurDir[MAX_PATH]; 523 extern INCLUDEFILEINFO IncludeFileInfo; 524 int i,i2,i3,sw1,LineNum,FileLayer[255],layer,LastFileByte[255]; 525 char temporary[MAX_PATH],temp2[MAX_PATH+255],*LayerDir[255]; 526 527 IncludeFileInfo.ppFileNames=(char **)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(char *)); 528 extern char SourceFileName[MAX_PATH]; 529 IncludeFileInfo.ppFileNames[0]=(char *)HeapAlloc(hHeap,0,lstrlen(SourceFileName)+1); 530 lstrcpy(IncludeFileInfo.ppFileNames[0],SourceFileName); 531 IncludeFileInfo.FilesNum=1; 532 533 layer=0; 534 FileLayer[layer]=0; 535 LastFileByte[layer]=GetLength(); 536 LineNum=0; 537 538 //参照ディレクトリ 539 LayerDir[0]=(char *)HeapAlloc(hHeap,0,lstrlen(BasicCurDir)+1); 540 lstrcpy(LayerDir[0],BasicCurDir); 541 542 for(i=0;;i++){ 543 if(buffer[i]=='\0'){ 544 IncludeFileInfo.LineOfFile[LineNum]=-1; 545 break; 546 } 547 if(buffer[i]=='\n'){ 548 IncludeFileInfo.LineOfFile[LineNum]=FileLayer[layer]; 549 LineNum++; 550 } 551 if(i>LastFileByte[layer]){ 552 HeapDefaultFree(LayerDir[layer]); 553 layer--; 554 } 555 if((buffer[i-1]=='\n'||i==0)&&buffer[i]=='#'){ 556 bool isRequire = false; 557 if(memcmp( buffer + i + 1, "include", 7 ) == 0 558 || memcmp( buffer + i + 1, "require", 7 ) == 0){ 559 560 //#requireの場合 561 if( buffer[i + 1] == 'r' ) isRequire = true; 562 563 i2=i+8; 564 while(buffer[i2]==' '||buffer[i2]=='\t') i2++; 565 566 if(buffer[i2]=='\"') sw1=0; 567 else if(buffer[i2]=='<') sw1=1; 568 i2++; 569 570 for(i3=0;;i2++,i3++){ 571 if((buffer[i2]=='\"'&&sw1==0)||(buffer[i2]=='>'&&sw1==1)||buffer[i2]=='\n'||buffer[i2]=='\0'){ 572 temporary[i3]=0; 573 break; 574 } 575 temporary[i3]=buffer[i2]; 576 } 577 while(buffer[i2]!='\n'&&buffer[i2]!='\0') i2++; 578 579 if(sw1){ 580 sprintf(temp2,"%s%s",szIncludeDir,temporary); 581 lstrcpy(temporary,temp2); 582 } 583 else GetFullPath(temporary,LayerDir[layer]); 584 } 585 else if(memcmp(buffer+i+1,"prompt",6)==0){ 586 i2=i+7; 587 sprintf(temporary,"%sbasic\\prompt.sbp",szIncludeDir); 588 } 589 else if(memcmp(buffer+i+1,"N88BASIC",8)==0){ 590 i2=i+9; 591 sprintf(temporary,"%sbasic\\prompt.sbp",szIncludeDir); 592 } 593 else if(memcmp(buffer+i+1,"console",7)==0){ 594 //サブシステム タイプをCUIに変更 595 extern unsigned short TypeOfSubSystem; 596 TypeOfSubSystem=IMAGE_SUBSYSTEM_WINDOWS_CUI; 597 598 i2=i+8; 599 sprintf(temporary,"%sbasic\\dos_console.sbp",szIncludeDir); 600 } 601 else continue; 602 603 if(i){ 604 //ディレクティブが消えるため、一行減ってしまうのを防ぐ(basic.sbpを除く) 605 SlideString(buffer+i2,1); 606 buffer[i2]='\n'; 607 for(i3=0;i3<=layer;i3++) LastFileByte[i3]++; 608 } 609 610 IncludeFileInfo.ppFileNames=(char **)HeapReAlloc(hHeap,0,IncludeFileInfo.ppFileNames,(IncludeFileInfo.FilesNum+1)*sizeof(char *)); 611 IncludeFileInfo.ppFileNames[IncludeFileInfo.FilesNum]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1); 612 lstrcpy(IncludeFileInfo.ppFileNames[IncludeFileInfo.FilesNum],temporary); 613 614 layer++; 615 FileLayer[layer]=IncludeFileInfo.FilesNum; 616 IncludeFileInfo.FilesNum++; 617 618 //#requireの場合では、既に読み込まれているファイルは読み込まないようにする 619 bool isFake = false; 620 if( isRequire ){ 621 if( requireFiles.IsIncluded( temporary ) ){ 622 //既に読み込まれているとき 623 isFake = true; 624 } 625 } 626 627 BasicSource source; 628 629 if( isFake ){ 630 //既に読み込まれているときは空データ 631 source.SetBuffer( "" ); 632 } 633 else{ 634 //取り込まれたファイルを収集する 635 requireFiles.Add( temporary ); 636 637 //インクルードファイルを読み込む 638 if( !source.ReadFile_InIncludeDirective( temporary ) ){ 639 sprintf(temp2,"インクルードファイル \"%s\" をオープンできません",temporary); 640 SetError(-1,temp2,i); 641 break; 642 } 643 } 644 645 i3=lstrlen(buffer)+source.GetLength(); 646 Realloc( i3 ); 647 SlideString(buffer+i2,source.GetLength()+(i-i2)); 648 memcpy(buffer+i,source.GetBuffer(),source.GetLength()); 649 650 //新しい参照ディレクトリをセット 651 char temp4[MAX_PATH]; 652 _splitpath(temporary,temp2,temp4,0,0); 653 lstrcat(temp2,temp4); 654 LayerDir[layer]=(char *)HeapAlloc(hHeap,0,lstrlen(temp2)+1); 655 lstrcpy(LayerDir[layer],temp2); 656 657 //ファイル範囲をスライド 658 LastFileByte[layer]=i+source.GetLength()-1; 659 for(i3=0;i3<layer;i3++) LastFileByte[i3]+=source.GetLength()+(i-i2); 660 661 i--; 662 } 663 } 664 665 HeapDefaultFree(LayerDir[0]); 666 667 length = lstrlen(buffer); 668 } 669 670 int KillReturnCode_InParameter(char *buffer,int *pRnum,char cBeginPare,char cEndPare){ 671 int i,i2,i3,IsStr; 672 673 //カッコ'('直下の改行 674 while(buffer[0]=='\n'){ 675 SlideString(buffer+1,-1); 676 (*pRnum)++; 677 } 678 679 for(i=0,IsStr=0;;i++){ 680 if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){ 681 i++; 682 continue; 683 } 684 685 if(buffer[i]=='\"') IsStr^=1; 686 687 if(buffer[i]=='\0') break; //エラー 688 if(buffer[i]=='\n'){ 689 i2=0; 690 i3=0; 691 while(buffer[i+i2]=='\n'){ 692 i2++; 693 i3++; 694 while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++; 695 } 696 while(buffer[i+i2]==' '||buffer[i+i2]=='\t') i2++; 697 698 if(buffer[i+i2]==cEndPare){ 699 SlideString(buffer+i+i2,-i2); 700 (*pRnum)+=i3; 701 break; 702 } 703 704 //エラー 705 break; 706 } 707 708 if(buffer[i]==cBeginPare&&IsStr==0){ 709 i++; 710 i2=KillReturnCode_InParameter(buffer+i,pRnum,cBeginPare,cEndPare); 711 i+=i2; 712 if(buffer[i]!=cEndPare) break; 713 continue; 714 } 715 if(buffer[i]==cEndPare&&IsStr==0) break; 716 717 if(buffer[i]==','&&buffer[i+1]=='\n'&&IsStr==0){ 718 i++; 719 while(buffer[i]=='\n'){ 720 SlideString(buffer+i+1,-1); 721 (*pRnum)++; 722 } 723 i--; 724 } 725 } 726 return i; 727 } 19 728 void BasicSource::RemoveReturnLineUnderbar(){ 20 } 21 22 void BasicSource::FormatDefStatement(){ 23 } 24 void BasicSource::FormatIfStatement(){ 729 int i,i2; 730 731 //アンダーバーによる改行 732 for(i=0;;i++){ 733 i2=0; 734 while(buffer[i]=='_'&&buffer[i+1]=='\n'){ 735 i2++; 736 SlideString(buffer+i+2,-2); 737 while(buffer[i]=='\n'){ 738 SlideString(buffer+i+1,-1); 739 i2++; 740 } 741 for(;;i++){ 742 if(buffer[i]=='_'&&buffer[i+1]=='\n') break; 743 if(buffer[i]=='\n'||buffer[i]=='\0'){ 744 SlideString(buffer+i,i2); 745 memset(buffer+i,'\n',i2); 746 break; 747 } 748 } 749 } 750 if(buffer[i]=='\0') break; 751 } 752 753 //カッコ内パラメータの改行 754 int IsStr,rnum; 755 for(i=0,IsStr=0,rnum=0;;i++){ 756 if(IsDBCSLeadByte(buffer[i])&&buffer[i+1]){ 757 i++; 758 continue; 759 } 760 if(buffer[i]=='\0') break; 761 if(buffer[i]=='\n'){ 762 if(rnum){ 763 SlideString(buffer+i+1,rnum); 764 memset(buffer+i+1,'\n',rnum); 765 rnum=0; 766 } 767 } 768 if(buffer[i]=='\"') IsStr^=1; 769 if(buffer[i]=='('&&IsStr==0){ 770 i++; 771 i2=KillReturnCode_InParameter(buffer+i,&rnum,'(',')'); 772 i+=i2; 773 if(buffer[i]!=')') break; 774 } 775 if(buffer[i]=='['&&IsStr==0){ 776 i++; 777 i2=KillReturnCode_InParameter(buffer+i,&rnum,'[',']'); 778 i+=i2; 779 if(buffer[i]!=']') break; 780 } 781 } 782 783 length = lstrlen(buffer); 784 } 785 786 void BasicSource::SetBuffer( const char *buffer ){ 787 this->buffer = (char *)calloc( lstrlen(buffer) + 1, 1 ); 788 lstrcpy( this->buffer, buffer ); 789 length = lstrlen(buffer); 790 791 // ダミー改行をセット 792 Realloc( length + 2 ); 793 SlideString( this->buffer, 2 ); 794 this->buffer[0] = '\n'; 795 this->buffer[1] = '\n'; 796 } 797 798 bool BasicSource::ReadFile( const string &filePath ){ 799 if( !Text::ReadFile( filePath ) ){ 800 return false; 801 } 802 803 // 改行コードをCRLFからLFに変換 804 ChangeReturnLineChar(); 805 806 // basic.sbpをインクルード 807 const char *headCode = "#include <basic.sbp>\n"; 808 Realloc( length + lstrlen(headCode) ); 809 SlideString( buffer, lstrlen(headCode) ); 810 memcpy( buffer, headCode, lstrlen(headCode) ); 811 812 // #defineと#requireを初期化 813 objDefine.Init(); 814 requireFiles.clear(); 815 816 // コメントを削除 817 RemoveComments(); 818 819 // #ifdefディレクティブを処理 820 objDefine.DirectiveIfdef( buffer ); 821 822 //最終行には文字を含ませないようにする 823 Realloc( length + 1 ); 824 lstrcat( buffer, "\n" ); 825 826 // #include / #require ディレクティブを処理 827 DirectiveIncludeOrRequire(); 828 829 // アンダーバーによる改行を正規表現に戻す 830 RemoveReturnLineUnderbar(); 831 832 // ダミー改行をセット 833 Realloc( length + 2 ); 834 SlideString( buffer, 2 ); 835 buffer[0] = '\n'; 836 buffer[1] = '\n'; 837 838 extern char *basbuf; 839 basbuf = GetBuffer(); 840 841 return true; 25 842 } 26 843 … … 28 845 return true; 29 846 } 847 848 void BasicSource::Addition( const char *buffer ){ 849 Realloc( length + lstrlen(buffer) ); 850 lstrcat( this->buffer, buffer ); 851 }
Note:
See TracChangeset
for help on using the changeset viewer.