10 static struct node
*nodestack
[1024];
11 static int nodecount
= 0;
12 static int parse_error
= 0;
14 static int list_nodetype(enum nodetype nodetype
)
26 case AST_INITIALIZERLIST
:
29 case AST_STRUCTDECLLIST
:
31 case AST_STRUCTQUALLIST
:
32 case AST_TYPEQUALLIST
:
39 static int list_size(struct node
*node
)
42 struct node
*cur
= node
;
44 if (!list_nodetype(node
->type
) || !node
->count
)
46 preorder
= cur
->children
[0]->type
== node
->type
;
47 while (cur
->type
== node
->type
) {
52 cur
= cur
->children
[0];
54 cur
= cur
->children
[1];
59 static void node_free_base(struct node
*node
, int children
)
61 if (children
&& node
->children
) {
63 for (i
= 0; i
< node
->count
; i
++)
64 node_free(node
->children
[i
]);
72 static void linear_lists(struct node
*node
)
74 int n
= list_size(node
);
77 struct node
*cur
= node
;
78 struct node
**newchildren
= xmalloc(n
* sizeof(struct node
*));
79 int preorder
= cur
->children
[0]->type
== node
->type
;
80 for (i
= 0; i
< n
; i
++) {
81 struct node
*old
= cur
;
84 newchildren
[n
- i
- 1] = cur
->children
[1];
86 newchildren
[n
- i
- 1] = cur
->children
[0];
87 cur
= cur
->children
[0];
89 newchildren
[i
] = cur
->children
[0];
90 cur
= cur
->children
[cur
->count
- 1];
92 if (old
!= node
&& i
< n
- 1)
93 node_free_base(old
, 0);
95 for (i
= 0; i
< n
; i
++)
96 newchildren
[i
]->parent
= node
;
98 node
->children
= newchildren
;
101 for (i
= 0; i
< node
->count
; i
++)
102 linear_lists(node
->children
[i
]);
105 static void error_happened()
109 for (i
= 0; i
< nodecount
; i
++)
110 node_free(nodestack
[i
]);
114 struct node
*parse(char *filename
)
116 FILE *file
= fopen(filename
, "r");
123 reset_tokenizer(filename
);
124 if (parse_error
|| nodecount
!= 1) {
125 fprintf(stderr
, "ERROR: %d nodes on the stack\n", nodecount
);
129 linear_lists(nodestack
[0]);
130 return nodestack
[--nodecount
];
133 struct node
*push_node(enum nodetype type
, long start
, long end
, int nchild
)
136 if (parse_error
|| nchild
> nodecount
) {
140 node
= xmalloc(sizeof(struct node
));
141 memset(node
, 0, sizeof(struct node
));
147 node
->count
= nchild
;
148 node
->children
= xmalloc(nchild
* sizeof(struct node
*));
149 for (i
= nchild
- 1; i
>= 0; i
--) {
150 struct node
*child
= nodestack
[--nodecount
];
151 node
->children
[i
] = child
;
152 child
->parent
= node
;
155 nodestack
[nodecount
++] = node
;
159 struct node
*push_node_name(enum nodetype type
, long start
, long end
, char *name
)
161 struct node
*node
= push_node(type
, start
, end
, 0);
162 if (parse_error
|| !node
)
164 node
->data
= xmalloc(strlen(name
) + 1);
165 strcpy(node
->data
, name
);
169 struct node
*push_decl(enum nodetype type
, long start
, long end
,
170 int nchild
, enum decltype decltype)
172 struct node
*node
= push_node(type
, start
, end
, nchild
);
173 if (parse_error
|| !node
)
175 node
->data
= xmalloc(sizeof(decltype));
176 *(enum decltype *) node
->data
= decltype;
180 void node_free(struct node
*node
)
182 node_free_base(node
, 1);