Changeset ee3c93d


Ignore:
Timestamp:
Jun 4, 2018, 12:40:46 PM (4 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
2ad4b49
Parents:
b429026
git-author:
Rob Schluntz <rschlunt@…> (06/04/18 12:40:38)
git-committer:
Rob Schluntz <rschlunt@…> (06/04/18 12:40:46)
Message:

Add support for while loops with control declarations

Location:
src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.impl.h

    rb429026 ree3c93d  
    828828        VISIT_START( node );
    829829
    830         visitExpression( node->condition );
    831         node->body = visitStatement( node->body );
     830        {
     831                // while statements introduce a level of scope (for the initialization)
     832                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     833                maybeAccept_impl( node->initialization, *this );
     834                visitExpression ( node->condition );
     835                node->body = visitStatement( node->body );
     836        }
    832837
    833838        VISIT_END( node );
     
    838843        MUTATE_START( node );
    839844
    840         node->condition = mutateExpression( node->condition );
    841         node->body      = mutateStatement ( node->body      );
     845        {
     846                // while statements introduce a level of scope (for the initialization)
     847                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     848                maybeMutate_impl( node->initialization, *this );
     849                node->condition = mutateExpression( node->condition );
     850                node->body      = mutateStatement ( node->body      );
     851        }
     852
    842853
    843854        MUTATE_END( Statement, node );
  • src/ControlStruct/ForExprMutator.cc

    rb429026 ree3c93d  
    4545                return hoist( forStmt, forStmt->initialization );
    4646        }
     47        Statement *ForExprMutator::postmutate( WhileStmt *whileStmt ) {
     48                return hoist( whileStmt, whileStmt->initialization );
     49        }
    4750} // namespace ControlStruct
    4851
  • src/ControlStruct/ForExprMutator.h

    rb429026 ree3c93d  
    1818class IfStmt;
    1919class ForStmt;
     20class WhileStmt;
    2021class Statement;
    2122
     
    2526                Statement *postmutate( IfStmt * );
    2627                Statement *postmutate( ForStmt * );
     28                Statement *postmutate( WhileStmt * );
    2729        };
    2830} // namespace ControlStruct
  • src/Parser/ParseNode.h

    rb429026 ree3c93d  
    403403};
    404404
     405Expression * build_if_control( IfCtl * ctl, std::list< Statement * > & init );
    405406Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
    406407Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt );
  • src/Parser/StatementNode.cc

    rb429026 ree3c93d  
    8080}
    8181
    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;
     82Expression * build_if_control( IfCtl * ctl, std::list< Statement * > & init ) {
    9783        if ( ctl->init != 0 ) {
    9884                buildMoveList( ctl->init, init );
     
    10288        if ( ctl->condition ) {
    10389                // compare the provided condition against 0
    104                 cond =  notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
     90                cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
    10591        } else {
    10692                for ( Statement * stmt : init ) {
     
    11399        }
    114100        delete ctl;
     101        return cond;
     102}
     103
     104Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
     105        Statement * thenb, * elseb = 0;
     106        std::list< Statement * > branches;
     107        buildMoveList< Statement, StatementNode >( then_stmt, branches );
     108        assert( branches.size() == 1 );
     109        thenb = branches.front();
     110
     111        if ( else_stmt ) {
     112                std::list< Statement * > branches;
     113                buildMoveList< Statement, StatementNode >( else_stmt, branches );
     114                assert( branches.size() == 1 );
     115                elseb = branches.front();
     116        } // if
     117
     118        std::list< Statement * > init;
     119        Expression * cond = build_if_control( ctl, init );
    115120        return new IfStmt( cond, thenb, elseb, init );
    116121}
     
    144149        buildMoveList< Statement, StatementNode >( stmt, branches );
    145150        assert( branches.size() == 1 );
    146         return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
     151
     152        std::list< Statement * > init;
     153        return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, kind );
    147154}
    148155
  • src/SynTree/Statement.cc

    rb429026 ree3c93d  
    243243}
    244244
    245 WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):
    246         Statement(), condition( condition), body( body), isDoWhile( isDoWhile) {
     245WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ):
     246        Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) {
    247247}
    248248
  • src/SynTree/Statement.h

    rb429026 ree3c93d  
    220220        Expression *condition;
    221221        Statement *body;
     222        std::list<Statement *> initialization;
    222223        bool isDoWhile;
    223224
    224225        WhileStmt( Expression *condition,
    225                Statement *body, bool isDoWhile = false );
     226               Statement *body, std::list<Statement *> & initialization, bool isDoWhile = false );
    226227        WhileStmt( const WhileStmt &other );
    227228        virtual ~WhileStmt();
Note: See TracChangeset for help on using the changeset viewer.