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

Last change on this file since 204 was 199, checked in by dai_9181, 17 years ago
File size: 4.7 KB
Line 
1#include <Compiler.h>
2#include <NamespaceSupporter.h>
3
4#include "common.h"
5#include OPCODE_H_PATH //opcode.h
6
7
8//シングルトンオブジェクト
9CDBConst CDBConst::obj;
10
11
12bool 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}
19bool 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
31CConst::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}
38CConst::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}
45CConst::~CConst(){
46 //連結先のオブジェクトを破棄
47 if(pNext){
48 delete pNext;
49 pNext = 0;
50 }
51}
52
53Type CConst::GetType(){
54 return type;
55}
56_int64 CConst::GetWholeData(){
57 return i64data;
58}
59double CConst::GetDoubleData(){
60 double dbl;
61 memcpy(&dbl,&i64data,sizeof(_int64));
62 return dbl;
63}
64
65
66
67CConstMacro::CConstMacro( const NamespaceScopes &namespaceScopes, const string &name, char *Expression)
68 : ConstBase( namespaceScopes, name )
69{
70}
71CConstMacro::~CConstMacro(){
72}
73
74
75
76
77
78CDBConst::CDBConst(){
79 memset(this,0,sizeof(CDBConst));
80
81 ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1);
82 Init();
83}
84CDBConst::~CDBConst(){
85 Free();
86
87 free(ppHash);
88}
89
90//解放
91void CDBConst::Free(){
92 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//初期化
102void CDBConst::Init(){
103 Free();
104}
105
106void AddConstData(char *Command);
107void CDBConst::Add( const NamespaceScopes &namespaceScopes, char *buffer ){
108 int i;
109
110 //名前を取得
111 char Name[VN_SIZE];
112 for(i=0;;i++){
113 if(buffer[i]=='\0'){
114 SetError(10,"Const",cp);
115 return;
116 }
117 if(buffer[i]=='='||buffer[i]=='('){
118 Name[i]=0;
119 break;
120 }
121 Name[i]=buffer[i];
122 }
123
124 //重複チェック
125 if(GetBasicType(Name)){
126 SetError(15,Name,cp);
127 return;
128 }
129
130 if(buffer[i] == '('){
131 //定数マクロ
132
133 //未完成
134 AddConstData(buffer);
135 }
136 else{
137 //一般の定数
138 char *Expression = buffer + i + 1;
139
140 AddConst( namespaceScopes, Name,Expression);
141 }
142}
143
144void 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}
160void CDBConst::AddConst( const NamespaceScopes &namespaceScopes, const string &name , char *Expression){
161 _int64 i64data;
162 Type resultType;
163 if( !StaticCalculation(false, Expression, 0, &i64data, resultType) ){
164 //変数の場合
165 //何もしない(実行領域コンパイル時にdim宣言として扱う)
166 return;
167 }
168
169 //リテラル値の場合
170 //登録を行う
171
172 CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data);
173
174 AddConst( name, newconst);
175}
176void CDBConst::AddConst(const NamespaceScopes &namespaceScopes, const string &name, int value){
177 CConst *newconst = new CConst( namespaceScopes, name, value);
178
179 AddConst(name, newconst);
180}
181
182CConst *CDBConst::GetObjectPtr( const string &name ){
183 char ObjName[VN_SIZE]; //オブジェクト変数
184 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;
198 }
199
200 return pConst;
201}
202
203
204int CDBConst::GetBasicType(char *Name){
205 CConst *pConst = GetObjectPtr(Name);
206
207 if(!pConst) return 0;
208
209 return pConst->GetType().GetBasicType();
210}
211_int64 CDBConst::GetWholeData(char *Name){
212 CConst *pConst = GetObjectPtr(Name);
213
214 if(!pConst) return 0;
215
216 return pConst->GetWholeData();
217}
218double CDBConst::GetDoubleData(char *Name){
219 CConst *pConst = GetObjectPtr(Name);
220
221 if(!pConst) return 0;
222
223 return pConst->GetDoubleData();
224}
225bool CDBConst::IsStringPtr( char *Name ){
226 CConst *pConst = GetObjectPtr(Name);
227
228 if(!pConst) return false;
229
230 const Type &type = pConst->GetType();
231
232 return ( type.GetBasicType() == typeOfPtrChar && type.GetIndex() == LITERAL_STRING );
233}
Note: See TracBrowser for help on using the repository browser.