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

Last change on this file since 182 was 182, checked in by dai_9181, 17 years ago
File size: 4.6 KB
Line 
1#include "common.h"
2#include OPCODE_H_PATH //opcode.h
3
4
5//シングルトンオブジェクト
6CDBConst CDBConst::obj;
7
8
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 );
15}
16bool ConstBase::IsEqualSymbol( const string &fullName ) const
17{
18 char AreaName[VN_SIZE] = ""; //オブジェクト変数
19 char NestName[VN_SIZE] = ""; //入れ子メンバ
20 bool isNest = CClass::SplitName( fullName.c_str(), AreaName, NestName );
21
22 return IsEqualSymbol( NamespaceScopes( AreaName ), NestName );
23}
24
25
26
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 )
33{
34}
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 )
40{
41}
42CConst::~CConst(){
43 //連結先のオブジェクトを破棄
44 if(pNext){
45 delete pNext;
46 pNext = 0;
47 }
48}
49
50Type CConst::GetType(){
51 return type;
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
64CConstMacro::CConstMacro( const NamespaceScopes &namespaceScopes, const string &name, char *Expression)
65 : ConstBase( namespaceScopes, name )
66{
67}
68CConstMacro::~CConstMacro(){
69}
70
71
72
73
74
75CDBConst::CDBConst(){
76 memset(this,0,sizeof(CDBConst));
77
78 ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1);
79 Init();
80}
81CDBConst::~CDBConst(){
82 Free();
83
84 free(ppHash);
85}
86
87//解放
88void CDBConst::Free(){
89 int i;
90 for(i=0; i<MAX_HASH; i++){
91 if(ppHash[i]){
92 delete ppHash[i];
93 ppHash[i] = 0;
94 }
95 }
96}
97
98//初期化
99void CDBConst::Init(){
100 Free();
101}
102
103void AddConstData(char *Command);
104void CDBConst::Add( const NamespaceScopes &namespaceScopes, char *buffer ){
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
121 //重複チェック
122 if(GetBasicType(Name)){
123 SetError(15,Name,cp);
124 return;
125 }
126
127 if(buffer[i] == '('){
128 //定数マクロ
129
130 //未完成
131 AddConstData(buffer);
132 }
133 else{
134 //一般の定数
135 char *Expression = buffer + i + 1;
136
137 AddConst( namespaceScopes, Name,Expression);
138 }
139}
140
141void CDBConst::AddConst( const string &name, CConst *newconst){
142 int key = hash_default(name.c_str());
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}
157void CDBConst::AddConst( const NamespaceScopes &namespaceScopes, const string &name , char *Expression){
158 _int64 i64data;
159 Type resultType;
160 if( !StaticCalculation(false, Expression, 0, &i64data, resultType) ){
161 //変数の場合
162 //何もしない(実行領域コンパイル時にdim宣言として扱う)
163 return;
164 }
165
166 //リテラル値の場合
167 //登録を行う
168
169 CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data);
170
171 AddConst( name, newconst);
172}
173void CDBConst::AddConst(const NamespaceScopes &namespaceScopes, const string &name, int value){
174 CConst *newconst = new CConst( namespaceScopes, name, value);
175
176 AddConst(name, newconst);
177}
178
179CConst *CDBConst::GetObjectPtr( const string &name ){
180 char ObjName[VN_SIZE]; //オブジェクト変数
181 char NestMember[VN_SIZE]; //入れ子メンバ
182 bool isObjectMember = CClass::SplitName( name.c_str(), ObjName, NestMember );
183
184 //ハッシュ値を取得
185 int key;
186 key=hash_default( NestMember );
187
188 //格納位置を取得
189 CConst *pConst;
190 pConst=ppHash[key];
191 while(pConst){
192 if( pConst->IsEqualSymbol( name ) ) break;
193
194 pConst=pConst->pNext;
195 }
196
197 return pConst;
198}
199
200
201int CDBConst::GetBasicType(char *Name){
202 CConst *pConst = GetObjectPtr(Name);
203
204 if(!pConst) return 0;
205
206 return pConst->GetType().GetBasicType();
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}
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.