comparison lwcc/tree.c @ 312:41118fb0a8f2 ccdev

Add no-op parser and parse tree infrastructure
author William Astle <lost@l-w.ca>
date Sat, 21 Sep 2013 18:43:39 -0600
parents
children a3e277c58df9
comparison
equal deleted inserted replaced
311:7957e90d0a35 312:41118fb0a8f2
1 /*
2 lwcc/tree.c
3
4 Copyright © 2013 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <stdarg.h>
23 #include <string.h>
24 #include <lw_alloc.h>
25
26 #include "tree.h"
27
28 static char *node_names[] = {
29 "NONE",
30 "PROGRAM",
31 };
32
33
34
35 node_t *node_create(int type, ...)
36 {
37 node_t *r;
38 int nargs = 0;
39 va_list args;
40
41 va_start(args, type);
42 r = lw_alloc(sizeof(node_t));
43 memset(r, sizeof(node_t), 0);
44 r -> type = type;
45
46 switch (type)
47 {
48 }
49
50 while (nargs--)
51 {
52 node_addchild(r, va_arg(args, node_t *));
53 }
54 va_end(args);
55 return r;
56 }
57
58 void node_destroy(node_t *node)
59 {
60 node_t *n;
61
62 while (node -> children)
63 {
64 n = node -> children -> next_child;
65 node_destroy(node -> children);
66 node -> children = n;
67 }
68 lw_free(node -> strval);
69 lw_free(node);
70 }
71
72 void node_addchild(node_t *node, node_t *nn)
73 {
74 node_t *tmp;
75
76 nn -> parent = node;
77 nn -> next_child = NULL;
78 if (node -> children)
79 {
80 for (tmp = node -> children; tmp -> next_child; tmp = tmp -> next_child)
81 /* do nothing */ ;
82 tmp -> next_child = nn;
83 }
84 else
85 {
86 node -> children = nn;
87 }
88 }
89
90 void node_removechild(node_t *node, node_t *nn)
91 {
92 node_t **pp;
93 node_t *np;
94
95 if (!node)
96 node = nn -> parent;
97
98 pp = &(node -> children);
99 for (np = node -> children; np; np = np -> next_child)
100 {
101 if (np -> next_child == nn)
102 break;
103 pp = &((*pp) -> next_child);
104 }
105 if (!np)
106 return;
107
108 *pp = nn -> next_child;
109 nn -> parent = NULL;
110 nn -> next_child = NULL;
111 }
112
113 void node_removechild_destroy(node_t *node, node_t *nn)
114 {
115 node_removechild(node, nn);
116 node_destroy(nn);
117 }
118
119 static void node_display_aux(node_t *node, FILE *f, int level)
120 {
121 node_t *nn;
122 int i;
123
124 for (i = 0; i < level * 4; i++)
125 fputc(' ', f);
126 fprintf(f, "(%s ", node_names[node -> type]);
127 fputc('\n', f);
128 for (nn = node -> children; nn; nn = nn -> next_child)
129 node_display_aux(nn, f, level + 1);
130 for (i = 0; i < level * 4; i++)
131 fputc(' ', f);
132 fputc(')', f);
133 fputc('\n', f);
134 }
135
136 void node_display(node_t *node, FILE *f)
137 {
138 node_display_aux(node, f, 0);
139 }