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 | |
---|
10 | InitializerNode::InitializerNode( ExpressionNode *_expr, bool aggrp, ExpressionNode *des ) |
---|
11 | : expr( _expr ), aggregate( aggrp ), designator( des ), kids( 0 ) |
---|
12 | { |
---|
13 | if ( aggrp ) |
---|
14 | kids = dynamic_cast< InitializerNode *>( get_link() ); |
---|
15 | |
---|
16 | if ( kids != 0 ) |
---|
17 | set_link( 0 ); |
---|
18 | } |
---|
19 | |
---|
20 | InitializerNode::InitializerNode( InitializerNode *init, bool aggrp, ExpressionNode *des ) |
---|
21 | : aggregate( aggrp ), designator( des ), kids( 0 ) |
---|
22 | { |
---|
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 | { |
---|
44 | |
---|
45 | if ( designator != 0 ) |
---|
46 | { |
---|
47 | os << "designated by: ("; |
---|
48 | ExpressionNode *curdes = designator; |
---|
49 | while( curdes != 0){ |
---|
50 | curdes->printOneLine(os); |
---|
51 | curdes = (ExpressionNode *)(curdes->get_link()); |
---|
52 | if(curdes) os << ", "; |
---|
53 | } |
---|
54 | os << ")"; |
---|
55 | } |
---|
56 | |
---|
57 | if (expr) expr->printOneLine(os); |
---|
58 | } |
---|
59 | else // It's an aggregate |
---|
60 | { |
---|
61 | os << "[--"; |
---|
62 | if( next_init() != 0 ) |
---|
63 | next_init()->printOneLine(os); |
---|
64 | |
---|
65 | if (aggregate) os << "--]"; |
---|
66 | } |
---|
67 | |
---|
68 | InitializerNode * moreInit; |
---|
69 | if ( get_link() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_link() ) ) != 0) ) |
---|
70 | moreInit->printOneLine( os ); |
---|
71 | } |
---|
72 | |
---|
73 | Initializer *InitializerNode::build() const { |
---|
74 | // if ( get_expression() == 0 ) return 0; // XXX (?) |
---|
75 | |
---|
76 | if ( aggregate ) |
---|
77 | { |
---|
78 | assert( next_init() != 0 ); |
---|
79 | |
---|
80 | std::list< Initializer *> initlist; |
---|
81 | buildList<Initializer, InitializerNode>( next_init(), initlist ); |
---|
82 | |
---|
83 | std::list< Expression *> designlist; |
---|
84 | if ( designator != 0 ) |
---|
85 | buildList<Expression, ExpressionNode>( designator, designlist ); |
---|
86 | |
---|
87 | return new ListInit( initlist, designlist ); |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | std::list< Expression *> designators; |
---|
92 | |
---|
93 | if ( designator != 0 ) |
---|
94 | buildList<Expression, ExpressionNode>( designator, designators ); |
---|
95 | |
---|
96 | if ( get_expression() != 0) |
---|
97 | return new SingleInit( get_expression()->build(), designators ); |
---|
98 | } |
---|
99 | |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|