Changes in / [c967ef9:b44a7c5]


Ignore:
Location:
src/Parser
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.cc

    rc967ef9 rb44a7c5  
    1010// Created On       : Sat May 16 13:26:29 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jul 24 02:17:01 2016
    13 // Update Count     : 90
     12// Last Modified On : Fri Jul 15 18:49:25 2016
     13// Update Count     : 62
    1414//
    1515
     
    2929// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
    3030// type.
    31 
    32 static Type::Qualifiers emptyQualifiers;                                // no qualifiers on constants
    3331
    3432static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }
     
    3937static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }
    4038
    41 ConstantNode *makeConstantInteger( std::string & str ) {
    42         static const BasicType::Kind kind[2][3] = {
    43                 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
    44                 { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
    45         };
    46         bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
    47         int size;                                                                                       // 0 => int, 1 => long, 2 => long long
    48         unsigned long long v;                                                           // converted integral value
    49         size_t last = str.length() - 1;                                         // last character of constant
    50 
    51         if ( str[0] == '0' ) {                                                          // octal/hex constant ?
    52                 dec = false;
    53                 if ( last != 0 && checkX( str[1] ) ) {                  // hex constant ?
    54                         sscanf( (char *)str.c_str(), "%llx", &v );
    55                         //printf( "%llx %llu\n", v, v );
    56                 } else {                                                                                // octal constant
    57                         sscanf( (char *)str.c_str(), "%llo", &v );
    58                         //printf( "%llo %llu\n", v, v );
     39BasicType::Kind literalType( ConstantNode::Type type, string &value ) {
     40        BasicType::Kind btype;
     41
     42        // lexing divides constants into 4 kinds
     43        switch ( type ) {
     44          case ConstantNode::Integer:
     45                {
     46                        static const BasicType::Kind kind[2][3] = {
     47                                { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },
     48                                { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },
     49                        };
     50                        bool dec = true, Unsigned = false;                      // decimal, unsigned constant
     51                        int size;                                                                       // 0 => int, 1 => long, 2 => long long
     52                        unsigned long long v;                                           // converted integral value
     53                        size_t last = value.length() - 1;                       // last character of constant
     54
     55                        if ( value[0] == '0' ) {                                        // octal/hex constant ?
     56                                dec = false;
     57                                if ( last != 0 && checkX( value[1] ) ) { // hex constant ?
     58                                        sscanf( (char *)value.c_str(), "%llx", &v );
     59                                        //printf( "%llx %llu\n", v, v );
     60                                } else {                                                                // octal constant
     61                                        sscanf( (char *)value.c_str(), "%llo", &v );
     62                                        //printf( "%llo %llu\n", v, v );
     63                                } // if
     64                        } else {                                                                        // decimal constant ?
     65                                sscanf( (char *)value.c_str(), "%llu", &v );
     66                                //printf( "%llu %llu\n", v, v );
     67                        } // if
     68
     69                        if ( v <= INT_MAX ) {                                           // signed int
     70                                size = 0;
     71                        } else if ( v <= UINT_MAX && ! dec ) {          // unsigned int
     72                                size = 0;
     73                                Unsigned = true;                                                // unsigned
     74                        } else if ( v <= LONG_MAX ) {                           // signed long int
     75                                size = 1;
     76                        } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
     77                                size = 1;
     78                                Unsigned = true;                                                // unsigned long int
     79                        } else if ( v <= LLONG_MAX ) {                          // signed long long int
     80                                size = 2;
     81                        } else {                                                                        // unsigned long long int
     82                                size = 2;
     83                                Unsigned = true;                                                // unsigned long long int
     84                        } // if
     85
     86                        if ( checkU( value[last] ) ) {                          // suffix 'u' ?
     87                                Unsigned = true;
     88                                if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'l' ?
     89                                        size = 1;
     90                                        if ( last > 1 && checkL( value[ last - 2 ] ) ) { // suffix 'll' ?
     91                                                size = 2;
     92                                        } // if
     93                                } // if
     94                        } else if ( checkL( value[ last ] ) ) {         // suffix 'l' ?
     95                                size = 1;
     96                                if ( last > 0 && checkL( value[ last - 1 ] ) ) { // suffix 'll' ?
     97                                        size = 2;
     98                                        if ( last > 1 && checkU( value[ last - 2 ] ) ) { // suffix 'u' ?
     99                                                Unsigned = true;
     100                                        } // if
     101                                } else {
     102                                        if ( last > 0 && checkU( value[ last - 1 ] ) ) { // suffix 'u' ?
     103                                                Unsigned = true;
     104                                        } // if
     105                                } // if
     106                        } // if
     107                        btype = kind[Unsigned][size];                           // lookup constant type
     108                        break;
     109                }
     110          case ConstantNode::Float:
     111                {
     112                        //long double v;
     113                        static const BasicType::Kind kind[2][3] = {
     114                                { BasicType::Float, BasicType::Double, BasicType::LongDouble },
     115                                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
     116                        };
     117                        bool complx = false;                                            // real, complex
     118                        int size = 1;                                                           // 0 => float, 1 => double (default), 2 => long double
     119                        // floating-point constant has minimum of 2 characters: 1. or .1
     120                        size_t last = value.length() - 1;
     121
     122                        if ( checkI( value[last] ) ) {                          // imaginary ?
     123                                complx = true;
     124                                last -= 1;                                                              // backup one character
     125                        } // if
     126
     127                        //sscanf( (char *)value.c_str(), "%Lf", &v );
     128                        //printf( "%s %24.22Lf %Lf\n", value.c_str(), v, v );
     129
     130                        if ( checkF( value[last] ) ) {                          // float ?
     131                                size = 0;
     132                        } else if ( checkD( value[last] ) ) {           // double ?
     133                                size = 1;
     134                        } else if ( checkL( value[last] ) ) {           // long double ?
     135                                size = 2;
     136                        } // if
     137                        if ( ! complx && checkI( value[last - 1] ) ) { // imaginary ?
     138                                complx = true;
     139                        } // if
     140                        btype = kind[complx][size];                                     // lookup constant type
     141                        break;
     142                }
     143          case ConstantNode::Character:
     144                btype = BasicType::Char;                                                // default
     145                if ( string( "LUu" ).find( value[0] ) != string::npos ) {
     146                        // ???
    59147                } // if
    60         } else {                                                                                        // decimal constant ?
    61                 sscanf( (char *)str.c_str(), "%llu", &v );
    62                 //printf( "%llu %llu\n", v, v );
    63         } // if
    64 
    65         if ( v <= INT_MAX ) {                                                           // signed int
    66                 size = 0;
    67         } else if ( v <= UINT_MAX && ! dec ) {                          // unsigned int
    68                 size = 0;
    69                 Unsigned = true;                                                                // unsigned
    70         } else if ( v <= LONG_MAX ) {                                           // signed long int
    71                 size = 1;
    72         } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
    73                 size = 1;
    74                 Unsigned = true;                                                                // unsigned long int
    75         } else if ( v <= LLONG_MAX ) {                                          // signed long long int
    76                 size = 2;
    77         } else {                                                                                        // unsigned long long int
    78                 size = 2;
    79                 Unsigned = true;                                                                // unsigned long long int
    80         } // if
    81 
    82         if ( checkU( str[last] ) ) {                                            // suffix 'u' ?
    83                 Unsigned = true;
    84                 if ( last > 0 && checkL( str[last - 1] ) ) {    // suffix 'l' ?
    85                         size = 1;
    86                         if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?
    87                                 size = 2;
     148                break;
     149          case ConstantNode::String:
     150                assert( false );
     151                // array of char
     152                if ( string( "LUu" ).find( value[0] ) != string::npos ) {
     153                        if ( value[0] == 'u' && value[1] == '8' ) {
     154                                // ???
     155                        } else {
     156                                // ???
    88157                        } // if
    89158                } // if
    90         } else if ( checkL( str[ last ] ) ) {                           // suffix 'l' ?
    91                 size = 1;
    92                 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'll' ?
    93                         size = 2;
    94                         if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?
    95                                 Unsigned = true;
    96                         } // if
    97                 } else {
    98                         if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
    99                                 Unsigned = true;
    100                         } // if
    101                 } // if
    102         } // if
    103 
    104         return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ), nullptr ) );
    105 } // makeConstantInteger
    106 
    107 ConstantNode *makeConstantFloat( std::string & str ) {
    108         static const BasicType::Kind kind[2][3] = {
    109                 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
    110                 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
    111         };
    112 
    113         bool complx = false;                                                            // real, complex
    114         int size = 1;                                                                           // 0 => float, 1 => double (default), 2 => long double
    115         // floating-point constant has minimum of 2 characters: 1. or .1
    116         size_t last = str.length() - 1;
    117 
    118         if ( checkI( str[last] ) ) {                                            // imaginary ?
    119                 complx = true;
    120                 last -= 1;                                                                              // backup one character
    121         } // if
    122 
    123         if ( checkF( str[last] ) ) {                                            // float ?
    124                 size = 0;
    125         } else if ( checkD( str[last] ) ) {                                     // double ?
    126                 size = 1;
    127         } else if ( checkL( str[last] ) ) {                                     // long double ?
    128                 size = 2;
    129         } // if
    130         if ( ! complx && checkI( str[last - 1] ) ) {            // imaginary ?
    131                 complx = true;
    132         } // if
    133 
    134         return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ), nullptr ) );
    135 } // makeConstantFloat
    136 
    137 ConstantNode *makeConstantChar( std::string & str ) {
    138         return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ), nullptr ) );
    139 } // makeConstantChar
    140 
    141 ConstantNode *makeConstantStr( std::string & str ) {
     159                break;
     160        } // switch
     161        return btype;
     162} // literalType
     163
     164
     165ConstantNode *makeConstant( ConstantNode::Type type, std::string *str ) {
     166        ::Type::Qualifiers emptyQualifiers;                                     // no qualifiers on constants
     167        return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, literalType( type, *str ) ), *str ), nullptr ) );
     168}
     169
     170ConstantNode *makeConstantStr( ConstantNode::Type type, std::string *str ) {
     171        ::Type::Qualifiers emptyQualifiers;                                     // no qualifiers on constants
    142172        // string should probably be a primitive type
    143173        ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ),
    144174                                                                   new ConstantExpr(
    145175                                                                           Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ),
    146                                                                                                  toString( str.size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
     176                                                                                                 toString( str->size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
    147177                                                                   false, false );
    148         return new ConstantNode( new ConstantExpr( Constant( at, str ), nullptr ) );
    149 } // makeConstantStr
     178        return new ConstantNode( new ConstantExpr( Constant( at, *str ), nullptr ) );
     179}
    150180
    151181
  • src/Parser/ParseNode.h

    rc967ef9 rb44a7c5  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jul 24 02:17:00 2016
    13 // Update Count     : 269
     12// Last Modified On : Tue Jul 12 20:50:21 2016
     13// Update Count     : 261
    1414//
    1515
     
    141141};
    142142
    143 ConstantNode *makeConstantInteger( std::string & );
    144 ConstantNode *makeConstantFloat( std::string & );
    145 ConstantNode *makeConstantChar( std::string & );
    146 ConstantNode *makeConstantStr( std::string & );
     143ConstantNode *makeConstant( ConstantNode::Type, std::string * );
     144ConstantNode *makeConstantStr( ConstantNode::Type type, std::string *str );
    147145
    148146class VarRefNode : public ExpressionNode {
  • src/Parser/parser.cc

    rc967ef9 rb44a7c5  
    52355235/* Line 1806 of yacc.c  */
    52365236#line 305 "parser.yy"
    5237     { (yyval.constant) = makeConstantInteger( *(yyvsp[(1) - (1)].tok) ); }
     5237    { (yyval.constant) = makeConstant( ConstantNode::Integer, (yyvsp[(1) - (1)].tok) ); }
    52385238    break;
    52395239
     
    52425242/* Line 1806 of yacc.c  */
    52435243#line 306 "parser.yy"
    5244     { (yyval.constant) = makeConstantFloat( *(yyvsp[(1) - (1)].tok) ); }
     5244    { (yyval.constant) = makeConstant( ConstantNode::Float, (yyvsp[(1) - (1)].tok) ); }
    52455245    break;
    52465246
     
    52495249/* Line 1806 of yacc.c  */
    52505250#line 307 "parser.yy"
    5251     { (yyval.constant) = makeConstantChar( *(yyvsp[(1) - (1)].tok) ); }
     5251    { (yyval.constant) = makeConstant( ConstantNode::Character, (yyvsp[(1) - (1)].tok) ); }
    52525252    break;
    52535253
     
    52565256/* Line 1806 of yacc.c  */
    52575257#line 332 "parser.yy"
    5258     { (yyval.constant) = makeConstantStr( *(yyvsp[(1) - (1)].tok) ); }
     5258    { (yyval.constant) = makeConstantStr( ConstantNode::String, (yyvsp[(1) - (1)].tok) ); }
    52595259    break;
    52605260
  • src/Parser/parser.yy

    rc967ef9 rb44a7c5  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul 23 17:01:30 2016
    13 // Update Count     : 1668
     12// Last Modified On : Tue Jul 12 20:52:53 2016
     13// Update Count     : 1661
    1414//
    1515
     
    303303constant:
    304304                // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
    305 INTEGERconstant                                                                 { $$ = makeConstantInteger( *$1 ); }
    306         | FLOATINGconstant                                                      { $$ = makeConstantFloat( *$1 ); }
    307         | CHARACTERconstant                                                     { $$ = makeConstantChar( *$1 ); }
     305INTEGERconstant                                                                 { $$ = makeConstant( ConstantNode::Integer, $1 ); }
     306        | FLOATINGconstant                                                      { $$ = makeConstant( ConstantNode::Float, $1 ); }
     307        | CHARACTERconstant                                                     { $$ = makeConstant( ConstantNode::Character, $1 ); }
    308308        ;
    309309
     
    330330
    331331string_literal_list:                                                                    // juxtaposed strings are concatenated
    332         STRINGliteral                                                           { $$ = makeConstantStr( *$1 ); }
     332        STRINGliteral                                                           { $$ = makeConstantStr( ConstantNode::String, $1 ); }
    333333        | string_literal_list STRINGliteral                     { $$ = $1->appendstr( $2 ); }
    334334        ;
Note: See TracChangeset for help on using the changeset viewer.