Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    rf271bdd rba3706f  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Aug  4 09:39:25 2018
    13 // Update Count     : 363
     12// Last Modified On : Fri Sep  1 23:25:23 2017
     13// Update Count     : 346
    1414//
    1515
     
    3333
    3434
    35 StatementNode::StatementNode( DeclarationNode * decl ) {
     35StatementNode::StatementNode( DeclarationNode *decl ) {
    3636        assert( decl );
    37         DeclarationNode * agg = decl->extractAggregate();
     37        DeclarationNode *agg = decl->extractAggregate();
    3838        if ( agg ) {
    39                 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
     39                StatementNode *nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
    4040                set_next( nextStmt );
    4141                if ( decl->get_next() ) {
     
    5353} // StatementNode::StatementNode
    5454
    55 StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
    56         StatementNode * prev = this;
     55StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
     56        StatementNode *prev = this;
    5757        // find end of list and maintain previous pointer
    5858        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    59                 StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
     59                StatementNode *node = strict_dynamic_cast< StatementNode * >(curr);
    6060                assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
    6161                prev = curr;
    6262        } // for
    6363        // convert from StatementNode list to Statement list
    64         StatementNode * node = dynamic_cast< StatementNode * >(prev);
     64        StatementNode *node = dynamic_cast< StatementNode * >(prev);
    6565        std::list< Statement * > stmts;
    6666        buildMoveList( stmt, stmts );
     
    6969        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    7070        return this;
    71 } // StatementNode::append_last_case
    72 
    73 Statement * build_expr( ExpressionNode * ctl ) {
    74         Expression * e = maybeMoveBuild< Expression >( ctl );
    75 
    76         if ( e ) return new ExprStmt( e );
    77         else return new NullStmt();
    78 } // build_expr
    79 
    80 Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) {
     71}
     72
     73Statement *build_expr( ExpressionNode *ctl ) {
     74        Expression *e = maybeMoveBuild< Expression >( ctl );
     75
     76        if ( e )
     77                return new ExprStmt( e );
     78        else
     79                return new NullStmt();
     80}
     81
     82Statement *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;
    8197        if ( ctl->init != 0 ) {
    8298                buildMoveList( ctl->init, init );
     
    86102        if ( ctl->condition ) {
    87103                // compare the provided condition against 0
    88                 cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
     104                cond =  notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
    89105        } else {
    90106                for ( Statement * stmt : init ) {
     
    97113        }
    98114        delete ctl;
    99         return cond;
    100 } // build_if_control
    101 
    102 Statement * build_if( IfCtrl * 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 );
    118115        return new IfStmt( cond, thenb, elseb, init );
    119 } // build_if
    120 
    121 Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
     116}
     117
     118Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
    122119        std::list< Statement * > branches;
    123120        buildMoveList< Statement, StatementNode >( stmt, branches );
    124         if ( ! isSwitch ) {                                                                             // choose statement
    125                 for ( Statement * stmt : branches ) {
    126                         CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
    127                         if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
    128                                 CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
    129                                 block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
    130                         } // if
    131                 } // for
    132         } // if
    133121        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
    134122        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
    135 } // build_switch
    136 
    137 Statement * build_case( ExpressionNode * ctl ) {
     123}
     124Statement *build_case( ExpressionNode *ctl ) {
    138125        std::list< Statement * > branches;
    139126        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
    140 } // build_case
    141 
    142 Statement * build_default() {
     127}
     128Statement *build_default() {
    143129        std::list< Statement * > branches;
    144130        return new CaseStmt( nullptr, branches, true );
    145 } // build_default
    146 
    147 Statement * build_while( IfCtrl * ctl, StatementNode * stmt ) {
     131}
     132
     133Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
    148134        std::list< Statement * > branches;
    149135        buildMoveList< Statement, StatementNode >( stmt, branches );
    150136        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 
    157 Statement * 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
    165 
    166 Statement * build_for( ForCtrl * forctl, StatementNode * stmt ) {
     137        return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
     138}
     139
     140Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
    167141        std::list< Statement * > branches;
    168142        buildMoveList< Statement, StatementNode >( stmt, branches );
     
    174148        } // if
    175149
    176         Expression * cond = 0;
     150        Expression *cond = 0;
    177151        if ( forctl->condition != 0 )
    178152                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
    179153
    180         Expression * incr = 0;
     154        Expression *incr = 0;
    181155        if ( forctl->change != 0 )
    182156                incr = maybeMoveBuild< Expression >(forctl->change);
     
    184158        delete forctl;
    185159        return new ForStmt( init, cond, incr, branches.front() );
    186 } // build_for
    187 
    188 Statement * build_branch( BranchStmt::Type kind ) {
     160}
     161
     162Statement *build_branch( BranchStmt::Type kind ) {
    189163        Statement * ret = new BranchStmt( "", kind );
    190164        return ret;
    191 } // build_branch
    192 
    193 Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
    194         Statement * ret = new BranchStmt( * identifier, kind );
     165}
     166Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
     167        Statement * ret = new BranchStmt( *identifier, kind );
    195168        delete identifier;                                                                      // allocated by lexer
    196169        return ret;
    197 } // build_branch
    198 
    199 Statement * build_computedgoto( ExpressionNode * ctl ) {
     170}
     171Statement *build_computedgoto( ExpressionNode *ctl ) {
    200172        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
    201 } // build_computedgoto
    202 
    203 Statement * build_return( ExpressionNode * ctl ) {
     173}
     174
     175Statement *build_return( ExpressionNode *ctl ) {
    204176        std::list< Expression * > exps;
    205177        buildMoveList( ctl, exps );
    206178        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
    207 } // build_return
    208 
    209 Statement * build_throw( ExpressionNode * ctl ) {
     179}
     180
     181Statement *build_throw( ExpressionNode *ctl ) {
    210182        std::list< Expression * > exps;
    211183        buildMoveList( ctl, exps );
    212184        assertf( exps.size() < 2, "This means we are leaking memory");
    213185        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
    214 } // build_throw
    215 
    216 Statement * build_resume( ExpressionNode * ctl ) {
     186}
     187
     188Statement *build_resume( ExpressionNode *ctl ) {
    217189        std::list< Expression * > exps;
    218190        buildMoveList( ctl, exps );
    219191        assertf( exps.size() < 2, "This means we are leaking memory");
    220192        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
    221 } // build_resume
    222 
    223 Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
     193}
     194
     195Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
    224196        (void)ctl;
    225197        (void)target;
    226198        assertf( false, "resume at (non-local throw) is not yet supported," );
    227 } // build_resume_at
    228 
    229 Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
     199}
     200
     201Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
    230202        std::list< CatchStmt * > branches;
    231203        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
    232         CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
    233         FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
     204        CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
     205        FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
    234206        return new TryStmt( tryBlock, branches, finallyBlock );
    235 } // build_try
    236 
    237 Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
     207}
     208Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
    238209        std::list< Statement * > branches;
    239210        buildMoveList< Statement, StatementNode >( body, branches );
    240211        assert( branches.size() == 1 );
    241212        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
    242 } // build_catch
    243 
    244 Statement * build_finally( StatementNode * stmt ) {
     213}
     214Statement *build_finally( StatementNode *stmt ) {
    245215        std::list< Statement * > branches;
    246216        buildMoveList< Statement, StatementNode >( stmt, branches );
    247217        assert( branches.size() == 1 );
    248218        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
    249 } // build_finally
     219}
    250220
    251221WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
     
    268238
    269239        return node;
    270 } // build_waitfor
     240}
    271241
    272242WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
     
    287257
    288258        return node;
    289 } // build_waitfor
     259}
    290260
    291261WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
     
    296266                node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
    297267                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    298         } else {
     268        }
     269        else {
    299270                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
    300271                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    301         } // if
     272        }
    302273
    303274        return node;
    304 } // build_waitfor_timeout
     275}
    305276
    306277WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
     
    311282        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
    312283
    313         node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
     284        node->orelse.statement = maybeMoveBuild<Statement >( else_stmt );
    314285        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
    315286
    316287        return node;
    317 } // build_waitfor_timeout
    318 
    319 WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
    320         std::list< Expression * > e;
    321         buildMoveList( exprs, e );
    322         Statement * s = maybeMoveBuild<Statement>( stmt );
    323         return new WithStmt( e, s );
    324 } // build_with
    325 
    326 Statement * build_compound( StatementNode * first ) {
    327         CompoundStmt * cs = new CompoundStmt();
     288}
     289
     290Statement *build_compound( StatementNode *first ) {
     291        CompoundStmt *cs = new CompoundStmt();
    328292        buildMoveList( first, cs->get_kids() );
    329293        return cs;
    330 } // build_compound
    331 
    332 Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
     294}
     295
     296Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
    333297        std::list< Expression * > out, in;
    334298        std::list< ConstantExpr * > clob;
     
    338302        buildMoveList( clobber, clob );
    339303        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
    340 } // build_asm
    341 
    342 Statement * build_directive( string * directive ) {
    343         return new DirectiveStmt( *directive );
    344 } // build_directive
     304}
    345305
    346306// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.