source: dev/trunk/abdev/BasicCompiler_Common/Const.cpp@ 185

Last change on this file since 185 was 182, checked in by dai_9181, 17 years ago
File size: 4.6 KB
RevLine 
[4]1#include "common.h"
[7]2#include OPCODE_H_PATH //opcode.h
[4]3
4
[7]5//シングルトンオブジェクト
6CDBConst CDBConst::obj;
[4]7
[7]8
[103]9bool ConstBase::IsEqualSymbol( const NamespaceScopes &namespaceScopes, const string &name ) const
10{
11 if( GetName() != name ){
12 return false;
13 }
14 return NamespaceScopes::IsSameArea( this->namespaceScopes, namespaceScopes );
[4]15}
[103]16bool ConstBase::IsEqualSymbol( const string &fullName ) const
17{
18 char AreaName[VN_SIZE] = ""; //オブジェクト変数
19 char NestName[VN_SIZE] = ""; //入れ子メンバ
[182]20 bool isNest = CClass::SplitName( fullName.c_str(), AreaName, NestName );
[4]21
[103]22 return IsEqualSymbol( NamespaceScopes( AreaName ), NestName );
[7]23}
[4]24
25
[7]26
[103]27
28CConst::CConst( const NamespaceScopes &namespaceScopes, const string &name, const Type &type, _int64 i64data)
29 : ConstBase( namespaceScopes, name )
30 , type( type )
31 , i64data( i64data )
32 , pNext( NULL )
[4]33{
34}
[103]35CConst::CConst( const NamespaceScopes &namespaceScopes, const string &name, int value)
36 : ConstBase( namespaceScopes, name )
37 , type( Type(DEF_LONG) )
38 , i64data( value )
39 , pNext( NULL )
[7]40{
41}
[4]42CConst::~CConst(){
[7]43 //連結先のオブジェクトを破棄
44 if(pNext){
45 delete pNext;
46 pNext = 0;
47 }
[4]48}
49
[103]50Type CConst::GetType(){
51 return type;
[7]52}
53_int64 CConst::GetWholeData(){
54 return i64data;
55}
56double CConst::GetDoubleData(){
57 double dbl;
58 memcpy(&dbl,&i64data,sizeof(_int64));
59 return dbl;
60}
61
62
63
[103]64CConstMacro::CConstMacro( const NamespaceScopes &namespaceScopes, const string &name, char *Expression)
65 : ConstBase( namespaceScopes, name )
[5]66{
67}
68CConstMacro::~CConstMacro(){
69}
[4]70
71
[5]72
[7]73
74
[4]75CDBConst::CDBConst(){
[7]76 memset(this,0,sizeof(CDBConst));
[4]77
[7]78 ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1);
79 Init();
[4]80}
81CDBConst::~CDBConst(){
[7]82 Free();
83
84 free(ppHash);
85}
86
87//解放
88void CDBConst::Free(){
[4]89 int i;
[7]90 for(i=0; i<MAX_HASH; i++){
91 if(ppHash[i]){
92 delete ppHash[i];
93 ppHash[i] = 0;
94 }
[4]95 }
[7]96}
[4]97
[7]98//初期化
99void CDBConst::Init(){
100 Free();
[4]101}
102
[7]103void AddConstData(char *Command);
[103]104void CDBConst::Add( const NamespaceScopes &namespaceScopes, char *buffer ){
[4]105 int i;
106
107 //名前を取得
108 char Name[VN_SIZE];
109 for(i=0;;i++){
110 if(buffer[i]=='\0'){
111 SetError(10,"Const",cp);
112 return;
113 }
114 if(buffer[i]=='='||buffer[i]=='('){
115 Name[i]=0;
116 break;
117 }
118 Name[i]=buffer[i];
119 }
120
[7]121 //重複チェック
[103]122 if(GetBasicType(Name)){
[7]123 SetError(15,Name,cp);
124 return;
125 }
126
[4]127 if(buffer[i] == '('){
128 //定数マクロ
129
130 //未完成
[7]131 AddConstData(buffer);
[4]132 }
133 else{
134 //一般の定数
135 char *Expression = buffer + i + 1;
136
[103]137 AddConst( namespaceScopes, Name,Expression);
[4]138 }
139}
140
[103]141void CDBConst::AddConst( const string &name, CConst *newconst){
142 int key = hash_default(name.c_str());
[7]143
144 //ハッシュリストに追加
145 if(ppHash[key]){
146 CConst *pConst = ppHash[key];
147 while(pConst->pNext){
148 pConst = pConst->pNext;
149 }
150
151 pConst->pNext = newconst;
152 }
153 else{
154 ppHash[key] = newconst;
155 }
156}
[103]157void CDBConst::AddConst( const NamespaceScopes &namespaceScopes, const string &name , char *Expression){
[7]158 _int64 i64data;
[75]159 Type resultType;
160 if( !StaticCalculation(false, Expression, 0, &i64data, resultType) ){
161 //変数の場合
162 //何もしない(実行領域コンパイル時にdim宣言として扱う)
163 return;
164 }
[4]165
[75]166 //リテラル値の場合
167 //登録を行う
[7]168
[103]169 CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data);
[7]170
[103]171 AddConst( name, newconst);
[4]172}
[103]173void CDBConst::AddConst(const NamespaceScopes &namespaceScopes, const string &name, int value){
174 CConst *newconst = new CConst( namespaceScopes, name, value);
[7]175
[103]176 AddConst(name, newconst);
[7]177}
178
[103]179CConst *CDBConst::GetObjectPtr( const string &name ){
180 char ObjName[VN_SIZE]; //オブジェクト変数
181 char NestMember[VN_SIZE]; //入れ子メンバ
[182]182 bool isObjectMember = CClass::SplitName( name.c_str(), ObjName, NestMember );
[103]183
[7]184 //ハッシュ値を取得
185 int key;
[103]186 key=hash_default( NestMember );
[7]187
188 //格納位置を取得
189 CConst *pConst;
190 pConst=ppHash[key];
191 while(pConst){
[103]192 if( pConst->IsEqualSymbol( name ) ) break;
[7]193
194 pConst=pConst->pNext;
195 }
196
197 return pConst;
198}
199
200
[103]201int CDBConst::GetBasicType(char *Name){
[7]202 CConst *pConst = GetObjectPtr(Name);
203
204 if(!pConst) return 0;
205
[103]206 return pConst->GetType().GetBasicType();
[7]207}
208_int64 CDBConst::GetWholeData(char *Name){
209 CConst *pConst = GetObjectPtr(Name);
210
211 if(!pConst) return 0;
212
213 return pConst->GetWholeData();
214}
215double CDBConst::GetDoubleData(char *Name){
216 CConst *pConst = GetObjectPtr(Name);
217
218 if(!pConst) return 0;
219
220 return pConst->GetDoubleData();
221}
[103]222bool CDBConst::IsStringPtr( char *Name ){
223 CConst *pConst = GetObjectPtr(Name);
224
225 if(!pConst) return false;
226
227 const Type &type = pConst->GetType();
228
229 return ( type.GetBasicType() == typeOfPtrChar && type.GetIndex() == LITERAL_STRING );
230}
Note: See TracBrowser for help on using the repository browser.