Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    re612146c rea6332d  
    77// ExpressionNode.cc --
    88//
    9 // Author           : Peter A. Buhr
     9// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Sep  3 22:21:21 2017
    13 // Update Count     : 639
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed Aug  2 11:12:00 2017
     13// Update Count     : 568
    1414//
    1515
     
    5858static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    5959
    60 static 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 
    68 Expression * build_constantInteger( string & str ) {
     60Expression * build_constantInteger( const std::string & str ) {
    6961        static const BasicType::Kind kind[2][3] = {
    7062                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
    7163                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
    7264        };
    73 
    74         string units;                                                                           // units
    75         sepNumeric( str, units );                                                       // separate constant from units
    76 
    7765        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
    7866        int size;                                                                                       // 0 => int, 1 => long, 2 => long long
     
    8169        Expression * ret;
    8270
    83         // ROB: what do we do with units on 0 and 1?
    8471        // special constants
    8572        if ( str == "0" ) {
     
    147134        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][size] ), str, v ) );
    148135  CLEANUP:
    149         if ( units.length() != 0 ) {
    150                 ret = new UntypedExpr( new NameExpr( units ), { ret } );
    151         } // if
    152 
    153136        delete &str;                                                                            // created by lex
    154137        return ret;
    155138} // build_constantInteger
    156139
    157 Expression * build_constantFloat( string & str ) {
     140Expression * build_constantFloat( const std::string & str ) {
    158141        static const BasicType::Kind kind[2][3] = {
    159142                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
    160143                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
    161144        };
    162 
    163         string units;                                                                           // units
    164         sepNumeric( str, units );                                                       // separate constant from units
    165145
    166146        bool complx = false;                                                            // real, complex
     
    189169
    190170        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][size] ), str, v ) );
    191         if ( units.length() != 0 ) {
    192                 ret = new UntypedExpr( new NameExpr( units ), { ret } );
    193         } // if
    194 
    195171        delete &str;                                                                            // created by lex
    196172        return ret;
    197173} // build_constantFloat
    198174
    199 static void sepString( string & str, string & units, char delimit ) {
    200         string::size_type posn = str.find_last_of( delimit ) + 1;
    201         if ( posn != str.length() ) {
    202                 units = "?" + str.substr( posn );                               // extract units
    203                 str.erase( posn );                                                              // remove units
    204         } // if
    205 } // sepString
    206 
    207 Expression * build_constantChar( string & str ) {
    208         string units;                                                                           // units
    209         sepString( str, units, '\'' );                                          // separate constant from units
    210 
     175Expression * build_constantChar( const std::string & str ) {
    211176        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
    212         if ( units.length() != 0 ) {
    213                 ret = new UntypedExpr( new NameExpr( units ), { ret } );
    214         } // if
    215 
    216177        delete &str;                                                                            // created by lex
    217178        return ret;
    218179} // build_constantChar
    219180
    220 Expression * build_constantStr( string & str ) {
    221         string units;                                                                           // units
    222         sepString( str, units, '"' );                                           // separate constant from units
    223 
    224         BasicType::Kind strtype = BasicType::Char;                      // default string type
    225         switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string ""
    226           case 'u':
    227                 if ( str[1] == '8' ) break;                                             // utf-8 characters
    228                 strtype = BasicType::ShortUnsignedInt;
    229                 break;
    230           case 'U':
    231                 strtype = BasicType::UnsignedInt;
    232                 break;
    233           case 'L':
    234                 strtype = BasicType::SignedInt;
    235                 break;
    236         } // switch
    237         ArrayType * at = new ArrayType( noQualifiers, new BasicType( Type::Qualifiers( Type::Const ), strtype ),
    238                                                                         new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
    239                                                                         false, false );
    240         Expression * ret = new ConstantExpr( Constant( at, str, (unsigned long long int)0 ) ); // constant 0 is ignored for pure string value
    241         if ( units.length() != 0 ) {
    242                 ret = new UntypedExpr( new NameExpr( units ), { ret } );
    243         } // if
    244 
     181ConstantExpr * 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
    245187        delete &str;                                                                            // created by lex
    246188        return ret;
    247189} // build_constantStr
    248190
    249 Expression * build_field_name_FLOATINGconstant( const string & str ) {
     191Expression * build_field_name_FLOATINGconstant( const std::string & str ) {
    250192        // str is of the form A.B -> separate at the . and return member expression
    251193        int a, b;
    252194        char dot;
    253         stringstream ss( str );
     195        std::stringstream ss( str );
    254196        ss >> a >> dot >> b;
    255197        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
     
    265207                } else {
    266208                        return new UntypedMemberExpr( fracts, fieldName );
    267                 } // if
    268         } // if
     209                }
     210        }
    269211        return fieldName;
    270212} // make_field_name_fraction_constants
     
    274216} // build_field_name_fraction_constants
    275217
    276 Expression * build_field_name_REALFRACTIONconstant( const string & str ) {
     218Expression * build_field_name_REALFRACTIONconstant( const std::string & str ) {
    277219        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) throw SemanticError( "invalid tuple index " + str );
    278         Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
     220        Expression * ret = build_constantInteger( *new std::string( str.substr(1) ) );
    279221        delete &str;
    280222        return ret;
    281223} // build_field_name_REALFRACTIONconstant
    282224
    283 Expression * build_field_name_REALDECIMALconstant( const string & str ) {
     225Expression * build_field_name_REALDECIMALconstant( const std::string & str ) {
    284226        if ( str[str.size()-1] != '.' ) throw SemanticError( "invalid tuple index " + str );
    285         Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
     227        Expression * ret = build_constantInteger( *new std::string( str.substr( 0, str.size()-1 ) ) );
    286228        delete &str;
    287229        return ret;
     
    294236} // build_varref
    295237
    296 // TODO: get rid of this and OperKinds and reuse code from OperatorTable
     238
    297239static const char * OperName[] = {                                              // must harmonize with OperKinds
    298240        // diadic
     
    302244        "?[?]", "...",
    303245        // monadic
    304         "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--",
     246        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
    305247}; // OperName
    306248
     
    365307
    366308Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
    367         list< Expression * > args;
     309        std::list< Expression * > args;
    368310        args.push_back( maybeMoveBuild< Expression >(expr_node) );
    369311        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     
    371313
    372314Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
    373         list< Expression * > args;
    374         args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
     315        std::list< Expression * > args;
     316        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node) ) );
    375317        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    376318} // build_unary_ptr
    377319
    378320Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
    379         list< Expression * > args;
     321        std::list< Expression * > args;
    380322        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
    381323        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
     
    384326
    385327Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
    386         list< Expression * > args;
    387         args.push_back( maybeMoveBuild< Expression >(expr_node1) );
     328        std::list< Expression * > args;
     329        args.push_back( new AddressExpr( maybeMoveBuild< Expression >(expr_node1) ) );
    388330        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
    389331        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     
    407349
    408350Expression * build_tuple( ExpressionNode * expr_node ) {
    409         list< Expression * > exprs;
     351        std::list< Expression * > exprs;
    410352        buildMoveList( expr_node, exprs );
    411353        return new UntypedTupleExpr( exprs );;
     
    413355
    414356Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
    415         list< Expression * > args;
     357        std::list< Expression * > args;
    416358        buildMoveList( expr_node, args );
    417359        return new UntypedExpr( maybeMoveBuild< Expression >(function), args, nullptr );
     
    422364} // build_range
    423365
    424 Expression * build_asmexpr( ExpressionNode * inout, Expression * constraint, ExpressionNode * operand ) {
     366Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand ) {
    425367        return new AsmExpr( maybeMoveBuild< Expression >( inout ), constraint, maybeMoveBuild< Expression >(operand) );
    426368} // build_asmexpr
Note: See TracChangeset for help on using the changeset viewer.