Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r1869adf re869d663  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jun 24 16:20:00 2015
    13 // Update Count     : 158
     12// Last Modified On : Wed Aug 12 13:51:11 2015
     13// Update Count     : 254
    1414//
    1515
     
    3232ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {}
    3333
    34 ExpressionNode::ExpressionNode( const string *name_ ) : ParseNode( name_ ), argName( 0 ) {}
     34ExpressionNode::ExpressionNode( const string *name ) : ParseNode( name ), argName( 0 ) {}
    3535
    3636ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) {
     
    4242}
    4343
    44 ExpressionNode * ExpressionNode::set_asArgName( const std::string *aName ) {
     44ExpressionNode * ExpressionNode::set_argName( const std::string *aName ) {
    4545        argName = new VarRefNode( aName );
    4646        return this;
    4747}
    4848
    49 ExpressionNode * ExpressionNode::set_asArgName( ExpressionNode *aDesignator ) {
     49ExpressionNode * ExpressionNode::set_argName( ExpressionNode *aDesignator ) {
    5050        argName = aDesignator;
    5151        return this;
     
    210210        assert( type == String );
    211211
    212         // "abc" "def" "ghi" => "abcdefghi", so remove new text from quotes and insert before last quote in old string.
     212        // "abc" "def" "ghi" => "abcdefghi", remove new text from quotes and insert before last quote in old string.
    213213        value.insert( value.length() - 1, newValue->substr( 1, newValue->length() - 2 ) );
    214214       
     
    284284        os << "Variable: " << get_name();
    285285        os << endl;
     286}
     287
     288//##############################################################################
     289
     290DesignatorNode::DesignatorNode( ExpressionNode *expr, bool isArrayIndex ) : isArrayIndex( isArrayIndex ) {
     291        set_argName( expr );
     292        assert( get_argName() );
     293
     294        if ( ! isArrayIndex ) {
     295                if ( VarRefNode * var = dynamic_cast< VarRefNode * >( expr ) ) {
     296
     297                        stringstream ss( var->get_name() );
     298                        double value;
     299                        if ( ss >> value ) {
     300                                // this is a floating point constant. It MUST be
     301                                // ".0" or ".1", otherwise the program is invalid
     302                                if ( ! (var->get_name() == ".0" || var->get_name() == ".1") ) {
     303                                        throw SemanticError( "invalid designator name: " + var->get_name() );
     304                                } // if
     305                                var->set_name( var->get_name().substr(1) );
     306                        } // if
     307                } // if
     308        } // if
     309}
     310
     311DesignatorNode::DesignatorNode( const DesignatorNode &other ) : ExpressionNode( other ), isArrayIndex( other.isArrayIndex ) {
     312}
     313
     314class DesignatorFixer : public Mutator {
     315public:
     316        virtual Expression* mutate( NameExpr *nameExpr ) {
     317                if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) {
     318                        Constant val( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nameExpr->get_name() );
     319                        delete nameExpr;
     320                        return new ConstantExpr( val );
     321                }
     322                return nameExpr;
     323        }
     324};
     325
     326Expression *DesignatorNode::build() const {
     327        Expression * ret = get_argName()->build();
     328
     329        if ( isArrayIndex ) {
     330                // need to traverse entire structure and change any instances of 0 or 1 to
     331                // ConstantExpr
     332                DesignatorFixer fixer;
     333                ret = ret->acceptMutator( fixer );
     334        } // if
     335
     336        return ret;
     337}
     338
     339void DesignatorNode::printOneLine( std::ostream &os, int indent ) const {
     340        if ( get_argName() ) {
     341                if ( isArrayIndex ) {
     342                        os << "[";
     343                        get_argName()->printOneLine( os, indent );
     344                        os << "]";
     345                } else {
     346                        os << ".";
     347                        get_argName()->printOneLine( os, indent );
     348                }
     349        } // if
     350}
     351
     352void DesignatorNode::print( std::ostream &os, int indent ) const {
     353        if ( get_argName() ) {
     354                if ( isArrayIndex ) {
     355                        os << "[";
     356                        get_argName()->print( os, indent );
     357                        os << "]";
     358                } else {
     359                        os << ".";
     360                        get_argName()->print( os, indent );
     361                }
     362        } // if
    286363}
    287364
     
    330407//##############################################################################
    331408
    332 CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) {
     409CompositeExprNode::CompositeExprNode() : ExpressionNode(), function( 0 ), arguments( 0 ) {
    333410}
    334411
     
    541618                {
    542619                        assert( args.size() == 3);
    543                         std::list< Expression* >::const_iterator i = args.begin();
     620                        std::list< Expression * >::const_iterator i = args.begin();
    544621                        Expression *arg1 = notZeroExpr( *i++ );
    545622                        Expression *arg2 = *i++;
     
    552629                {
    553630                        assert( args.size() == 2);
    554                         std::list< Expression* >::const_iterator i = args.begin();
     631                        std::list< Expression * >::const_iterator i = args.begin();
    555632                        Expression *ret = *i++;
    556633                        while ( i != args.end() ) {
     
    617694                set_args( arg );
    618695}
     696
     697//##############################################################################
     698
     699Expression *AsmExprNode::build() const {
     700        return new AsmExpr( maybeBuild< Expression >( inout ), (ConstantExpr *)constraint->build(), operand->build() );
     701}
     702
     703void AsmExprNode::print( std::ostream &os, int indent ) const {
     704        os << string( indent, ' ' ) << "Assembler Expression:" << endl;
     705        if ( inout ) {
     706                os << string( indent, ' ' ) << "inout: " << std::endl;
     707                inout->print( os, indent + 2 );
     708        } // if
     709        if ( constraint ) {
     710                os << string( indent, ' ' ) << "constraint: " << std::endl;
     711                constraint->print( os, indent + 2 );
     712        } // if
     713        if ( operand ) {
     714                os << string( indent, ' ' ) << "operand: " << std::endl;
     715                operand->print( os, indent + 2 );
     716        } // if
     717}
     718
     719void AsmExprNode::printOneLine( std::ostream &os, int indent ) const {
     720        printDesignation( os );
     721        os << "( ";
     722        if ( inout ) inout->printOneLine( os, indent + 2 );
     723        os << ", ";
     724        if ( constraint ) constraint->printOneLine( os, indent + 2 );
     725        os << ", ";
     726        if ( operand ) operand->printOneLine( os, indent + 2 );
     727        os << ") ";
     728}
     729
     730//##############################################################################
     731
     732void LabelNode::print( std::ostream &os, int indent ) const {}
     733
     734void LabelNode::printOneLine( std::ostream &os, int indent ) const {}
    619735
    620736//##############################################################################
Note: See TracChangeset for help on using the changeset viewer.