Changeset e869d663


Ignore:
Timestamp:
Aug 12, 2015, 2:26:44 PM (9 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
43ee2a8, d60ccbf
Parents:
d58ebf3
Message:

designator parsing and AST building

Location:
src/Parser
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    rd58ebf3 re869d663  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 11 15:13:48 2015
    13 // Update Count     : 200
     11// Last Modified By : Rob Schluntz
     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 ) {
     
    290290DesignatorNode::DesignatorNode( ExpressionNode *expr, bool isArrayIndex ) : isArrayIndex( isArrayIndex ) {
    291291        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
    292309}
    293310
     
    295312}
    296313
     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
    297326Expression *DesignatorNode::build() const {
     327        Expression * ret = get_argName()->build();
     328
    298329        if ( isArrayIndex ) {
    299                 return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) );
    300         } else {
    301                 return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) );
    302         } // if
     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;
    303337}
    304338
  • src/Parser/ParseNode.cc

    rd58ebf3 re869d663  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:26:29 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jul 30 14:55:54 2015
    13 // Update Count     : 26
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Aug 12 13:26:00 2015
     13// Update Count     : 36
    1414//
    1515
     
    2020int ParseNode::indent_by = 4;
    2121
    22 ParseNode::ParseNode() : name( 0 ), next( 0 ) {};
    23 ParseNode::ParseNode( const string *name_ ) : name( name_ ), next( 0 ) {}
     22ParseNode::ParseNode() : next( 0 ) {};
     23ParseNode::ParseNode( const string *name ) : name( *name ), next( 0 ) { delete name; }
     24ParseNode::ParseNode( const string &name ) : name( name ), next( 0 ) { }
    2425
    2526ParseNode::~ParseNode() {
    26         delete next; delete name;
     27        delete next;
    2728};
    2829
  • src/Parser/ParseNode.h

    rd58ebf3 re869d663  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:28:16 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 11 15:11:29 2015
    13 // Update Count     : 145
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Aug 12 13:27:11 2015
     13// Update Count     : 172
    1414//
    1515
     
    4040        ParseNode();
    4141        ParseNode( const std::string * );
     42        ParseNode( const std::string & );  // for copy constructing subclasses
    4243        virtual ~ParseNode();
    4344
     
    4950        virtual ParseNode *clone() const { return 0; };
    5051
    51         const std::string &get_name() const { return *name; }
     52        const std::string &get_name() const { return name; }
     53        void set_name( const std::string &newValue ) { name = newValue; }
     54
    5255        virtual void print( std::ostream &, int indent = 0 ) const;
    5356        virtual void printList( std::ostream &, int indent = 0 ) const;
     
    5558        ParseNode &operator,( ParseNode &);
    5659  protected:
    57         const std::string *name;
     60        std::string name;
    5861        ParseNode *next;
    5962        static int indent_by;
Note: See TracChangeset for help on using the changeset viewer.