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