Changeset 7 in dev for BasicCompiler_Common/Const.cpp


Ignore:
Timestamp:
Dec 7, 2006, 3:09:44 AM (17 years ago)
Author:
dai_9181
Message:

Constステートメントで定数変数を宣言できるように改良。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • BasicCompiler_Common/Const.cpp

    r5 r7  
    11#include "common.h"
    2 
     2#include OPCODE_H_PATH  //opcode.h
     3
     4
     5//シングルトンオブジェクト
     6CDBConst CDBConst::obj;
    37
    48
     
    1216}
    1317
    14 
    15 
    16 CConst::CConst(char *Name, char *Expression):CConstBase(Name)
     18char *CConstBase::GetName(){
     19    return Name;
     20}
     21
     22
     23
     24CConst::CConst(char *Name, int Type, _int64 i64data):CConstBase(Name)
    1725{
    18     LONG_PTR lpIndex;
    19     Type = StaticCalculation(false, Expression, 0, &i64data, &lpIndex);
     26    this->Type = Type;
     27    this->i64data = i64data;
     28
     29    //連結リストを初期化
     30    pNext = 0;
     31}
     32CConst::CConst(char *Name, int value):CConstBase(Name)
     33{
     34    Type = DEF_LONG;
     35    i64data = value;
     36
     37    //連結リストを初期化
     38    pNext = 0;
    2039}
    2140CConst::~CConst(){
    22 }
     41    //連結先のオブジェクトを破棄
     42    if(pNext){
     43        delete pNext;
     44        pNext = 0;
     45    }
     46}
     47
     48int CConst::GetType(){
     49    return Type;
     50}
     51_int64 CConst::GetWholeData(){
     52    return i64data;
     53}
     54double CConst::GetDoubleData(){
     55    double dbl;
     56    memcpy(&dbl,&i64data,sizeof(_int64));
     57    return dbl;
     58}
     59
     60
    2361
    2462CConstMacro::CConstMacro(char *Name, char *Expression):CConstBase(Name)
     
    3068
    3169
     70
     71
    3272CDBConst::CDBConst(){
    33     //定数管理領域を初期化
    34     ppobj_Const = (CConst **)malloc(1);
    35     NumOfConst = 0;
    36 
    37     //定数マクロ管理領域を初期化
    38     ppobj_Macro = (CConstMacro **)malloc(1);
    39     NumOfMacro = 0;
     73    memset(this,0,sizeof(CDBConst));
     74
     75    ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1);
     76    Init();
    4077}
    4178CDBConst::~CDBConst(){
     79    Free();
     80
     81    free(ppHash);
     82}
     83
     84//解放
     85void CDBConst::Free(){
    4286    int i;
    43     for(i=0; i<NumOfConst; i++){
    44         delete ppobj_Const[i];
    45     }
    46     free(ppobj_Const);
    47     ppobj_Const = 0;
    48 
    49     for(i=0; i<NumOfMacro; i++){
    50         delete ppobj_Macro[i];
    51     }
    52     free(ppobj_Macro);
    53     ppobj_Macro = 0;
    54 }
    55 
     87    for(i=0; i<MAX_HASH; i++){
     88        if(ppHash[i]){
     89            delete ppHash[i];
     90            ppHash[i] = 0;
     91        }
     92    }
     93}
     94
     95//初期化
     96void CDBConst::Init(){
     97    Free();
     98}
     99
     100void AddConstData(char *Command);
    56101void CDBConst::Add(char *buffer){
    57102    int i;
     
    71116    }
    72117
     118    //重複チェック
     119    if(GetType(Name)){
     120        SetError(15,Name,cp);
     121        return;
     122    }
     123
    73124    if(buffer[i] == '('){
    74125        //定数マクロ
    75126
    76127        //未完成
     128        AddConstData(buffer);
    77129    }
    78130    else{
     
    84136}
    85137
     138void CDBConst::AddConst(char *Name, CConst *newconst){
     139    int key = hash_default(Name);
     140
     141    //ハッシュリストに追加
     142    if(ppHash[key]){
     143        CConst *pConst = ppHash[key];
     144        while(pConst->pNext){
     145            pConst = pConst->pNext;
     146        }
     147
     148        pConst->pNext = newconst;
     149    }
     150    else{
     151        ppHash[key] = newconst;
     152    }
     153}
    86154void CDBConst::AddConst(char *Name, char *Expression){
    87     CConst *newconst = new CConst(Name, Expression);
    88 
    89     //管理クラスへ追加
    90     ppobj_Const = (CConst **)realloc( ppobj_Const, (NumOfConst + 1) * sizeof(CConst) );
    91     ppobj_Const[NumOfConst] = newconst;
    92     NumOfConst++;
    93 }
     155    LONG_PTR lpIndex;
     156    _int64 i64data;
     157    int Type = StaticCalculation(false, Expression, 0, &i64data, &lpIndex);
     158
     159    if(Type){
     160        //リテラル値の場合
     161        //登録を行う
     162
     163        CConst *newconst = new CConst(Name, Type, i64data);
     164
     165        AddConst(Name, newconst);
     166    }
     167    else{
     168        //変数の場合
     169        //何もしない(実行領域コンパイル時にdim宣言として扱う)
     170    }
     171}
     172void CDBConst::AddConst(char *Name, int value){
     173    CConst *newconst = new CConst(Name, value);
     174
     175    AddConst(Name, newconst);
     176}
     177
     178CConst *CDBConst::GetObjectPtr(char *Name){
     179    //ハッシュ値を取得
     180    int key;
     181    key=hash_default(Name);
     182
     183    //格納位置を取得
     184    CConst *pConst;
     185    pConst=ppHash[key];
     186    while(pConst){
     187        if(lstrcmp(pConst->GetName(),Name)==0) break;
     188
     189        pConst=pConst->pNext;
     190    }
     191
     192    return pConst;
     193}
     194
     195
     196int CDBConst::GetType(char *Name){
     197    CConst *pConst = GetObjectPtr(Name);
     198
     199    if(!pConst) return 0;
     200
     201    return pConst->GetType();
     202}
     203_int64 CDBConst::GetWholeData(char *Name){
     204    CConst *pConst = GetObjectPtr(Name);
     205
     206    if(!pConst) return 0;
     207
     208    return pConst->GetWholeData();
     209}
     210double CDBConst::GetDoubleData(char *Name){
     211    CConst *pConst = GetObjectPtr(Name);
     212
     213    if(!pConst) return 0;
     214
     215    return pConst->GetDoubleData();
     216}
Note: See TracChangeset for help on using the changeset viewer.