Changeset f9a0dd0 for src/Parser


Ignore:
Timestamp:
Dec 16, 2024, 9:30:52 PM (3 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
9a01745
Parents:
0497b6ba
Message:

fix length for juxtaposed strings: "ABC" "DEF"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cpp

    r0497b6ba rf9a0dd0  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Sep 12 22:40:35 2024
    13 // Update Count     : 1090
     12// Last Modified On : Mon Dec 16 20:50:27 2024
     13// Update Count     : 1110
    1414//
    1515
     
    162162                lnthSuffix( str, type, ltype );                                 // could have length suffix
    163163        } else {
    164                 // At least one digit in integer constant, so safe to backup while looking for suffix.
    165                 // This declaration and the comma expressions in the conditions mimic
    166                 // the declare and check pattern allowed in later compiler versions.
    167                 // (Only some early compilers/C++ standards do not support it.)
     164                // At least one digit in integer constant, so safe to backup while looking for suffix.  This
     165                // declaration and the comma expressions in the conditions mimic the declare and check
     166                // pattern allowed in later compiler versions.  (Only some early compilers/C++ standards do
     167                // not support it.)
    168168                string::size_type posn;
    169169                // pointer value
     
    465465}
    466466
    467 // A "sequence" is the series of characters in a character/string literal
    468 // that becomes a single character value in the runtime value.
     467// A "sequence" is the series of characters in a character/string literal that becomes a single
     468// character value in the runtime value.
    469469static size_t sequenceLength( const std::string & str, size_t pos ) {
    470470        // Most "sequences" are just a single character, filter those out:
     
    497497                assertf( false, "Unknown escape sequence (start %c).", str[pos] );
    498498                return 1;
    499         }
     499        } // switch
    500500}
    501501
    502 ast::Expr * build_constantStr(
    503                 const CodeLocation & location,
    504                 string & str ) {
     502ast::Expr * build_constantStr( const CodeLocation & location, string & str ) {
    505503        assert( str.length() > 0 );
    506504        string units;                                                                           // units
     
    525523        } // switch
    526524
    527         // The dimension value of the type is equal to the number of "sequences"
    528         // not including the openning and closing quotes in the literal plus 1
    529         // for the implicit null terminator.
    530         size_t dimension = 1;
    531         for ( size_t pos = 1 ; pos < str.size() - 1 ;
    532                         pos += sequenceLength( str, pos ) ) {
    533                 dimension += 1;
    534         }
     525        // The length value of the type is equal to the number of "sequences" not including the openning
     526        // and closing quotes in the literal plus 1 for the implicit null terminator.
     527        size_t length = 1;
     528        for ( size_t pos = 1 ; pos < str.size() - 1 ; pos += sequenceLength( str, pos ) ) {
     529                if ( '"' == str[pos] ) {                                                // concatenated strings ? "ABC" "DEF"
     530                        int cnt = 1;                                                            // skip outside quotes and space between
     531                        for ( unsigned int i = pos + 1; str[i] != '"'; i += 1, cnt += 1 );
     532                        pos += cnt;
     533                        continue;                                                                       // not part of length
     534                } // if
     535                length += 1;
     536        } // for
    535537
    536538        ast::ArrayType * at = new ast::ArrayType(
    537539                strtype,
    538                 ast::ConstantExpr::from_ulong( location, dimension ),
     540                ast::ConstantExpr::from_ulong( location, length ),
    539541                ast::FixedLen,
    540542                ast::DynamicDim );
    541543        ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
    542544        if ( units.length() != 0 ) {
    543                 ret = new ast::UntypedExpr( location,
    544                         new ast::NameExpr( location, units ),
    545                         { ret } );
     545                ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ),     { ret } );
    546546        } // if
    547547
Note: See TracChangeset for help on using the changeset viewer.