Ignore:
Timestamp:
Aug 14, 2016, 8:36:29 AM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
777bfcf
Parents:
46b4259
Message:

more refactoring of parser code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    r46b4259 r1d4580a  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 11 16:19:45 2016
    13 // Update Count     : 210
     12// Last Modified On : Sun Aug 14 08:09:31 2016
     13// Update Count     : 271
    1414//
    1515
     
    3434};
    3535
    36 StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
    37 
    38 StatementNode::StatementNode( const string *name ) : ParseNode( name ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
    39 
    40 StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), isCatchRest ( false ) {
     36StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), catchAny( false ) {}
     37
     38StatementNode::StatementNode( const string *name ) : ParseNode( name ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), catchAny( false ) { assert( false ); }
     39
     40StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), catchAny( false ) {
     41        assert( false );
    4142        if ( decl ) {
    4243                if ( DeclarationNode *agg = decl->extractAggregate() ) {
     
    4647                        nextStmt->decl = decl;
    4748                        next = nextStmt;
    48                         if ( decl->get_link() ) {
    49                                 next->set_next( new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ) );
     49                        if ( decl->get_next() ) {
     50                                next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) );
    5051                                decl->set_next( 0 );
    5152                        } // if
    5253                } else {
    53                         if ( decl->get_link() ) {
    54                                 next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) );
     54                        if ( decl->get_next() ) {
     55                                next = new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
    5556                                decl->set_next( 0 );
    5657                        } // if
     
    6061}
    6162
    62 StatementNode::StatementNode( Type t, ExpressionNode *ctrl_label, StatementNode *block ) : type( t ), control( ctrl_label ), block( block ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {
     63StatementNode2::StatementNode2( DeclarationNode *decl ) {
     64        if ( decl ) {
     65                DeclarationNode *agg = decl->extractAggregate();
     66                if ( agg ) {
     67                        StatementNode *nextStmt = new StatementNode;
     68                        nextStmt->type = Decl;
     69                        nextStmt->decl = decl;
     70                        next = nextStmt;
     71                        if ( decl->get_next() ) {
     72                                next->set_next( new StatementNode2( dynamic_cast<DeclarationNode *>(decl->get_next()) ) );
     73                                decl->set_next( 0 );
     74                        } // if
     75                } else {
     76                        if ( decl->get_next() ) {
     77                                next = new StatementNode2( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
     78                                decl->set_next( 0 );
     79                        } // if
     80                        agg = decl;
     81                } // if
     82                stmt = new DeclStmt( noLabels, maybeBuild<Declaration>(agg) );
     83        } else {
     84                assert( false );
     85        } // if
     86}
     87
     88StatementNode::StatementNode( Type t, ExpressionNode *ctrl_label, StatementNode *block ) : type( t ), control( ctrl_label ), block( block ), labels( 0 ), target( 0 ), decl( 0 ), catchAny( false ) {
    6389        this->control = ( t == Default ) ? 0 : control;
    6490}
    6591
    66 StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), isCatchRest ( false ) {}
     92StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), catchAny( false ) {}
    6793
    6894StatementNode::~StatementNode() {
     
    7399}
    74100
    75 StatementNode * StatementNode::newCatchStmt( DeclarationNode *d, StatementNode *s, bool catchRestP ) {
    76         StatementNode *ret = new StatementNode( StatementNode::Catch, 0, s );
    77         ret->addDeclaration( d );
    78         ret->setCatchRest( catchRestP );
    79 
    80         return ret;
    81 }
    82 
    83 std::string StatementNode::get_target() const{
    84         if ( target )
    85                 return *target;
    86 
    87         return string("");
    88 }
    89 
    90101StatementNode * StatementNode::clone() const {
     102        assert( false );
    91103        StatementNode *newnode = new StatementNode( type, maybeClone( control ), maybeClone( block ) );
    92104        if ( target ) {
     
    112124                        block = stmt;
    113125                else
    114                         block->set_link( stmt );
     126                        block->set_last( stmt );
    115127        } // if
    116128        return this;
     
    120132        assert( false );
    121133        if ( stmt != 0 ) {
    122                 StatementNode *next = ( StatementNode *)get_link();
     134                StatementNode *next = ( StatementNode *)get_next();
    123135                if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) )
    124136                        next->append_last_case( stmt );
     
    127139                                block = stmt;
    128140                        else
    129                                 block->set_link( stmt );
     141                                block->set_last( stmt );
    130142        } // if
    131143        return this;
     
    134146StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
    135147        StatementNode *prev = this;
    136         for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_link() ) {
     148        // find end of list and maintain previous pointer
     149        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    137150                StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
    138151                assert( node );
     
    140153                prev = curr;
    141154        } // for
     155        // conver from StatementNode list to Statement list
    142156        StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
    143157        std::list<Statement *> stmts;
    144158        buildList( stmt, stmts );
    145         CaseStmt * caseStmt;
    146         caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
     159        // splice any new Statements to end of currents Statements
     160        CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
    147161        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    148162        return this;
     
    177191                                os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
    178192                                decl->print( os, indent + 2 * ParseNode::indent_by );
    179                         } else if ( isCatchRest ) {
     193                        } else if ( catchAny ) {
    180194                                os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
    181195                        } else {
     
    191205                        block->printList( os, indent + 2 * ParseNode::indent_by );
    192206                } // if
    193                 if ( target ) {
    194                         os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
    195                 } // if
    196207                break;
    197208        } // switch
     
    208219        } // if
    209220
    210         // try {
    211221        buildList<Statement, StatementNode>( get_block(), branches );
    212222
     
    214224          case Decl:
    215225                return new DeclStmt( labs, maybeBuild< Declaration >( decl ) );
     226                assert( false );
    216227          case Exp:
    217                 {
    218                         Expression *e = maybeBuild< Expression >( get_control() );
    219 
    220                         if ( e )
    221                                 return new ExprStmt( labs, e );
    222                         else
    223                                 return new NullStmt( labs );
    224                 }
     228                // {
     229                //      Expression *e = maybeBuild< Expression >( get_control() );
     230
     231                //      if ( e )
     232                //              return new ExprStmt( labs, e );
     233                //      else
     234                //              return new NullStmt( labs );
     235                // }
    225236                assert( false );
    226237          case If:
     
    279290                assert( false );
    280291          case Goto:
    281                 // {
    282                 //      if ( get_target() == "" ) {                                     // computed goto
    283                 //              assert( get_control() != 0 );
    284                 //              return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
    285                 //      } // if
    286 
    287                 //      return new BranchStmt( labs, get_target(), BranchStmt::Goto );
    288                 // }
    289292                assert( false );
    290293          case Break:
    291                 // return new BranchStmt( labs, get_target(), BranchStmt::Break );
    292294                assert( false );
    293295          case Continue:
    294                 // return new BranchStmt( labs, get_target(), BranchStmt::Continue );
    295296                assert( false );
    296297          case Return:
     
    303304                assert( false );
    304305          case Try:
    305                 {
    306                         assert( branches.size() >= 0 );
    307                         CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
    308                         branches.pop_front();
    309                         FinallyStmt *finallyBlock = 0;
    310                         if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
    311                                 branches.pop_back();
    312                         } // if
    313                         return new TryStmt( labs, tryBlock, branches, finallyBlock );
    314                 }
     306                // {
     307                //      assert( branches.size() >= 0 );
     308                //      CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
     309                //      branches.pop_front();
     310                //      FinallyStmt *finallyBlock = 0;
     311                //      if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
     312                //              branches.pop_back();
     313                //      } // if
     314                //      return new TryStmt( labs, tryBlock, branches, finallyBlock );
     315                // }
     316                assert( false );
    315317          case Catch:
    316                 {
    317                         assert( branches.size() == 1 );
    318 
    319                         return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
    320                 }
     318                // {
     319                //      assert( branches.size() == 1 );
     320
     321                //      return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), catchAny );
     322                // }
     323                assert( false );
    321324          case Finally:
    322                 {
    323                         assert( branches.size() == 1 );
    324                         CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
    325                         assert( block != 0 );
    326 
    327                         return new FinallyStmt( labs, block );
    328                 }
     325                // {
     326                //      assert( branches.size() == 1 );
     327                //      CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
     328                //      assert( block != 0 );
     329
     330                //      return new FinallyStmt( labs, block );
     331                // }
     332                assert( false );
    329333          case Asm:
    330334                assert( false );
    331335          default:
    332                 // shouldn't be here
     336                assert( false );
    333337                return 0;
    334338        } // switch
     
    422426}
    423427
     428Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
     429        std::list<Statement *> branches;
     430        buildList<Statement, StatementNode>( catch_stmt, branches );
     431        CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>(maybeBuild<Statement>(try_stmt));
     432        assert( tryBlock );
     433        FinallyStmt *finallyBlock = dynamic_cast<FinallyStmt *>(maybeBuild<Statement>(finally_stmt) );
     434        return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
     435}
     436Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny ) {
     437        std::list<Statement *> branches;
     438        buildList<Statement, StatementNode>( stmt, branches );
     439        assert( branches.size() == 1 );
     440        return new CatchStmt( noLabels, maybeBuild<Declaration>(decl), branches.front(), catchAny );
     441}
     442Statement *build_finally( StatementNode *stmt ) {
     443        std::list<Statement *> branches;
     444        buildList<Statement, StatementNode>( stmt, branches );
     445        assert( branches.size() == 1 );
     446        return new FinallyStmt( noLabels, dynamic_cast<CompoundStmt *>( branches.front() ) );
     447}
     448
    424449
    425450CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
    426 
    427 CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
    428451
    429452CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
     
    441464void CompoundStmtNode::add_statement( StatementNode *stmt ) {
    442465        if ( stmt != 0 ) {
    443                 last->set_link( stmt );
    444                 last = ( StatementNode *)( stmt->get_link());
     466                last->set_last( stmt );
     467                last = ( StatementNode *)( stmt->get_next());
    445468        } // if
    446469}
Note: See TracChangeset for help on using the changeset viewer.