Ignore:
Timestamp:
Aug 31, 2017, 10:30:37 PM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
51c6353, e8ccca3
Parents:
2ad507b8
Message:

first attempt at user-defined constants (units)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r2ad507b8 r76c62b2  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Aug  2 11:12:00 2017
    13 // Update Count     : 568
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Thu Aug 31 21:05:04 2017
     13// Update Count     : 605
    1414//
    1515
     
    5858static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    5959
    60 Expression * build_constantInteger( const std::string & str ) {
     60static void sepNumeric( string & str, string & units ) {
     61        string::size_type posn = str.find_first_of( "`" );
     62        if ( posn != string::npos ) {
     63                units = str.substr( posn );                                             // extract units
     64                str.erase( posn );                                                              // remove units
     65        } // if
     66} // sepNumeric
     67
     68Expression * build_constantInteger( std::string & str ) {
    6169        static const BasicType::Kind kind[2][3] = {
    6270                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
    6371                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
    6472        };
     73
     74        string units;                                                                           // units
     75        sepNumeric( str, units );                                                       // separate constant from units
     76
    6577        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
    6678        int size;                                                                                       // 0 => int, 1 => long, 2 => long long
     
    6981        Expression * ret;
    7082
     83        // ROB: what do we do with units on 0 and 1?
    7184        // special constants
    7285        if ( str == "0" ) {
     
    132145        } // if
    133146
    134         ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
     147//      if ( units.length() == 0 ) {
     148                ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
     149//      } else {
     150//              // ROB: generate call to units routine
     151//              ret = nullptr;
     152//      } // if
    135153  CLEANUP:
    136154        delete &str;                                                                            // created by lex
     
    138156} // build_constantInteger
    139157
    140 Expression * build_constantFloat( const std::string & str ) {
     158Expression * build_constantFloat( std::string & str ) {
    141159        static const BasicType::Kind kind[2][3] = {
    142160                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
    143161                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
    144162        };
     163
     164        string units;                                                                           // units
     165        sepNumeric( str, units );                                                       // separate constant from units
    145166
    146167        bool complx = false;                                                            // real, complex
     
    168189        } // if
    169190
    170         Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
     191        Expression * ret;
     192//      if ( units.length() == 0 ) {
     193                ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
     194//      } else {
     195//              ret = nullptr;
     196//              // ROB: generate call to units routine
     197//      } // if
     198
    171199        delete &str;                                                                            // created by lex
    172200        return ret;
    173201} // build_constantFloat
    174202
    175 Expression * build_constantChar( const std::string & str ) {
    176         Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
     203static void sepString( string & str, string & units, char delimit ) {
     204        string::size_type posn = str.find_last_of( delimit ) + 1;
     205        if ( posn != str.length() ) {
     206                units = str.substr( posn );                                             // extract units
     207                str.erase( posn );                                                              // remove units
     208        } // if
     209} // sepString
     210
     211Expression * build_constantChar( std::string & str ) {
     212        string units;                                                                           // units
     213        sepString( str, units, '\'' );                                          // separate constant from units
     214
     215        Expression * ret;
     216//      if ( units.length() == 0 ) {
     217                ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
     218//      } else {
     219//              ret = nullptr;
     220//              // ROB: generate call to units routine
     221//      } // if
     222
    177223        delete &str;                                                                            // created by lex
    178224        return ret;
    179225} // build_constantChar
    180226
    181 ConstantExpr * build_constantStr( const std::string & str ) {
    182         // string should probably be a primitive type
    183         ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
    184                                                                    new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
    185                                                                    false, false );
    186         ConstantExpr * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
     227ConstantExpr * build_constantStr( std::string & str ) {
     228        string units;                                                                           // units
     229        sepString( str, units, '"' );                                           // separate constant from units
     230
     231        ConstantExpr * ret;
     232//      if ( units.length() == 0 ) {
     233                // string should probably be a primitive type
     234                ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ),
     235                                                                                new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
     236                                                                                false, false );
     237                ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
     238//      } else {
     239//              ret = nullptr;
     240//              // ROB: generate call to units routine
     241//      } // if
     242               
    187243        delete &str;                                                                            // created by lex
    188244        return ret;
Note: See TracChangeset for help on using the changeset viewer.