Changeset 7 in dev for BasicCompiler_Common


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

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

Location:
BasicCompiler_Common
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • BasicCompiler_Common/Compile.cpp

    r5 r7  
    111111        switch(Command[1]){
    112112            case ESC_CONST:
     113                OpcodeDim(Command+2, DIMFLAG_CONST);
     114                break;
     115
    113116            case ESC_TYPEDEF:
    114117                break;
  • 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}
  • BasicCompiler_Common/Const.h

    r5 r7  
    99    CConstBase(char *Name);
    1010    ~CConstBase();
     11
     12    char *GetName();
    1113};
    1214
     
    1517    int Type;
    1618    _int64 i64data;
     19
    1720public:
     21    CConst *pNext;
    1822
    19     CConst(char *Name, char *Expression);
     23    CConst(char *Name, int Type, _int64 i64data);
     24    CConst(char *Name, int value);
    2025    ~CConst();
     26
     27    int GetType();
     28    _int64 GetWholeData();
     29    double GetDoubleData();
    2130};
    2231
     
    3342//定数管理クラス
    3443class CDBConst{
    35     CConst **ppobj_Const;
    36     int NumOfConst;
     44    CConst **ppHash;
    3745
    3846    CConstMacro **ppobj_Macro;
    3947    int NumOfMacro;
     48
     49    //シングルトンクラスなので、プライベートに置く
     50    CDBConst();
     51    ~CDBConst();
     52    void _free();
     53    void Free();
     54
    4055public:
    4156
    42     CDBConst();
    43     ~CDBConst();
     57    void Init();
    4458
    4559    void Add(char *buffer);
     60private:
     61    void AddConst(char *Name, CConst *newconst);
     62public:
     63    void AddConst(char *Name, char *Expression);
     64    void AddConst(char *Name, int value);
    4665
    47     void AddConst(char *Name, char *Expression);
     66private:
     67    CConst *GetObjectPtr(char *Name);
     68public:
     69
     70    int GetType(char *Name);
     71    _int64 GetWholeData(char *Name);
     72    double GetDoubleData(char *Name);
     73
     74
     75    //シングルトンオブジェクト
     76    static CDBConst obj;
    4877};
  • BasicCompiler_Common/Intermediate_Step2.cpp

    r4 r7  
    4444            return;
    4545        }
    46         if(Command[i]=='='||Command[i]=='('){
     46        if(Command[i]=='='){
     47            //定数定義は新しいクラスモジュール(CDBConst)へ移行
     48            return;
     49        }
     50        if(Command[i]=='('){
    4751            temporary[i]=0;
    4852            break;
     
    230234
    231235        //定数を追加
    232         CONSTINFO *pci;
    233         pci=GetNewConstHash(temporary);
    234         pci->name=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
    235         lstrcpy(pci->name,temporary);
    236         pci->type=DEF_LONG;
    237         pci->lpIndex=-1;
    238         pci->DblValue=(double)NextValue;
    239         pci->StrValue=0;
     236        CDBConst::obj.AddConst(temporary, NextValue);
    240237    }
    241238}
     
    282279                temporary[i2]=basbuf[i];
    283280            }
    284             AddConstData(temporary);
     281            CDBConst::obj.Add(temporary);
    285282            if(basbuf[i]=='\0') break;
    286283        }
  • BasicCompiler_Common/NumOpe_GetType.cpp

    r5 r7  
    434434                    //////////////
    435435
    436                     i3=GetConstValue(values[i],&dbl,temporary,&index_stack[sp]);
    437                     if(i3!=-1){
     436                    i3 = CDBConst::obj.GetType(values[i]);
     437                    if(i3){
    438438                        type[sp]=i3;
    439439                        if(IsRealNumberType(i3)){
  • BasicCompiler_Common/Variable.cpp

    r5 r7  
    10101010    if(VarSize%PTR_SIZE) VarSize+=PTR_SIZE-(VarSize%PTR_SIZE);
    10111011
    1012     lstrcpy(GlobalVar[MaxGlobalVarNum].name,name);
    1013     if(SubScripts[0]==-1) GlobalVar[MaxGlobalVarNum].bArray=0;
    1014     else GlobalVar[MaxGlobalVarNum].bArray=1;
    1015     GlobalVar[MaxGlobalVarNum].type=pTypeInfo->type;
    1016     GlobalVar[MaxGlobalVarNum].u.index=pTypeInfo->u.lpIndex;
     1012    //新しいオブジェクトポインタを取得
     1013    VARIABLE *pVar = &GlobalVar[MaxGlobalVarNum];
     1014
     1015    //グローバル変数の個数を増加
     1016    MaxGlobalVarNum++;
     1017
     1018    lstrcpy(pVar->name,name);
     1019    if(dwFlag & DIMFLAG_CONST) pVar->bConst = 1;
     1020    else pVar->bConst = 0;
     1021    if(SubScripts[0]==-1) pVar->bArray=0;
     1022    else pVar->bArray=1;
     1023    pVar->type=pTypeInfo->type;
     1024    pVar->u.index=pTypeInfo->u.lpIndex;
    10171025
    10181026    //コンストラクタ用パラメータ
    1019     GlobalVar[MaxGlobalVarNum].ConstractParameter=(char *)HeapAlloc(hHeap,0,lstrlen(ConstractParameter)+1);
    1020     lstrcpy(GlobalVar[MaxGlobalVarNum].ConstractParameter,ConstractParameter);
     1027    pVar->ConstractParameter=(char *)HeapAlloc(hHeap,0,lstrlen(ConstractParameter)+1);
     1028    lstrcpy(pVar->ConstractParameter,ConstractParameter);
    10211029
    10221030    if(InitBuf[0]||dwFlag==DIMFLAG_INITDEBUGVAR){
    10231031        //初期バッファがあるとき
    1024         GlobalVar[MaxGlobalVarNum].offset=AllInitGlobalVarSize;
     1032        pVar->offset=AllInitGlobalVarSize;
    10251033        AllInitGlobalVarSize+=VarSize;
    10261034    }
    10271035    else{
    10281036        //初期バッファがないとき
    1029         GlobalVar[MaxGlobalVarNum].offset=AllGlobalVarSize | 0x80000000;
     1037        pVar->offset=AllGlobalVarSize | 0x80000000;
    10301038        AllGlobalVarSize+=VarSize;
    10311039    }
    10321040
    10331041    //レキシカルスコープ
    1034     GlobalVar[MaxGlobalVarNum].ScopeLevel=obj_LexScopes.GetNowLevel();
    1035     GlobalVar[MaxGlobalVarNum].ScopeStartAddress=obj_LexScopes.GetStartAddress();
    1036     GlobalVar[MaxGlobalVarNum].bLiving=TRUE;
     1042    pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
     1043    pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
     1044    pVar->bLiving=TRUE;
     1045
     1046    //エラー用
     1047    pVar->source_code_address=cp;
    10371048
    10381049    //初期バッファにデータをセット
     
    10421053        initGlobalBuf,
    10431054        AllInitGlobalVarSize);
    1044     if(InitBuf[0])
    1045         SetInitGlobalData(GlobalVar[MaxGlobalVarNum].offset,
    1046             GlobalVar[MaxGlobalVarNum].type,
    1047             GlobalVar[MaxGlobalVarNum].u.index,
    1048             GlobalVar[MaxGlobalVarNum].SubScripts,
     1055    if(InitBuf[0]){
     1056        int result = SetInitGlobalData(pVar->offset,
     1057            pVar->type,
     1058            pVar->u.index,
     1059            pVar->SubScripts,
    10491060            InitBuf);
    10501061
    1051 
    1052     //エラー用
    1053     GlobalVar[MaxGlobalVarNum].source_code_address=cp;
    1054 
    1055 
    1056     MaxGlobalVarNum++;
     1062        if(!result){
     1063            //動的な式だった場合は代入演算を行う
     1064            char temporary[8192];
     1065            sprintf(temporary,"%s=%s",name,InitBuf);
     1066            OpcodeCalc(temporary);
     1067        }
     1068    }
     1069
    10571070
    10581071
  • BasicCompiler_Common/calculation.cpp

    r5 r7  
    627627                        //定数
    628628                        /////////
    629                         double dbl;
    630629                        StrPtr[pnum]=0;
    631                         type[pnum]=GetConstValue(Parms,&dbl,temporary,&before_index[pnum]);
    632                         if(type[pnum]!=-1){
     630                        type[pnum] = CDBConst::obj.GetType(Parms);
     631                        if(type[pnum]){
    633632                            if(IsRealNumberType(type[pnum])){
    634633                                //実数型
    635                                 nums[pnum]=dbl;
    636                             }
    637                             else if(Is64Type(type[pnum])){
    638                                 //64ビット整数
    639                                 memcpy(&i64nums[pnum],&dbl,sizeof(_int64));
     634                                nums[pnum] = CDBConst::obj.GetDoubleData(Parms);
    640635                            }
    641636                            else if(IsWholeNumberType(type[pnum])){
    642637                                //整数
    643                                 i64nums[pnum]=(_int64)dbl;
     638                                i64nums[pnum] = CDBConst::obj.GetWholeData(Parms);
    644639                            }
    645                             else if(type[pnum]==DEF_STRING){
     640/*                          else if(type[pnum]==DEF_STRING){
    646641                                //リテラル文字列
    647642
     
    652647                                memcpy(Parms,temporary,(int)nums[pnum]);
    653648                                goto StrLiteral;
    654                             }
     649                            }*/
    655650                            else{
    656651                                //エラー
     
    671666                            if(bDebuggingWatchList) return -1;
    672667                            //エラー
    673                             if(enableerror) SetError(33,NULL,cp);
     668                            if(enableerror) SetError(3,Parms,cp);
    674669                            return 0;
    675670                        }
     
    12041199int IsStrCalculation(char *Command){
    12051200    int i,i2,i3,i4,type,PareNum;
    1206     double dbl;
    12071201    char temporary[VN_SIZE],temp2[8192];
    12081202
     
    13001294
    13011295                //定数
    1302                 i3=GetConstValue(Command+i2,&dbl,temporary,0);
     1296                i3 = CDBConst::obj.GetType(Command+i2);
    13031297                if(i3==DEF_STRING) return 1;    //文字列
    1304                 if(i3!=-1) return 0;            //数値
     1298                if(i3) return 0;            //数値
    13051299
    13061300                //変数
  • BasicCompiler_Common/common.h

    r5 r7  
    1515#include "../BasicCompiler64/CommandValue.h"
    1616#include "../BasicCompiler64/FunctionValue.h"
     17#define OPCODE_H_PATH "../BasicCompiler64/opcode.h"
    1718#else
    1819#include "../BasicCompiler32/resource.h"
    1920#include "../BasicCompiler32/CommandValue.h"
    2021#include "../BasicCompiler32/FunctionValue.h"
     22#define OPCODE_H_PATH "../BasicCompiler32/opcode.h"
    2123#endif
    2224
     
    162164
    163165    DWORD fRef;
     166
     167    //定数変数かどうか
     168    BOOL bConst;
    164169
    165170    BOOL bArray;
Note: See TracChangeset for help on using the changeset viewer.