Ignore:
Timestamp:
Mar 10, 2023, 4:05:50 PM (16 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
85a95cc
Parents:
fed03b3
Message:

Preventing NullStmts? from being added into ForCtrl? ended up being a larger rework as some timing issues caused problems. However, the NullStmts? are mostly gone now along with the extra hosting they caused.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/parser.yy

    rfed03b3 r1cdc052  
    206206#define MISSING_HIGH "Missing high value for down-to range so index is uninitialized."
    207207
     208static ForCtrl * makeForCtrl(
     209                DeclarationNode * init,
     210                enum OperKinds compop,
     211                ExpressionNode * comp,
     212                ExpressionNode * inc ) {
     213        // Wrap both comp/inc if they are non-null.
     214        if ( comp ) comp = new ExpressionNode( build_binary_val(
     215                compop,
     216                new ExpressionNode( build_varref( new string( *init->name ) ) ),
     217                comp ) );
     218        if ( inc ) inc = new ExpressionNode( build_binary_val(
     219                // choose += or -= for upto/downto
     220                compop == OperKinds::LThan || compop == OperKinds::LEThan ? OperKinds::PlusAssn : OperKinds::MinusAssn,
     221                new ExpressionNode( build_varref( new string( *init->name ) ) ),
     222                inc ) );
     223        // The StatementNode call frees init->name, it must happen later.
     224        return new ForCtrl( new StatementNode( init ), comp, inc );
     225}
     226
    208227ForCtrl * forCtrl( DeclarationNode * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
    209228        if ( index->initializer ) {
     
    213232                SemanticError( yylloc, "Multiple loop indexes disallowed in for-loop declaration." );
    214233        } // if
    215         return new ForCtrl( index->addInitializer( new InitializerNode( start ) ),
    216                 // NULL comp/inc => leave blank
    217                 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index->name ) ) ), comp ) ) : nullptr,
    218                 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto
    219                                                         OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index->name ) ) ), inc ) ) : nullptr );
     234        DeclarationNode * initDecl = index->addInitializer( new InitializerNode( start ) );
     235        return makeForCtrl( initDecl, compop, comp, inc );
    220236} // forCtrl
    221237
     
    225241                type = new ExpressionNode( new CastExpr( maybeMoveBuild( type ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
    226242        } // if
    227 //      type = new ExpressionNode( build_func( new ExpressionNode( build_varref( new string( "__for_control_index_constraints__" ) ) ), type ) );
    228         return new ForCtrl(
    229                 distAttr( DeclarationNode::newTypeof( type, true ), DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) ) ),
    230                 // NULL comp/inc => leave blank
    231                 comp ? new ExpressionNode( build_binary_val( compop, new ExpressionNode( build_varref( new string( *index ) ) ), comp ) ) : nullptr,
    232                 inc ? new ExpressionNode( build_binary_val( compop == OperKinds::LThan || compop == OperKinds::LEThan ? // choose += or -= for upto/downto
    233                                                         OperKinds::PlusAssn : OperKinds::MinusAssn, new ExpressionNode( build_varref( new string( *index ) ) ), inc ) ) : nullptr );
     243        DeclarationNode * initDecl = distAttr(
     244                DeclarationNode::newTypeof( type, true ),
     245                DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) )
     246        );
     247        return makeForCtrl( initDecl, compop, comp, inc );
    234248} // forCtrl
    235249
     
    12981312                { $$ = new StatementNode( build_do_while( $5, maybe_build_compound( $2 ), $8 ) ); }
    12991313        | FOR '(' ')' statement                                                         %prec THEN // CFA => for ( ;; )
    1300                 { $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) ); }
     1314                { $$ = new StatementNode( build_for( new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( $4 ) ) ); }
    13011315        | FOR '(' ')' statement ELSE statement                          // CFA
    13021316                {
    1303                         $$ = new StatementNode( build_for( new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr ), maybe_build_compound( $4 ) ) );
     1317                        $$ = new StatementNode( build_for( new ForCtrl( nullptr, nullptr, nullptr ), maybe_build_compound( $4 ) ) );
    13041318                        SemanticWarning( yylloc, Warning::SuperfluousElse );
    13051319                }
     
    13351349for_control_expression:
    13361350        ';' comma_expression_opt ';' comma_expression_opt
    1337                 { $$ = new ForCtrl( (ExpressionNode * )nullptr, $2, $4 ); }
     1351                { $$ = new ForCtrl( nullptr, $2, $4 ); }
    13381352        | comma_expression ';' comma_expression_opt ';' comma_expression_opt
    1339                 { $$ = new ForCtrl( $1, $3, $5 ); }
     1353                {
     1354                        StatementNode * init = $1 ? new StatementNode( new ExprStmt( maybeMoveBuild( $1 ) ) ) : nullptr;
     1355                        $$ = new ForCtrl( init, $3, $5 );
     1356                }
    13401357        | declaration comma_expression_opt ';' comma_expression_opt // C99, declaration has ';'
    1341                 { $$ = new ForCtrl( $1, $2, $4 ); }
     1358                { $$ = new ForCtrl( new StatementNode( $1 ), $2, $4 ); }
    13421359
    13431360        | '@' ';' comma_expression                                                      // CFA, empty loop-index
    1344                 { $$ = new ForCtrl( (ExpressionNode *)nullptr, $3, nullptr ); }
     1361                { $$ = new ForCtrl( nullptr, $3, nullptr ); }
    13451362        | '@' ';' comma_expression ';' comma_expression         // CFA, empty loop-index
    1346                 { $$ = new ForCtrl( (ExpressionNode *)nullptr, $3, $5 ); }
     1363                { $$ = new ForCtrl( nullptr, $3, $5 ); }
    13471364
    13481365        | comma_expression                                                                      // CFA, anonymous loop-index
Note: See TracChangeset for help on using the changeset viewer.