Changeset 206 in dev for trunk/abdev/BasicCompiler_Common/src/Const.cpp
- Timestamp:
- Jul 12, 2007, 2:58:26 AM (17 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/abdev/BasicCompiler_Common/src/Const.cpp
r201 r206 1 #include "stdafx.h" 2 1 3 #include <Compiler.h> 2 #include <NamespaceSupporter.h> 3 4 #include "common.h" 5 #include OPCODE_H_PATH //opcode.h 6 7 8 //シングルトンオブジェクト 9 CDBConst CDBConst::obj; 10 11 12 bool ConstBase::IsEqualSymbol( const NamespaceScopes &namespaceScopes, const string &name ) const 13 { 14 if( GetName() != name ){ 15 return false; 16 } 17 return compiler.GetNamespaceSupporter().IsSameAreaNamespace( this->namespaceScopes, namespaceScopes ); 18 } 19 bool ConstBase::IsEqualSymbol( const string &fullName ) const 20 { 21 char AreaName[VN_SIZE] = ""; //オブジェクト変数 22 char NestName[VN_SIZE] = ""; //入れ子メンバ 23 bool isNest = CClass::SplitName( fullName.c_str(), AreaName, NestName ); 24 25 return IsEqualSymbol( NamespaceScopes( AreaName ), NestName ); 26 } 27 28 29 30 31 CConst::CConst( const NamespaceScopes &namespaceScopes, const string &name, const Type &newType, _int64 i64data) 32 : ConstBase( namespaceScopes, name ) 33 , type( newType ) 34 , i64data( i64data ) 35 , pNext( NULL ) 36 { 37 } 38 CConst::CConst( const NamespaceScopes &namespaceScopes, const string &name, int value) 39 : ConstBase( namespaceScopes, name ) 40 , type( Type(DEF_LONG) ) 41 , i64data( value ) 42 , pNext( NULL ) 43 { 44 } 45 CConst::~CConst(){ 46 //連結先のオブジェクトを破棄 47 if(pNext){ 48 delete pNext; 49 pNext = 0; 50 } 51 } 52 53 Type CConst::GetType(){ 54 return type; 55 } 56 _int64 CConst::GetWholeData(){ 57 return i64data; 58 } 4 5 59 6 double CConst::GetDoubleData(){ 60 7 double dbl; … … 63 10 } 64 11 65 66 67 CConstMacro::CConstMacro( const NamespaceScopes &namespaceScopes, const string &name, char *Expression) 68 : ConstBase( namespaceScopes, name ) 69 { 70 } 71 CConstMacro::~CConstMacro(){ 72 } 73 74 75 76 77 78 CDBConst::CDBConst(){ 79 memset(this,0,sizeof(CDBConst)); 80 81 ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1); 82 Init(); 83 } 84 CDBConst::~CDBConst(){ 85 Free(); 86 87 free(ppHash); 88 } 89 90 //解放 91 void CDBConst::Free(){ 12 void AddConst( const NamespaceScopes &namespaceScopes, char *buffer ){ 92 13 int i; 93 for(i=0; i<MAX_HASH; i++){94 if(ppHash[i]){95 delete ppHash[i];96 ppHash[i] = 0;97 }98 }99 }100 101 //初期化102 void CDBConst::Init(){103 Free();104 }105 106 void AddConstData(char *Command);107 void CDBConst::Add( const NamespaceScopes &namespaceScopes, char *buffer ){108 int i;109 14 110 15 //名前を取得 111 char Name[VN_SIZE];16 char name[VN_SIZE]; 112 17 for(i=0;;i++){ 113 18 if(buffer[i]=='\0'){ … … 116 21 } 117 22 if(buffer[i]=='='||buffer[i]=='('){ 118 Name[i]=0;23 name[i]=0; 119 24 break; 120 25 } 121 Name[i]=buffer[i];26 name[i]=buffer[i]; 122 27 } 123 28 124 29 //重複チェック 125 if(GetBasicType(Name)){ 126 SetError(15,Name,cp); 30 if( compiler.GetMeta().GetGlobalConstMacros().IsExist( name ) 31 || compiler.GetMeta().GetGlobalConsts().IsExist( name ) ) 32 { 33 SetError(15,name,cp); 127 34 return; 128 35 } … … 131 38 //定数マクロ 132 39 133 //未完成 134 AddConstData(buffer); 40 compiler.GetMeta().GetGlobalConstMacros().Add( namespaceScopes, name, buffer + i ); 135 41 } 136 42 else{ 137 43 //一般の定数 138 char *Expression = buffer + i + 1; 139 140 AddConst( namespaceScopes, Name,Expression); 141 } 142 } 143 144 void CDBConst::AddConst( const string &name, CConst *newconst){ 145 int key = hash_default(name.c_str()); 146 147 //ハッシュリストに追加 148 if(ppHash[key]){ 149 CConst *pConst = ppHash[key]; 150 while(pConst->pNext){ 151 pConst = pConst->pNext; 152 } 153 154 pConst->pNext = newconst; 155 } 156 else{ 157 ppHash[key] = newconst; 158 } 159 } 160 void CDBConst::AddConst( const NamespaceScopes &namespaceScopes, const string &name , char *Expression){ 44 char *expression = buffer + i + 1; 45 46 compiler.GetMeta().GetGlobalConsts().Add( namespaceScopes, name, expression ); 47 } 48 } 49 50 void Consts::Add( const NamespaceScopes &namespaceScopes, const string &name , char *Expression) 51 { 161 52 _int64 i64data; 162 53 Type resultType; … … 172 63 CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data); 173 64 174 AddConst( name, newconst); 175 } 176 void CDBConst::AddConst(const NamespaceScopes &namespaceScopes, const string &name, int value){ 65 //ハッシュリストに追加 66 Put( newconst ); 67 } 68 void Consts::Add(const NamespaceScopes &namespaceScopes, const string &name, int value){ 177 69 CConst *newconst = new CConst( namespaceScopes, name, value); 178 70 179 AddConst(name, newconst); 180 } 181 182 CConst *CDBConst::GetObjectPtr( const string &name ){ 71 //ハッシュリストに追加 72 Put( newconst ); 73 } 74 75 CConst *Consts::GetObjectPtr( const string &name ){ 183 76 char ObjName[VN_SIZE]; //オブジェクト変数 184 77 char NestMember[VN_SIZE]; //入れ子メンバ 185 bool isObjectMember = CClass::SplitName( name.c_str(), ObjName, NestMember ); 186 187 //ハッシュ値を取得 188 int key; 189 key=hash_default( NestMember ); 190 191 //格納位置を取得 192 CConst *pConst; 193 pConst=ppHash[key]; 194 while(pConst){ 195 if( pConst->IsEqualSymbol( name ) ) break; 196 197 pConst=pConst->pNext; 78 bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember ); 79 80 CConst *pConst = GetHashArrayElement( NestMember ); 81 while( pConst ) 82 { 83 if( pConst->IsEqualSymbol( name ) ) 84 { 85 break; 86 } 87 pConst = pConst->GetChainNext(); 198 88 } 199 89 … … 202 92 203 93 204 int C DBConst::GetBasicType(char *Name){94 int Consts::GetBasicType(char *Name){ 205 95 CConst *pConst = GetObjectPtr(Name); 206 96 … … 209 99 return pConst->GetType().GetBasicType(); 210 100 } 211 _int64 C DBConst::GetWholeData(char *Name){101 _int64 Consts::GetWholeData(char *Name){ 212 102 CConst *pConst = GetObjectPtr(Name); 213 103 … … 216 106 return pConst->GetWholeData(); 217 107 } 218 double C DBConst::GetDoubleData(char *Name){108 double Consts::GetDoubleData(char *Name){ 219 109 CConst *pConst = GetObjectPtr(Name); 220 110 … … 223 113 return pConst->GetDoubleData(); 224 114 } 225 bool C DBConst::IsStringPtr( char *Name ){115 bool Consts::IsStringPtr( char *Name ){ 226 116 CConst *pConst = GetObjectPtr(Name); 227 117 … … 232 122 return ( type.GetBasicType() == typeOfPtrChar && type.GetIndex() == LITERAL_STRING ); 233 123 } 124 125 126 bool ConstMacro::GetCalcBuffer( const char *parameterStr, char *dest ) const 127 { 128 extern HANDLE hHeap; 129 int i2,i3,i4,num; 130 char temporary[VN_SIZE]; 131 char *pParms[MAX_PARMS]; 132 num=0; 133 i2=0; 134 while(1){ 135 i2=GetOneParameter(parameterStr,i2,temporary); 136 137 pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1); 138 lstrcpy(pParms[num],temporary); 139 140 num++; 141 if(parameterStr[i2]=='\0') break; 142 } 143 if( num != this->GetParameters().size() ){ 144 extern int cp; 145 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]); 146 SetError(10,GetName().c_str(),cp); 147 lstrcpy(dest,"0"); 148 return 1; 149 } 150 151 i2=0; 152 i4=0; 153 while(1){ 154 155 //数式内の項を取得 156 for(i3=0;;i2++,i3++){ 157 if(!IsVariableChar( this->GetExpression()[i2] )){ 158 temporary[i3]=0; 159 break; 160 } 161 temporary[i3] = this->GetExpression()[i2]; 162 } 163 164 //パラメータと照合する 165 for( i3=0; i3<(int)this->GetParameters().size(); i3++ ){ 166 if( this->GetParameters()[i3] == temporary ) break; 167 } 168 169 if( i3 == (int)this->GetParameters().size() ){ 170 //パラメータでないとき 171 lstrcpy(dest+i4,temporary); 172 i4+=lstrlen(temporary); 173 } 174 else{ 175 //パラメータのとき 176 lstrcpy(dest+i4,pParms[i3]); 177 i4+=lstrlen(pParms[i3]); 178 } 179 180 //演算子をコピー 181 for(;;i2++,i4++){ 182 if( this->GetExpression()[i2] == 1 ){ 183 dest[i4++] = this->GetExpression()[i2++]; 184 dest[i4] = this->GetExpression()[i2]; 185 continue; 186 } 187 if(IsVariableTopChar( this->GetExpression()[i2] )) break; 188 dest[i4] = this->GetExpression()[i2]; 189 if( this->GetExpression()[i2] == '\0' ) break; 190 } 191 192 if( this->GetExpression()[i2] == '\0' ) break; 193 } 194 195 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]); 196 197 return 1; 198 } 199 200 // マクロ定数を追加するための関数 201 void ConstMacros::Add( const NamespaceScopes &namespaceScopes, const std::string &name, const char *parameterStr ) 202 { 203 std::vector<std::string> parameters; 204 205 int i = 0; 206 if(parameterStr[i]!='(') 207 { 208 SetError(); 209 return; 210 } 211 212 char temporary[VN_SIZE]; 213 int i2; 214 for(i++,i2=0;;i++,i2++){ 215 if(parameterStr[i]=='\0'){ 216 SetError(1,NULL,cp); 217 return; 218 } 219 if(parameterStr[i]==','||parameterStr[i]==')'){ 220 temporary[i2]=0; 221 222 parameters.push_back( temporary ); 223 224 if(parameterStr[i]==')'){ 225 i++; 226 if(parameterStr[i]!='='){ 227 extern int cp; 228 SetError(1,NULL,cp); 229 return; 230 } 231 break; 232 } 233 234 i2=-1; 235 continue; 236 } 237 temporary[i2]=parameterStr[i]; 238 } 239 240 //データ 241 lstrcpy(temporary,parameterStr+i+1); 242 243 Put( new ConstMacro( namespaceScopes, name, parameters, temporary ) ); 244 } 245 ConstMacro *ConstMacros::Find( const std::string &name ){ 246 char ObjName[VN_SIZE]; //オブジェクト変数 247 char NestMember[VN_SIZE]; //入れ子メンバ 248 bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember ); 249 250 ConstMacro *pConstMacro = GetHashArrayElement( NestMember ); 251 while( pConstMacro ) 252 { 253 if( pConstMacro->IsEqualSymbol( name ) ) 254 { 255 break; 256 } 257 pConstMacro = pConstMacro->GetChainNext(); 258 } 259 260 return pConstMacro; 261 }
Note:
See TracChangeset
for help on using the changeset viewer.