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