Ignore:
Timestamp:
Mar 19, 2011, 1:29:12 AM (13 years ago)
Author:
イグトランス (egtra)
Message:

LexicalAnalyzer周りの修正

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/egtra/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Const.cpp

    r803 r814  
    8585
    8686    int i2;
    87     char temporary[1024];
    8887
    8988    // 名前空間管理
     
    9190    namespaceScopes.clear();
    9291
    93     for(int i=0;;i++){
     92    for (int i = 0; ; ++i)
     93    {
    9494        if( source[i] == '\0' ) break;
    9595
    9696        if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
    97             for(i+=2,i2=0;;i2++,i++){
    98                 if( IsCommandDelimitation( source[i] ) ){
    99                     temporary[i2]=0;
    100                     break;
    101                 }
    102                 temporary[i2]=source[i];
    103             }
    104             namespaceScopes.push_back( temporary );
     97            i+=2;
     98            char const* p = &source[i];
     99            while (!IsCommandDelimitation(source[i]))
     100            {
     101                ++i;
     102            }
     103            namespaceScopes.push_back(std::string(p, &source[i]));
    105104
    106105            continue;
     
    131130                }
    132131
     132                char const* beginTemp = &source[i];
     133                char const* endTemp = &source[i];
     134
    133135                for(i2=0;;i++,i2++){
    134136                    if(source[i]=='\"'){
    135                         temporary[i2]=source[i];
    136137                        for(i++,i2++;;i++,i2++){
    137                             temporary[i2]=source[i];
    138138                            if(source[i]=='\"') break;
    139139                        }
     
    141141                    }
    142142                    if(IsCommandDelimitation(source[i])){
    143                         temporary[i2]=0;
     143                        endTemp = beginTemp + i2;
    144144                        break;
    145145                    }
    146                     temporary[i2]=source[i];
    147146                }
    148147
    149148                //名前を取得
    150                 char name[VN_SIZE];
     149                char const* nameEnd;
    151150                for(i2=0;;i2++){
    152                     if(temporary[i2]=='\0'){
     151                    if(beginTemp[i2]=='\0'){
    153152                        compiler.errorMessenger.Output(10,"Const",cp);
    154153                        return;
    155154                    }
    156                     if(temporary[i2]=='='||temporary[i2]=='('){
    157                         name[i2]=0;
     155                    if(beginTemp[i2]=='='||beginTemp[i2]=='('){
     156                        nameEnd = beginTemp + i2;
    158157                        break;
    159158                    }
    160                     name[i2]=temporary[i2];
    161                 }
     159                }
     160                std::string name(beginTemp, nameEnd);
    162161
    163162                //重複チェック
     
    165164                    || compiler.GetObjectModule().meta.GetGlobalConsts().IsExistDuplicationKeyName( name ) )
    166165                {
    167                     compiler.errorMessenger.Output(15,name,cp);
     166                    compiler.errorMessenger.Output(15, name, cp);
    168167                    return;
    169168                }
    170169
    171                 if( temporary[i2] == '=' )
     170                if( beginTemp[i2] == '=' )
    172171                {
    173172                    // 定数
    174                     const char *expression = temporary + i2 + 1;
     173                    std::string expression(beginTemp + i2 + 1, endTemp);
    175174
    176175                    _int64 i64data;
    177176                    Type resultType;
    178                     if( StaticCalculation(false, expression, 0, &i64data, resultType) )
     177                    if( StaticCalculation(false, expression.c_str(), 0, &i64data, resultType) )
    179178                    {
    180179                        consts.Add( Symbol( namespaceScopes, name ), i64data, resultType );
     
    184183                {
    185184                    // 定数マクロ
    186                     const char *params = temporary + i2;
    187                     if( !constMacros.Add( Symbol( namespaceScopes, name ), params ) )
     185                    std::string params(beginTemp + i2, endTemp);
     186                    if( !constMacros.Add( Symbol( namespaceScopes, name ), params.c_str() ) )
    188187                    {
    189188                        compiler.errorMessenger.Output( 1, NULL, i );
     
    208207}
    209208
    210 bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest )
    211 {
    212     extern HANDLE hHeap;
    213     int i2,i3,i4,num;
     209bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest, std::size_t destSize )
     210{
     211    std::string s;
     212    auto ret = ConstMacroToExpression(constMacro, parameterStr, s);
     213    strcpy_s(dest, destSize, s.c_str());
     214    return ret;
     215}
     216
     217bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, std::string& dest )
     218{
     219    int i2,i3;
    214220    char temporary[VN_SIZE];
    215     char *pParms[MAX_PARMS];
    216     num=0;
     221    std::vector<std::string> Parms;
     222    dest.reserve(8192);
    217223    i2=0;
    218224    while(1){
    219225        i2=GetOneParameter(parameterStr,i2,temporary);
    220226
    221         pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
    222         lstrcpy(pParms[num],temporary);
    223 
    224         num++;
     227        Parms.push_back(temporary);
     228
    225229        if(parameterStr[i2]=='\0') break;
    226230    }
    227     if( num != constMacro.GetParameters().size() ){
     231    if( Parms.size() != constMacro.GetParameters().size() ){
    228232        extern int cp;
    229         for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
    230233        compiler.errorMessenger.Output(10,constMacro.GetName().c_str(),cp);
    231         lstrcpy(dest,"0");
     234        dest = '0';
    232235        return true;
    233236    }
    234237
    235238    i2=0;
    236     i4=0;
    237239    while(1){
    238240
     
    253255        if( i3 == (int)constMacro.GetParameters().size() ){
    254256            //パラメータでないとき
    255             lstrcpy(dest+i4,temporary);
    256             i4+=lstrlen(temporary);
     257            dest += temporary;
    257258        }
    258259        else{
    259260            //パラメータのとき
    260             lstrcpy(dest+i4,pParms[i3]);
    261             i4+=lstrlen(pParms[i3]);
     261            dest += Parms[i3];
    262262        }
    263263
    264264        //演算子をコピー
    265         for(;;i2++,i4++){
     265        for(;;i2++){
    266266            if( constMacro.GetExpression()[i2] == 1 ){
    267                 dest[i4++] = constMacro.GetExpression()[i2++];
    268                 dest[i4] = constMacro.GetExpression()[i2];
     267                dest += constMacro.GetExpression()[i2++];
     268                dest += constMacro.GetExpression()[i2];
    269269                continue;
    270270            }
    271271            if(IsVariableTopChar( constMacro.GetExpression()[i2] )) break;
    272             dest[i4] = constMacro.GetExpression()[i2];
     272            dest += constMacro.GetExpression()[i2];
    273273            if( constMacro.GetExpression()[i2] == '\0' ) break;
    274274        }
     
    277277    }
    278278
    279     for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
    280 
    281279    return true;
    282280}
Note: See TracChangeset for help on using the changeset viewer.