Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    rcc32d83 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 }
    81 
    82 Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
    83         Statement * thenb, * elseb = 0;
    84         std::list< Statement * > branches;
    85         buildMoveList< Statement, StatementNode >( then_stmt, branches );
    86         assert( branches.size() == 1 );
    87         thenb = branches.front();
    88 
    89         if ( else_stmt ) {
    90                 std::list< Statement * > branches;
    91                 buildMoveList< Statement, StatementNode >( else_stmt, branches );
    92                 assert( branches.size() == 1 );
    93                 elseb = branches.front();
    94         } // if
    95 
    96         std::list< Statement * > init;
     76        if ( e ) return new ExprStmt( e );
     77        else return new NullStmt();
     78} // build_expr
     79
     80Expression * build_if_control( IfCtl * ctl, std::list< Statement * > & init ) {
    9781        if ( ctl->init != 0 ) {
    9882                buildMoveList( ctl->init, init );
     
    10286        if ( ctl->condition ) {
    10387                // compare the provided condition against 0
    104                 cond =  notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
     88                cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
    10589        } else {
    10690                for ( Statement * stmt : init ) {
     
    11397        }
    11498        delete ctl;
     99        return cond;
     100} // build_if_control
     101
     102Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
     103        Statement * thenb, * elseb = nullptr;
     104        std::list< Statement * > branches;
     105        buildMoveList< Statement, StatementNode >( then_stmt, branches );
     106        assert( branches.size() == 1 );
     107        thenb = branches.front();
     108
     109        if ( else_stmt ) {
     110                std::list< Statement * > branches;
     111                buildMoveList< Statement, StatementNode >( else_stmt, branches );
     112                assert( branches.size() == 1 );
     113                elseb = branches.front();
     114        } // if
     115
     116        std::list< Statement * > init;
     117        Expression * cond = build_if_control( ctl, init );
    115118        return new IfStmt( cond, thenb, elseb, init );
    116 }
     119} // build_if
    117120
    118121Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
     
    130133        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    131134        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
    132 }
     135} // build_switch
     136
    133137Statement * build_case( ExpressionNode * ctl ) {
    134138        std::list< Statement * > branches;
    135139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
    136 }
     140} // build_case
     141
    137142Statement * build_default() {
    138143        std::list< Statement * > branches;
    139144        return new CaseStmt( nullptr, branches, true );
    140 }
    141 
    142 Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind ) {
    143         std::list< Statement * > branches;
    144         buildMoveList< Statement, StatementNode >( stmt, branches );
    145         assert( branches.size() == 1 );
    146         return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
    147 }
     145} // build_default
     146
     147Statement * build_while( IfCtl * ctl, StatementNode * stmt ) {
     148        std::list< Statement * > branches;
     149        buildMoveList< Statement, StatementNode >( stmt, branches );
     150        assert( branches.size() == 1 );
     151
     152        std::list< Statement * > init;
     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
    148165
    149166Statement * build_for( ForCtl * forctl, StatementNode * stmt ) {
     
    167184        delete forctl;
    168185        return new ForStmt( init, cond, incr, branches.front() );
    169 }
     186} // build_for
    170187
    171188Statement * build_branch( BranchStmt::Type kind ) {
    172189        Statement * ret = new BranchStmt( "", kind );
    173190        return ret;
    174 }
     191} // build_branch
     192
    175193Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
    176194        Statement * ret = new BranchStmt( * identifier, kind );
    177195        delete identifier;                                                                      // allocated by lexer
    178196        return ret;
    179 }
     197} // build_branch
     198
    180199Statement * build_computedgoto( ExpressionNode * ctl ) {
    181200        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
    182 }
     201} // build_computedgoto
    183202
    184203Statement * build_return( ExpressionNode * ctl ) {
     
    186205        buildMoveList( ctl, exps );
    187206        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
    188 }
     207} // build_return
    189208
    190209Statement * build_throw( ExpressionNode * ctl ) {
     
    193212        assertf( exps.size() < 2, "This means we are leaking memory");
    194213        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    195 }
     214} // build_throw
    196215
    197216Statement * build_resume( ExpressionNode * ctl ) {
     
    200219        assertf( exps.size() < 2, "This means we are leaking memory");
    201220        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    202 }
     221} // build_resume
    203222
    204223Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
     
    206225        (void)target;
    207226        assertf( false, "resume at (non-local throw) is not yet supported," );
    208 }
     227} // build_resume_at
    209228
    210229Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
     
    214233        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
    215234        return new TryStmt( tryBlock, branches, finallyBlock );
    216 }
     235} // build_try
     236
    217237Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
    218238        std::list< Statement * > branches;
     
    220240        assert( branches.size() == 1 );
    221241        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
    222 }
     242} // build_catch
     243
    223244Statement * build_finally( StatementNode * stmt ) {
    224245        std::list< Statement * > branches;
     
    226247        assert( branches.size() == 1 );
    227248        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
    228 }
     249} // build_finally
    229250
    230251WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
     
    247268
    248269        return node;
    249 }
     270} // build_waitfor
    250271
    251272WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
     
    266287
    267288        return node;
    268 }
     289} // build_waitfor
    269290
    270291WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
     
    275296                node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
    276297                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    277         }
    278         else {
     298        } else {
    279299                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
    280300                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    281         }
     301        } // if
    282302
    283303        return node;
    284 }
     304} // build_waitfor_timeout
    285305
    286306WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
     
    295315
    296316        return node;
    297 }
     317} // build_waitfor_timeout
    298318
    299319WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
     
    302322        Statement * s = maybeMoveBuild<Statement>( stmt );
    303323        return new WithStmt( e, s );
    304 }
     324} // build_with
    305325
    306326Statement * build_compound( StatementNode * first ) {
     
    308328        buildMoveList( first, cs->get_kids() );
    309329        return cs;
    310 }
     330} // build_compound
    311331
    312332Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
     
    318338        buildMoveList( clobber, clob );
    319339        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
    320 }
     340} // build_asm
    321341
    322342Statement * build_directive( string * directive ) {
    323343        return new DirectiveStmt( *directive );
    324 }
     344} // build_directive
    325345
    326346// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.