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 | |
---|
9 | #include <iostream> |
---|
10 | using namespace std; |
---|
11 | |
---|
12 | InitializerNode::InitializerNode( ExpressionNode *_expr, bool aggrp, ExpressionNode *des ) |
---|
13 | : expr( _expr ), aggregate( aggrp ), designator( des ), kids( 0 ) { |
---|
14 | if ( aggrp ) |
---|
15 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
16 | |
---|
17 | if ( kids != 0 ) |
---|
18 | set_link( 0 ); |
---|
19 | } |
---|
20 | |
---|
21 | InitializerNode::InitializerNode( InitializerNode *init, bool aggrp, ExpressionNode *des ) |
---|
22 | : expr( 0 ), aggregate( aggrp ), designator( des ), kids( 0 ) { |
---|
23 | if ( init != 0 ) |
---|
24 | set_link(init); |
---|
25 | |
---|
26 | if ( aggrp ) |
---|
27 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
28 | |
---|
29 | if ( kids != 0 ) |
---|
30 | set_next( 0 ); |
---|
31 | } |
---|
32 | |
---|
33 | InitializerNode::~InitializerNode() { |
---|
34 | delete expr; |
---|
35 | } |
---|
36 | |
---|
37 | void InitializerNode::print( std::ostream &os, int indent ) const { |
---|
38 | os << std::string(indent, ' ') << "Initializer expression" << std::endl; |
---|
39 | } |
---|
40 | |
---|
41 | void InitializerNode::printOneLine( std::ostream &os ) const { |
---|
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 | } // while |
---|
51 | os << ")"; |
---|
52 | } // if |
---|
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 << "--]"; |
---|
59 | } // if |
---|
60 | |
---|
61 | InitializerNode *moreInit; |
---|
62 | if ( get_link() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_link() ) ) != 0) ) |
---|
63 | moreInit->printOneLine( os ); |
---|
64 | } |
---|
65 | |
---|
66 | Initializer *InitializerNode::build() const { |
---|
67 | // if ( get_expression() == 0 ) return 0; // XXX (?) |
---|
68 | |
---|
69 | if ( aggregate ) { |
---|
70 | assert( next_init() != 0 ); |
---|
71 | |
---|
72 | std::list< Initializer *> initlist; |
---|
73 | buildList<Initializer, InitializerNode>( next_init(), initlist ); |
---|
74 | |
---|
75 | std::list< Expression *> designlist; |
---|
76 | |
---|
77 | if ( designator != 0 ) { |
---|
78 | buildList<Expression, ExpressionNode>( designator, designlist ); |
---|
79 | } // if |
---|
80 | |
---|
81 | return new ListInit( initlist, designlist ); |
---|
82 | } else { |
---|
83 | std::list< Expression *> designators; |
---|
84 | |
---|
85 | if ( designator != 0 ) |
---|
86 | buildList<Expression, ExpressionNode>( designator, designators ); |
---|
87 | |
---|
88 | if ( get_expression() != 0) |
---|
89 | return new SingleInit( get_expression()->build(), designators ); |
---|
90 | } // if |
---|
91 | |
---|
92 | return 0; |
---|
93 | } |
---|