Ignore:
Timestamp:
Aug 5, 2016, 3:56:24 PM (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:
35f9114
Parents:
71a3593
Message:

even more more refactoring of parser code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r71a3593 rd9e2280  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug  5 11:39:25 2016
    13 // Update Count     : 391
     12// Last Modified On : Fri Aug  5 15:07:19 2016
     13// Update Count     : 409
    1414//
    1515
     
    238238//##############################################################################
    239239
    240 static const char *opName[] = {
    241         "TupleC", "Comma", "TupleFieldSel", // "TuplePFieldSel", // n-adic
    242         // triadic
    243         "Cond", "NCond",
     240static const char *OperName[] = {
    244241        // diadic
    245242        "SizeOf", "AlignOf", "OffsetOf", "Attr", "?+?", "?-?", "?*?", "?/?", "?%?", "||", "&&",
    246243        "?|?", "?&?", "?^?", "Cast", "?<<?", "?>>?", "?<?", "?>?", "?<=?", "?>=?", "?==?", "?!=?",
    247244        "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?", "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?",
    248         "?[?]", "FieldSel", "PFieldSel", "...",
     245        "?[?]", "...",
    249246        // monadic
    250247        "+?", "-?", "AddressOf", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "&&"
    251248};
    252249
    253 OperatorNode::OperatorNode( Type t ) : type( t ) {}
    254 
    255 OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) {
    256 }
    257 
    258 OperatorNode::~OperatorNode() {}
    259 
    260 OperatorNode::Type OperatorNode::get_type( void ) const {
    261         return type;
    262 }
    263 
    264 void OperatorNode::printOneLine( std::ostream &os, int indent ) const {
    265         printDesignation( os );
    266         os << opName[ type ] << ' ';
    267 }
    268 
    269 void OperatorNode::print( std::ostream &os, int indent ) const{
    270         printDesignation( os );
    271         os << string( indent, ' ' ) << "Operator: " << opName[type] << endl;
    272         return;
    273 }
    274 
    275 const char *OperatorNode::get_typename( void ) const{
    276         return opName[ type ];
    277 }
    278 
    279 //##############################################################################
    280 
    281 CompositeExprNode::CompositeExprNode() : ExpressionNode(), function( 0 ), arguments( 0 ) {
    282 }
    283 
    284 CompositeExprNode::CompositeExprNode( const string *name_ ) : ExpressionNode( name_ ), function( 0 ), arguments( 0 ) {
    285 }
    286 
    287 CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ):
    288         function( f ), arguments( args ) {
    289 }
    290 
    291 CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2 ):
    292         function( f ), arguments( arg1 ) {
    293         arguments->set_link( arg2 );
    294 }
    295 
    296 CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : ExpressionNode( other ), function( maybeClone( other.function ) ), arguments( 0 ) {
    297         ParseNode *cur = other.arguments;
    298         while ( cur ) {
    299                 if ( arguments ) {
    300                         arguments->set_link( cur->clone() );
    301                 } else {
    302                         arguments = ( ExpressionNode*)cur->clone();
    303                 } // if
    304                 cur = cur->get_link();
    305         }
    306 }
    307 
    308 CompositeExprNode::~CompositeExprNode() {
    309         delete function;
    310         delete arguments;
    311 }
    312 
     250//##############################################################################
     251
     252CompositeExprNode::CompositeExprNode( Expression *expr ) : expr( expr ) {}
     253CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : expr( other.expr->clone() ) {}
     254CompositeExprNode::~CompositeExprNode() { delete expr; }
     255void CompositeExprNode::print( std::ostream &, int indent ) const { assert( false ); }
     256void CompositeExprNode::printOneLine( std::ostream &, int indent ) const { assert( false ); }
    313257
    314258Expression *build_cast( TypeValueNode * arg, ExpressionNode *expr_node ) {
     
    369313}
    370314
    371 Expression *build_unary_val( OperatorNode::Type op, ExpressionNode *expr_node ) {
     315Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ) {
    372316        std::list<Expression *> args;
    373317        args.push_back( maybeBuild<Expression>(expr_node) );
    374         return new UntypedExpr( new NameExpr( opName[ op ] ), args );
    375 }
    376 Expression *build_unary_ptr( OperatorNode::Type op, ExpressionNode *expr_node ) {
     318        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     319}
     320Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ) {
    377321        std::list<Expression *> args;
    378322        args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node) ) );
    379         return new UntypedExpr( new NameExpr( opName[ op ] ), args );
    380 }
    381 Expression *build_binary_val( OperatorNode::Type op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
     323        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     324}
     325Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    382326        std::list<Expression *> args;
    383327        args.push_back( maybeBuild<Expression>(expr_node1) );
    384328        args.push_back( maybeBuild<Expression>(expr_node2) );
    385         return new UntypedExpr( new NameExpr( opName[ op ] ), args );
    386 }
    387 Expression *build_binary_ptr( OperatorNode::Type op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
     329        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     330}
     331Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ) {
    388332        std::list<Expression *> args;
    389333        args.push_back( new AddressExpr( maybeBuild<Expression>(expr_node1) ) );
    390334        args.push_back( maybeBuild<Expression>(expr_node2) );
    391         return new UntypedExpr( new NameExpr( opName[ op ] ), args );
     335        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    392336}
    393337
     
    421365}
    422366
    423 CompositeExprNode2::CompositeExprNode2( Expression *expr ) : expr( expr ) {}
    424 CompositeExprNode2::CompositeExprNode2( const CompositeExprNode2 &other ) : expr( other.expr->clone() ) {}
    425 CompositeExprNode2::~CompositeExprNode2() { delete expr; }
    426 void CompositeExprNode2::print( std::ostream &, int indent ) const { assert( false ); }
    427 void CompositeExprNode2::printOneLine( std::ostream &, int indent ) const { assert( false ); }
    428 
    429 
    430 Expression *CompositeExprNode::build() const {
    431         OperatorNode *op;
    432         std::list<Expression *> args;
    433 
    434         buildList( get_args(), args );
    435 
    436         if ( ! ( op = dynamic_cast<OperatorNode *>( function ) ) ) { // function as opposed to operator
    437                 return new UntypedExpr( maybeBuild<Expression>(function), args, maybeBuild< Expression >( get_argName() ));
    438         } // if
    439 
    440         assert( ((void)"CompositeExprNode::build", false) );
    441         return nullptr;
    442 }
    443 
    444 void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const {
    445         printDesignation( os );
    446         os << "( ";
    447         function->printOneLine( os, indent );
    448         for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) {
    449                 cur->printOneLine( os, indent );
    450         } // for
    451         os << ") ";
    452 }
    453 
    454 void CompositeExprNode::print( std::ostream &os, int indent ) const {
    455         printDesignation( os );
    456         os << string( indent, ' ' ) << "Application of: " << endl;
    457         function->print( os, indent + ParseNode::indent_by );
    458 
    459         os << string( indent, ' ' ) ;
    460         if ( arguments ) {
    461                 os << "... on arguments: " << endl;
    462                 arguments->printList( os, indent + ParseNode::indent_by );
    463         } else
    464                 os << "... on no arguments: " << endl;
    465 }
    466 
    467 void CompositeExprNode::set_function( ExpressionNode *f ) {
    468         function = f;
    469 }
    470 
    471 void CompositeExprNode::set_args( ExpressionNode *args ) {
    472         arguments = args;
    473 }
    474 
    475 ExpressionNode *CompositeExprNode::get_function( void ) const {
    476         return function;
    477 }
    478 
    479 ExpressionNode *CompositeExprNode::get_args( void ) const {
    480         return arguments;
    481 }
    482 
    483 void CompositeExprNode::add_arg( ExpressionNode *arg ) {
    484         if ( arguments )
    485                 arguments->set_link( arg );
    486         else
    487                 set_args( arg );
     367Expression *build_range( ExpressionNode * low, ExpressionNode *high ) {
     368        Expression *low_cexpr = maybeBuild<Expression>( low );
     369        Expression *high_cexpr = maybeBuild<Expression>( high );
     370        return new RangeExpr( low_cexpr, high_cexpr );
    488371}
    489372
     
    676559}
    677560
    678 ExpressionNode *flattenCommas( ExpressionNode *list ) {
    679         if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) ) {
    680                 OperatorNode *op;
    681                 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) ) {
    682                         if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
    683                                 composite->add_arg( next );
    684                         return flattenCommas( composite->get_args() );
    685                 } // if
    686         } // if
    687 
    688         if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
    689                 list->set_next( flattenCommas( next ) );
    690 
    691         return list;
    692 }
    693 
    694 ExpressionNode *tupleContents( ExpressionNode *tuple ) {
    695         if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( tuple ) ) {
    696                 OperatorNode *op = 0;
    697                 if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) )
    698                         return composite->get_args();
    699         } // if
    700         return tuple;
    701 }
    702 
    703561// Local Variables: //
    704562// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.