Changes in / [a1d5d2a:b5b0907]


Ignore:
Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    ra1d5d2a rb5b0907  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun 13 07:12:27 2015
    13 // Update Count     : 129
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Mon Jun 15 12:43:21 2015
     13// Update Count     : 138
    1414//
    1515
     
    4444        }
    4545
    46         CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
    47 
    48         CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
    49                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     46        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
     47          return output << string( cg.cur_indent, ' ' );
     48        }
     49
     50        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
     51                return indent( output );
     52        }
     53
     54        CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
     55
     56        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
     57                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5058                //output << std::string( init );
    5159        }
    5260
    53         CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
    54                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     61        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
     62                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5563                //output << std::string( init );
    5664        }
     
    6371                } // if
    6472        }
    65   
     73 
    6674        //*** Declarations
    6775        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
     
    105113
    106114                if ( ! memb.empty() ) {
    107                         output << endl << string( cur_indent, ' ' ) << "{" << endl;
     115                        output << endl << indent << "{" << endl;
    108116
    109117                        cur_indent += CodeGenerator::tabsize;
    110118                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    111                                 output << string( cur_indent, ' ' );
     119                                output << indent;
    112120                                (*i)->accept(*this );
    113121                                output << ";" << endl;
     
    116124                        cur_indent -= CodeGenerator::tabsize;
    117125
    118                         output << string( cur_indent, ' ' ) << "}";
     126                        output << indent << "}";
    119127                } // if
    120128        }
     
    139147
    140148                if ( ! memb.empty() ) {
    141                         output << endl << "{" << endl;
     149                        output << " {" << endl;
    142150
    143151                        cur_indent += CodeGenerator::tabsize;
     
    145153                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    146154                                assert( obj );
    147                                 output << string( cur_indent, ' ' ) << mangleName( obj );
     155                                output << indent << mangleName( obj );
    148156                                if ( obj->get_init() ) {
    149157                                        output << " = ";
     
    155163                        cur_indent -= CodeGenerator::tabsize;
    156164
    157                         output << "}" << endl;
     165                        output << indent << "}";
    158166                } // if
    159167        }
     
    445453
    446454                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
    447                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
     455                        output << indent << printLabels( (*i)->get_labels() );
    448456                        (*i)->accept(*this );
    449457
     
    455463                cur_indent -= CodeGenerator::tabsize;
    456464
    457                 output << string( cur_indent, ' ' ) << "}";
     465                output << indent << "}";
    458466        }
    459467
     
    495503                cur_indent -= CodeGenerator::tabsize;
    496504
    497                 output << string( cur_indent, ' ' ) << "}";
     505                output << indent << "}";
    498506        }
    499507
    500508        void CodeGenerator::visit( CaseStmt *caseStmt ) {
    501                 output << string( cur_indent, ' ' );
     509                output << indent;
    502510                if ( caseStmt->isDefault()) {
    503511                        output << "default";
     
    512520                cur_indent += CodeGenerator::tabsize;
    513521                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    514                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
     522                        output << indent << printLabels( (*i)->get_labels() )  ;
    515523                        (*i)->accept(*this );
    516524                        output << endl;
     
    565573                whileStmt->get_body()->accept( *this );
    566574
    567                 output << string( cur_indent, ' ' );
     575                output << indent;
    568576
    569577                if ( whileStmt->get_isDoWhile() ) {
     
    597605
    598606        void CodeGenerator::visit( NullStmt *nullStmt ) {
    599                 //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
     607                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    600608                output << "/* null statement */ ;";
    601609        }
  • src/CodeGen/CodeGenerator.h

    ra1d5d2a rb5b0907  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jun  8 14:34:43 2015
    13 // Update Count     : 15
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Thu Jun 11 13:24:23 2015
     13// Update Count     : 23
    1414//
    1515
     
    8080
    8181                template< class Iterator > void genCommaList( Iterator begin, Iterator end );
     82
     83                struct Indenter {
     84                        Indenter(CodeGenerator &cg) : cg(cg) {}
     85                        CodeGenerator & cg;
     86                        std::ostream& operator()(std::ostream & os);
     87                };
    8288          private:
     89
     90                Indenter indent;
    8391                int cur_indent;
    8492                bool insideFunction;
  • src/Parser/ExpressionNode.cc

    ra1d5d2a rb5b0907  
    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 : Mon Jun  8 17:33:40 2015
    13 // Update Count     : 147
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 10 14:57:52 2015
     13// Update Count     : 151
    1414//
    1515
     
    252252                                                                                   new ConstantExpr(
    253253                                                                                           Constant( new BasicType( q, BasicType::UnsignedInt ),
    254                                                                                                                  toString( value.size() + 1 ) ) ),  // account for '\0'
     254                                                                                                                 toString( value.size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
    255255                                                                                   false, false );
    256256                        return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) );
  • src/ResolvExpr/Resolver.cc

    ra1d5d2a rb5b0907  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 12:17:01 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jun  7 21:50:37 2015
    13 // Update Count     : 23
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 10 16:11:19 2015
     13// Update Count     : 77
    1414//
    1515
     
    3737                virtual void visit( ObjectDecl *functionDecl );
    3838                virtual void visit( TypeDecl *typeDecl );
     39                virtual void visit( ArrayType * at );
    3940
    4041                virtual void visit( ExprStmt *exprStmt );
     
    157158                initContext = new_type;
    158159                SymTab::Indexer::visit( objectDecl );
    159 
    160                 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){
    161                         if ( at->get_dimension() ) {
    162                                 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    163                                 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
    164                                 Expression *newExpr = findSingleExpression( castExpr, *this );
    165                                 delete at->get_dimension();
    166                                 at->set_dimension( newExpr );
    167                         }
    168                 }
     160        }
     161
     162        void Resolver::visit( ArrayType * at ) {
     163                if ( at->get_dimension() ) {
     164                        BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
     165                        CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
     166                        Expression *newExpr = findSingleExpression( castExpr, *this );
     167                        delete at->get_dimension();
     168                        at->set_dimension( newExpr );
     169
     170                }
     171                Visitor::visit( at );
    169172        }
    170173 
     
    270273                        returnStmt->set_expr( newExpr );
    271274                } // if
     275        }
     276
     277        template< typename T >
     278        bool isCharType( T t ) {
     279                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
     280                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
     281                                bt->get_kind() == BasicType::UnsignedChar;
     282                }
     283                return false;
    272284        }
    273285
     
    296308                        delete castExpr;
    297309                        singleInit->set_value( newExpr );
     310
     311                        // check if initializing type is char[]
     312                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     313                                if ( isCharType( at->get_base() ) ) {
     314                                        // check if the resolved type is char *
     315                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
     316                                                if ( isCharType( pt->get_base() ) ) {
     317                                                        // strip cast if we're initializing a char[] with a char *, e.g.
     318                                                        // char x[] = "hello";
     319                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
     320                                                        singleInit->set_value( ce->get_arg() );
     321                                                        ce->set_arg( NULL );
     322                                                        delete ce;                                                                     
     323                                                }
     324                                        }
     325                                }
     326                        }
    298327                } // if
    299328//      singleInit->get_value()->accept( *this );
     
    301330
    302331        void Resolver::visit( ListInit *listInit ) {
    303                 Visitor::visit(listInit);
     332                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     333                        std::list< Initializer * >::iterator iter( listInit->begin_initializers() );
     334                        for ( ; iter != listInit->end_initializers(); ++iter ) {
     335                                initContext = at->get_base();
     336                                (*iter)->accept( *this );
     337                        } // for
     338                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
     339                        DeclarationWithType *dt = dynamic_cast< DeclarationWithType * >( *st->get_baseUnion()->get_members().begin() );
     340                        initContext = dt->get_type();
     341                        (*listInit->begin_initializers())->accept( *this );
     342                } else {
     343                        // basic types are handled here
     344                        Visitor::visit( listInit );
     345                }
     346
    304347#if 0
    305348                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
  • src/SynTree/Constant.cc

    ra1d5d2a rb5b0907  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jun  7 08:45:30 2015
    13 // Update Count     : 5
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 10 14:41:03 2015
     13// Update Count     : 8
    1414//
    1515
     
    2727
    2828void Constant::print( std::ostream &os ) const {
    29         os << value;
     29        os << "(" << value;
    3030        if ( type ) {
    31                 os << " ";
     31                os << ": ";
    3232                type->print( os );
    3333        } // if
     34  os << ")";
    3435}
    3536
Note: See TracChangeset for help on using the changeset viewer.