Changeset 401e61f


Ignore:
Timestamp:
Jun 5, 2018, 10:16:46 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, with_gc
Children:
2248dc7, dafdbe7
Parents:
6e3eaa57
Message:

add if control to while loop

Location:
src
Files:
3 edited
2 moved

Legend:

Unmodified
Added
Removed
  • src/Parser/ParseNode.h

    r6e3eaa57 r401e61f  
    1010// Created On       : Sat May 16 13:28:16 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Apr 30 09:19:17 2018
    13 // Update Count     : 831
     12// Last Modified On : Mon Jun  4 22:21:04 2018
     13// Update Count     : 832
    1414//
    1515
     
    408408Statement * build_case( ExpressionNode * ctl );
    409409Statement * build_default();
    410 Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false );
     410Statement * build_while( IfCtl * ctl, StatementNode * stmt );
     411Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt );
    411412Statement * build_for( ForCtl * forctl, StatementNode * stmt );
    412413Statement * build_branch( BranchStmt::Type kind );
  • src/Parser/StatementNode.cc

    r6e3eaa57 r401e61f  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Apr 30 09:21:16 2018
    13 // Update Count     : 354
     12// Last Modified On : Tue Jun  5 08:58:34 2018
     13// Update Count     : 362
    1414//
    1515
     
    6969        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    7070        return this;
    71 }
     71} // StatementNode::append_last_case
    7272
    7373Statement * build_expr( ExpressionNode * ctl ) {
    7474        Expression * e = maybeMoveBuild< Expression >( ctl );
    7575
    76         if ( e )
    77                 return new ExprStmt( e );
    78         else
    79                 return new NullStmt();
    80 }
     76        if ( e ) return new ExprStmt( e );
     77        else return new NullStmt();
     78} // build_expr
    8179
    8280Expression * build_if_control( IfCtl * ctl, std::list< Statement * > & init ) {
     
    10098        delete ctl;
    10199        return cond;
    102 }
     100} // build_if_control
    103101
    104102Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
    105         Statement * thenb, * elseb = 0;
     103        Statement * thenb, * elseb = nullptr;
    106104        std::list< Statement * > branches;
    107105        buildMoveList< Statement, StatementNode >( then_stmt, branches );
     
    119117        Expression * cond = build_if_control( ctl, init );
    120118        return new IfStmt( cond, thenb, elseb, init );
    121 }
     119} // build_if
    122120
    123121Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
     
    135133        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    136134        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
    137 }
     135} // build_switch
     136
    138137Statement * build_case( ExpressionNode * ctl ) {
    139138        std::list< Statement * > branches;
    140139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
    141 }
     140} // build_case
     141
    142142Statement * build_default() {
    143143        std::list< Statement * > branches;
    144144        return new CaseStmt( nullptr, branches, true );
    145 }
    146 
    147 Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind ) {
     145} // build_default
     146
     147Statement * build_while( IfCtl * ctl, StatementNode * stmt ) {
    148148        std::list< Statement * > branches;
    149149        buildMoveList< Statement, StatementNode >( stmt, branches );
     
    151151
    152152        std::list< Statement * > init;
    153         return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, kind );
    154 }
     153        Expression * cond = build_if_control( ctl, init );
     154        return new WhileStmt( cond, branches.front(), init, false );
     155} // build_while
     156
     157Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt ) {
     158        std::list< Statement * > branches;
     159        buildMoveList< Statement, StatementNode >( stmt, branches );
     160        assert( branches.size() == 1 );
     161
     162        std::list< Statement * > init;
     163        return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, true );
     164} // build_do_while
    155165
    156166Statement * build_for( ForCtl * forctl, StatementNode * stmt ) {
     
    174184        delete forctl;
    175185        return new ForStmt( init, cond, incr, branches.front() );
    176 }
     186} // build_for
    177187
    178188Statement * build_branch( BranchStmt::Type kind ) {
    179189        Statement * ret = new BranchStmt( "", kind );
    180190        return ret;
    181 }
     191} // build_branch
     192
    182193Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
    183194        Statement * ret = new BranchStmt( * identifier, kind );
    184195        delete identifier;                                                                      // allocated by lexer
    185196        return ret;
    186 }
     197} // build_branch
     198
    187199Statement * build_computedgoto( ExpressionNode * ctl ) {
    188200        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
    189 }
     201} // build_computedgoto
    190202
    191203Statement * build_return( ExpressionNode * ctl ) {
     
    193205        buildMoveList( ctl, exps );
    194206        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
    195 }
     207} // build_return
    196208
    197209Statement * build_throw( ExpressionNode * ctl ) {
     
    200212        assertf( exps.size() < 2, "This means we are leaking memory");
    201213        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    202 }
     214} // build_throw
    203215
    204216Statement * build_resume( ExpressionNode * ctl ) {
     
    207219        assertf( exps.size() < 2, "This means we are leaking memory");
    208220        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    209 }
     221} // build_resume
    210222
    211223Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
     
    213225        (void)target;
    214226        assertf( false, "resume at (non-local throw) is not yet supported," );
    215 }
     227} // build_resume_at
    216228
    217229Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
     
    221233        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
    222234        return new TryStmt( tryBlock, branches, finallyBlock );
    223 }
     235} // build_try
     236
    224237Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    225238        std::list< Statement * > branches;
     
    227240        assert( branches.size() == 1 );
    228241        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
    229 }
     242} // build_catch
     243
    230244Statement * build_finally( StatementNode * stmt ) {
    231245        std::list< Statement * > branches;
     
    233247        assert( branches.size() == 1 );
    234248        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
    235 }
     249} // build_finally
    236250
    237251WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
     
    254268
    255269        return node;
    256 }
     270} // build_waitfor
    257271
    258272WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
     
    273287
    274288        return node;
    275 }
     289} // build_waitfor
    276290
    277291WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
     
    282296                node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
    283297                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    284         }
    285         else {
     298        } else {
    286299                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
    287300                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    288         }
     301        } // if
    289302
    290303        return node;
    291 }
     304} // build_waitfor_timeout
    292305
    293306WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
     
    302315
    303316        return node;
    304 }
     317} // build_waitfor_timeout
    305318
    306319WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
     
    309322        Statement * s = maybeMoveBuild<Statement>( stmt );
    310323        return new WithStmt( e, s );
    311 }
     324} // build_with
    312325
    313326Statement * build_compound( StatementNode * first ) {
     
    315328        buildMoveList( first, cs->get_kids() );
    316329        return cs;
    317 }
     330} // build_compound
    318331
    319332Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
     
    325338        buildMoveList( clobber, clob );
    326339        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
    327 }
     340} // build_asm
    328341
    329342Statement * build_directive( string * directive ) {
    330343        return new DirectiveStmt( *directive );
    331 }
     344} // build_directive
    332345
    333346// Local Variables: //
  • src/Parser/parser.yy

    r6e3eaa57 r401e61f  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jun  4 21:24:46 2018
    13 // Update Count     : 3488
     12// Last Modified On : Mon Jun  4 22:22:04 2018
     13// Update Count     : 3492
    1414//
    1515
     
    10501050
    10511051iteration_statement:
    1052         WHILE '(' comma_expression ')' statement
    1053                 { $$ = new StatementNode( build_while( $3, $5 ) ); }
     1052        WHILE '(' push if_control_expression ')' statement pop
     1053                { $$ = new StatementNode( build_while( $4, $6 ) ); }
    10541054        | DO statement WHILE '(' comma_expression ')' ';'
    1055                 { $$ = new StatementNode( build_while( $5, $2, true ) ); }
     1055                { $$ = new StatementNode( build_do_while( $5, $2 ) ); }
    10561056        | FOR '(' push for_control_expression ')' statement pop
    10571057                { $$ = new StatementNode( build_for( $4, $6 ) ); }
  • src/tests/.expect/ifwhileCtl.txt

    r6e3eaa57 r401e61f  
    33x == y correct
    44s.i < 4 correct
     5x != 0 correct
     6x == y correct
     7s.i < 4 correct
  • src/tests/ifwhileCtl.c

    r6e3eaa57 r401e61f  
    1010// Created On       : Sat Aug 26 10:13:11 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun  2 09:55:02 2018
    13 // Update Count     : 15
     12// Last Modified On : Mon Jun  4 22:46:48 2018
     13// Update Count     : 19
    1414//
    1515
     
    4242
    4343        if ( struct S { int i; } s = { 3 }; s.i < 4 ) {
     44                S s1;
    4445                sout | "s.i < 4 correct" | endl;
    4546        } else {
     47                S s1;
    4648                sout | "s.i >= 4 incorrect" | endl;
    4749        } // if
     50
     51        while ( int x = 1 ) {
     52                sout | "x != 0 correct" | endl;
     53                break;
     54        } // while
     55
     56        while ( int x = 4, y = 0 ) {
     57                sout | "x != 0 && y != 0 incorrect" | endl;
     58        } // while
     59
     60        while ( int x = 5, y = f( x ); x == y ) {
     61                sout | "x == y correct" | endl;
     62                break;
     63        } // while
     64
     65        while ( struct S { int i; } s = { 3 }; s.i < 4 ) {
     66                S s1;
     67                sout | "s.i < 4 correct" | endl;
     68                break;
     69        } // while
    4870} // main
    4971
Note: See TracChangeset for help on using the changeset viewer.