[51b7345] | 1 | #include "ParseNode.h" |
---|
| 2 | #include "SynTree/Expression.h" |
---|
| 3 | #include "SynTree/Initializer.h" |
---|
| 4 | #include "utility.h" |
---|
| 5 | #include "SemanticError.h" |
---|
| 6 | // #include <cstdlib> // for strtol |
---|
| 7 | #include <cassert> |
---|
| 8 | |
---|
[d9a0e76] | 9 | #include <iostream> |
---|
| 10 | using namespace std; |
---|
[51b7345] | 11 | |
---|
| 12 | InitializerNode::InitializerNode( ExpressionNode *_expr, bool aggrp, ExpressionNode *des ) |
---|
[3848e0e] | 13 | : expr( _expr ), aggregate( aggrp ), designator( des ), kids( 0 ) { |
---|
| 14 | if ( aggrp ) |
---|
| 15 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
[51b7345] | 16 | |
---|
[3848e0e] | 17 | if ( kids != 0 ) |
---|
| 18 | set_link( 0 ); |
---|
[51b7345] | 19 | } |
---|
| 20 | |
---|
| 21 | InitializerNode::InitializerNode( InitializerNode *init, bool aggrp, ExpressionNode *des ) |
---|
[3848e0e] | 22 | : aggregate( aggrp ), designator( des ), kids( 0 ) { |
---|
| 23 | if ( init != 0 ) |
---|
| 24 | set_link(init); |
---|
[51b7345] | 25 | |
---|
[3848e0e] | 26 | if ( aggrp ) |
---|
| 27 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
[51b7345] | 28 | |
---|
[3848e0e] | 29 | if ( kids != 0 ) |
---|
| 30 | set_next( 0 ); |
---|
[51b7345] | 31 | } |
---|
| 32 | |
---|
| 33 | InitializerNode::~InitializerNode() { |
---|
[3848e0e] | 34 | delete expr; |
---|
[51b7345] | 35 | } |
---|
| 36 | |
---|
| 37 | void InitializerNode::print( std::ostream &os, int indent ) const { |
---|
[3848e0e] | 38 | os << std::string(indent, ' ') << "Initializer expression" << std::endl; |
---|
[51b7345] | 39 | } |
---|
| 40 | |
---|
| 41 | void InitializerNode::printOneLine( std::ostream &os ) const { |
---|
[3848e0e] | 42 | if ( ! aggregate ) { |
---|
| 43 | if ( designator != 0 ) { |
---|
| 44 | os << "designated by: ("; |
---|
| 45 | ExpressionNode *curdes = designator; |
---|
| 46 | while( curdes != 0) { |
---|
| 47 | curdes->printOneLine(os); |
---|
| 48 | curdes = (ExpressionNode *)(curdes->get_link()); |
---|
| 49 | if ( curdes ) os << ", "; |
---|
| 50 | } |
---|
| 51 | os << ")"; |
---|
[51b7345] | 52 | } |
---|
[3848e0e] | 53 | if ( expr ) expr->printOneLine(os); |
---|
| 54 | } else { // It's an aggregate |
---|
| 55 | os << "[--"; |
---|
| 56 | if ( next_init() != 0 ) |
---|
| 57 | next_init()->printOneLine(os); |
---|
| 58 | if (aggregate) os << "--]"; |
---|
[51b7345] | 59 | } |
---|
| 60 | |
---|
[3848e0e] | 61 | InitializerNode *moreInit; |
---|
| 62 | if ( get_link() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_link() ) ) != 0) ) |
---|
| 63 | moreInit->printOneLine( os ); |
---|
[51b7345] | 64 | } |
---|
| 65 | |
---|
| 66 | Initializer *InitializerNode::build() const { |
---|
[3848e0e] | 67 | // if ( get_expression() == 0 ) return 0; // XXX (?) |
---|
[51b7345] | 68 | |
---|
[3848e0e] | 69 | if ( aggregate ) { |
---|
| 70 | assert( next_init() != 0 ); |
---|
[51b7345] | 71 | |
---|
[3848e0e] | 72 | std::list< Initializer *> initlist; |
---|
| 73 | buildList<Initializer, InitializerNode>( next_init(), initlist ); |
---|
[51b7345] | 74 | |
---|
[3848e0e] | 75 | std::list< Expression *> designlist; |
---|
[d9a0e76] | 76 | |
---|
| 77 | if ( designator != 0 ) { |
---|
[3848e0e] | 78 | buildList<Expression, ExpressionNode>( designator, designlist ); |
---|
[d9a0e76] | 79 | } // if |
---|
[51b7345] | 80 | |
---|
[3848e0e] | 81 | return new ListInit( initlist, designlist ); |
---|
| 82 | } else { |
---|
| 83 | std::list< Expression *> designators; |
---|
[51b7345] | 84 | |
---|
[3848e0e] | 85 | if ( designator != 0 ) |
---|
| 86 | buildList<Expression, ExpressionNode>( designator, designators ); |
---|
[51b7345] | 87 | |
---|
[3848e0e] | 88 | if ( get_expression() != 0) |
---|
| 89 | return new SingleInit( get_expression()->build(), designators ); |
---|
[51b7345] | 90 | } |
---|
| 91 | |
---|
[3848e0e] | 92 | return 0; |
---|
[51b7345] | 93 | } |
---|