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

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