Changeset 4acd1f8


Ignore:
Timestamp:
Mar 11, 2026, 5:40:49 PM (14 hours ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
42bce4e
Parents:
1b6ec23
Message:

change codegen for loop else-clause to print else-clause as a compound statement

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cpp

    r1b6ec23 r4acd1f8  
    1010// Created On       : Tue Oct 17 15:54:00 2023
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jan 17 14:19:22 2025
    13 // Update Count     : 1
     12// Last Modified On : Wed Mar 11 11:13:10 2026
     13// Update Count     : 28
    1414//
    1515
     
    4949}
    5050
    51 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()(
    52                 std::vector<ast::Label> const & l ) {
     51CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::vector<ast::Label> const & l ) {
    5352        labels = &l;
    5453        return *this;
     
    12451244                output << "do";
    12461245        } else {
    1247                 output << "while (";
     1246                output << "while ( ";
    12481247                stmt->cond->accept( *visitor );
    1249                 output << ")";
     1248                output << " )";
    12501249        }
    12511250        output << " ";
     
    12551254
    12561255        if ( stmt->isDoWhile ) {
    1257                 output << " while (";
     1256                output << " while ( ";
    12581257                stmt->cond->accept( *visitor );
    1259                 output << ( ( nullptr == stmt->else_ ) ? ");" : ")" );
    1260         }
     1258                output << " );";                                                                // always terminate with semi-colon
     1259        }
     1260
    12611261        if ( stmt->else_ ) {
    1262                 output << " else ";
    1263                 stmt->else_->accept( *visitor );
     1262                stmt->else_->accept( *visitor );                                // not converted in AST pass
    12641263        }
    12651264}
    12661265
    12671266void CodeGenerator::postvisit( ast::ForStmt const * stmt ) {
    1268         // Initializer is always hoised so don't generate it.
    1269         // TODO: Do an assertion check?
    1270         output << "for (;";
     1267        assert( stmt->inits.empty() );                                          // initializer is hoisted
     1268        output << "for ( ; ";                                                           // empty initializer
    12711269
    12721270        if ( nullptr != stmt->cond ) {
    12731271                stmt->cond->accept( *visitor );
    12741272        }
    1275         output << ";";
     1273        output << "; ";                                                                         // separator
    12761274
    12771275        if ( nullptr != stmt->inc ) {
     
    12801278                expr->accept( *visitor );
    12811279        }
    1282         output << ") ";
     1280        output << " ) ";
    12831281
    12841282        if ( nullptr != stmt->body ) {
     
    12881286
    12891287        if ( nullptr != stmt->else_ ) {
    1290                 assertf( !options.genC, "Loop else should not reach code generation." );
    1291                 output << " else ";
    1292                 stmt->else_->accept( *visitor );
     1288                stmt->else_->accept( *visitor );                                // not converted in AST pass
    12931289        }
    12941290}
Note: See TracChangeset for help on using the changeset viewer.