Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    re82aa9df r777bfcf  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Aug 15 14:40:05 2016
    13 // Update Count     : 317
     12// Last Modified On : Sun Aug 14 13:10:54 2016
     13// Update Count     : 288
    1414//
    1515
     
    2626using namespace std;
    2727
    28 
    29 StatementNode::StatementNode( DeclarationNode *decl ) {
     28const char *StatementNode::StType[] = {
     29        "Exp",   "If",       "Switch", "Case",    "Default",  "Choose",   "Fallthru",
     30        "While", "Do",       "For",
     31        "Goto",  "Continue", "Break",  "Return",  "Throw",
     32        "Try",   "Catch",    "Finally", "Asm",
     33        "Decl"
     34};
     35
     36StatementNode::StatementNode() : ParseNode(), labels( 0 ), decl( 0 ) {}
     37
     38StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), labels( 0 ) {
     39        assert( false );
    3040        if ( decl ) {
    31                 DeclarationNode *agg = decl->extractAggregate();
    32                 if ( agg ) {
    33                         StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) );
     41                if ( DeclarationNode *agg = decl->extractAggregate() ) {
     42                        this->decl = agg;
     43                        StatementNode *nextStmt = new StatementNode;
     44                        nextStmt->type = Decl;
     45                        nextStmt->decl = decl;
    3446                        next = nextStmt;
    3547                        if ( decl->get_next() ) {
    36                                 next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>(decl->get_next()) ) );
     48                                next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) );
    3749                                decl->set_next( 0 );
    3850                        } // if
     
    4254                                decl->set_next( 0 );
    4355                        } // if
     56                        this->decl = decl;
     57                } // if
     58        } // if
     59}
     60
     61StatementNode2::StatementNode2( DeclarationNode *decl ) {
     62        if ( decl ) {
     63                DeclarationNode *agg = decl->extractAggregate();
     64                if ( agg ) {
     65                        StatementNode *nextStmt = new StatementNode;
     66                        nextStmt->type = Decl;
     67                        nextStmt->decl = decl;
     68                        next = nextStmt;
     69                        if ( decl->get_next() ) {
     70                                next->set_next( new StatementNode2( dynamic_cast<DeclarationNode *>(decl->get_next()) ) );
     71                                decl->set_next( 0 );
     72                        } // if
     73                } else {
     74                        if ( decl->get_next() ) {
     75                                next = new StatementNode2( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
     76                                decl->set_next( 0 );
     77                        } // if
    4478                        agg = decl;
    4579                } // if
     
    5084}
    5185
     86StatementNode::~StatementNode() {
     87        delete decl;
     88}
     89
     90StatementNode * StatementNode::clone() const {
     91        assert( false );
     92        return 0;
     93}
     94
     95StatementNode *StatementNode::add_label( const std::string *l ) {
     96        if ( l != 0 ) {
     97                labels.push_front( *l );
     98                delete l;
     99        } // if
     100        return this;
     101}
     102
    52103StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
     104        assert( false );
     105        return this;
     106}
     107
     108StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
    53109        StatementNode *prev = this;
    54110        // find end of list and maintain previous pointer
    55111        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    56                 StatementNode *node = dynamic_cast<StatementNode *>(curr);
     112                StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
    57113                assert( node );
    58114                assert( dynamic_cast<CaseStmt *>(node->stmt) );
    59115                prev = curr;
    60116        } // for
    61         // convert from StatementNode list to Statement list
    62         StatementNode *node = dynamic_cast<StatementNode *>(prev);
     117        // conver from StatementNode list to Statement list
     118        StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
    63119        std::list<Statement *> stmts;
    64120        buildList( stmt, stmts );
    65         // splice any new Statements to end of current Statements
     121        // splice any new Statements to end of currents Statements
    66122        CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
    67123        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    68124        return this;
     125}
     126
     127Statement *StatementNode::build() const {
     128        switch ( type ) {
     129          case Decl:
     130                return new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) );
     131                assert( false );
     132          case Exp:
     133          case If:
     134          case Switch:
     135          case Case:
     136          case Default:
     137          case While:
     138          case Do:
     139          case For:
     140          case Goto:
     141          case Break:
     142          case Continue:
     143          case Return:
     144          case Throw :
     145          case Try:
     146          case Catch:
     147          case Finally:
     148          case Asm:
     149          default:
     150                assert( false );
     151                return 0;
     152        } // switch
    69153}
    70154
     
    177261}
    178262
    179 Statement *build_compound( StatementNode *first ) {
    180         CompoundStmt *cs = new CompoundStmt( noLabels );
     263
     264CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
     265
     266CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
     267        if ( first ) {
     268                last = ( StatementNode *)( stmt->get_last());
     269        } else {
     270                last = 0;
     271        } // if
     272}
     273
     274CompoundStmtNode::~CompoundStmtNode() {
     275        delete first;
     276}
     277
     278void CompoundStmtNode::add_statement( StatementNode *stmt ) {
     279        if ( stmt != 0 ) {
     280                last->set_last( stmt );
     281                last = ( StatementNode *)( stmt->get_next());
     282        } // if
     283}
     284
     285void CompoundStmtNode::print( ostream &os, int indent ) const {
     286        if ( first ) {
     287                first->printList( os, indent+2 );
     288        } // if
     289}
     290
     291Statement *CompoundStmtNode::build() const {
     292        std::list<Label> labs;
     293        const std::list<std::string> &labels = get_labels();
     294
     295        if ( ! labels.empty() ) {
     296                std::back_insert_iterator< std::list<Label> > lab_it( labs );
     297                copy( labels.begin(), labels.end(), lab_it );
     298        } // if
     299
     300        CompoundStmt *cs = new CompoundStmt( labs );
    181301        buildList( first, cs->get_kids() );
    182302        return cs;
    183303}
    184304
    185 Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
     305
     306AsmStmtNode::AsmStmtNode( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
     307        voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
     308        type = Asm;
     309        if ( gotolabels ) {
     310                this->gotolabels = gotolabels->get_labels();
     311                delete gotolabels;
     312        } // if
     313}
     314
     315AsmStmtNode::~AsmStmtNode() {
     316        delete output; delete input; delete clobber;
     317}
     318
     319Statement *AsmStmtNode::build() const {
     320        std::list<Label> labs;
     321
     322        if ( ! get_labels().empty() ) {
     323                std::back_insert_iterator< std::list<Label> > lab_it( labs );
     324                copy( get_labels().begin(), get_labels().end(), lab_it );
     325        } // if
     326
    186327        std::list< Expression * > out, in;
    187328        std::list< ConstantExpr * > clob;
    188 
    189329        buildList( output, out );
    190330        buildList( input, in );
    191331        buildList( clobber, clob );
    192         return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
    193 }
     332        std::list< Label > gotolabs = gotolabels;
     333        return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
     334}
     335
     336// Statement *build_asm( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 ) {
     337//      std::list<Label> labs;
     338
     339//      if ( ! get_labels().empty() ) {
     340//              std::back_insert_iterator< std::list<Label> > lab_it( labs );
     341//              copy( get_labels().begin(), get_labels().end(), lab_it );
     342//      } // if
     343
     344//      std::list< Expression * > out, in;
     345//      std::list< ConstantExpr * > clob;
     346//      buildList( output, out );
     347//      buildList( input, in );
     348//      buildList( clobber, clob );
     349//      std::list< Label > gotolabs = gotolabels;
     350//      return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
     351// }
    194352
    195353// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.