Mercurial > hg > index.cgi
annotate lwcc/tree.c @ 560:dba08c7dff96
Fix off by one handling MOD directive optional arguments
Fixes an off by one in handling the optional arguments to the MOD directive
as of changeset 928c033c0cd0.
Thanks to Alex Evans <varmfskii@gmail.com> for reporting the problem and a
suggested fix. This fix is different but should be more stable should the
rest of the parsing code be refactored in the future.
author | William Astle <lost@l-w.ca> |
---|---|
date | Fri, 22 Sep 2023 12:15:09 -0600 |
parents | 7e8298f7bc0a |
children |
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> |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
25 #include <lw_string.h> |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
26 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
27 #include "tree.h" |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
28 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
29 static char *node_names[] = { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
30 "NONE", |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
31 "PROGRAM", |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
32 "DECL", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
33 "TYPE_CHAR", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
34 "TYPE_SHORT", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
35 "TYPE_INT", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
36 "TYPE_LONG", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
37 "TYPE_LONGLONG", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
38 "IDENT", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
39 "TYPE_PTR", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
40 "TYPE_SCHAR", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
41 "TYPE_UCHAR", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
42 "TYPE_USHORT", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
43 "TYPE_UINT", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
44 "TYPE_ULONG", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
45 "TYPE_ULONGLONG", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
46 "TYPE_VOID", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
47 "TYPE_FLOAT", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
48 "TYPE_DOUBLE", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
49 "TYPE_LDOUBLE", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
50 "FUNDEF", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
51 "FUNDECL", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
52 "FUNARGS", |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
53 "BLOCK", |
498
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
54 "STMT_RETURN", |
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
55 "CONST_INT", |
501
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
56 "OPER_PLUS", |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
57 "OPER_MINUS", |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
58 "OPER_TIMES", |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
59 "OPER_DIVIDE", |
502 | 60 "OPER_MOD", |
61 "OPER_COND", | |
62 "OPER_FNCALL", | |
63 "OPER_SUBSCRIPT", | |
64 "OPER_POSTINC", | |
65 "OPER_POSTDEC", | |
66 "OPER_PTRMEM", | |
67 "OPER_OBJMEM", | |
68 "OPER_LSH", | |
69 "OPER_RSH", | |
70 "OPER_LT", | |
71 "OPER_LE", | |
72 "OPER_GT", | |
73 "OPER_GE", | |
74 "OPER_EQ", | |
75 "OPER_NE", | |
76 "OPER_BWAND", | |
77 "OPER_BWXOR", | |
78 "OPER_BWOR", | |
79 "OPER_BAND", | |
80 "OPER_BOR", | |
81 "OPER_ASS", | |
82 "OPER_ADDASS", | |
83 "OPER_SUBASS", | |
84 "OPER_MULASS", | |
85 "OPER_DIVASS", | |
86 "OPER_MODASS", | |
87 "OPER_LSHASS", | |
88 "OPER_RSHASS", | |
89 "OPER_BWANDASS", | |
90 "OPER_BWXORASS", | |
91 "OPER_BWORASS", | |
92 "OPER_COMMA", | |
506
7e8298f7bc0a
Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents:
502
diff
changeset
|
93 "TYPECAST", |
312
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
96 |
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 node_t *node_create(int type, ...) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
99 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
100 node_t *r; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
101 int nargs = 0; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
102 va_list args; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
103 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
104 va_start(args, type); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
105 r = lw_alloc(sizeof(node_t)); |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
106 memset(r, 0, sizeof(node_t)); |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
107 r -> type = type; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
108 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
109 switch (type) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
110 { |
501
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
111 case NODE_OPER_PLUS: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
112 case NODE_OPER_MINUS: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
113 case NODE_OPER_TIMES: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
114 case NODE_OPER_DIVIDE: |
502 | 115 case NODE_OPER_MOD: |
116 case NODE_OPER_BWAND: | |
117 case NODE_OPER_BWXOR: | |
118 case NODE_OPER_BWOR: | |
119 case NODE_OPER_BAND: | |
120 case NODE_OPER_BOR: | |
121 case NODE_OPER_SUBSCRIPT: | |
122 case NODE_OPER_PTRMEM: | |
123 case NODE_OPER_OBJMEM: | |
124 case NODE_OPER_ASS: | |
125 case NODE_OPER_ADDASS: | |
126 case NODE_OPER_SUBASS: | |
127 case NODE_OPER_MULASS: | |
128 case NODE_OPER_DIVASS: | |
129 case NODE_OPER_MODASS: | |
130 case NODE_OPER_LSH: | |
131 case NODE_OPER_LSHASS: | |
132 case NODE_OPER_RSH: | |
133 case NODE_OPER_RSHASS: | |
134 case NODE_OPER_BWANDASS: | |
135 case NODE_OPER_BWORASS: | |
136 case NODE_OPER_BWXORASS: | |
137 case NODE_OPER_LT: | |
138 case NODE_OPER_LE: | |
139 case NODE_OPER_GT: | |
140 case NODE_OPER_GE: | |
141 case NODE_OPER_EQ: | |
142 case NODE_OPER_NE: | |
143 case NODE_OPER_COMMA: | |
144 nargs = 2; | |
145 break; | |
146 | |
147 case NODE_OPER_FNCALL: | |
501
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
148 nargs = 2; |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
149 break; |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
150 |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
151 case NODE_DECL: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
152 nargs = 2; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
153 break; |
502 | 154 |
506
7e8298f7bc0a
Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents:
502
diff
changeset
|
155 case NODE_TYPECAST: |
7e8298f7bc0a
Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents:
502
diff
changeset
|
156 nargs = 2; |
7e8298f7bc0a
Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents:
502
diff
changeset
|
157 break; |
7e8298f7bc0a
Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents:
502
diff
changeset
|
158 |
502 | 159 case NODE_OPER_POSTINC: |
160 case NODE_OPER_POSTDEC: | |
161 nargs = 1; | |
162 break; | |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
163 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
164 case NODE_TYPE_PTR: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
165 nargs = 1; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
166 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
167 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
168 case NODE_IDENT: |
498
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
169 case NODE_CONST_INT: |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
170 r -> strval = lw_strdup(va_arg(args, char *)); |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
171 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
172 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
173 case NODE_FUNDEF: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
174 nargs = 4; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
175 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
176 |
502 | 177 case NODE_OPER_COND: |
178 nargs = 3; | |
179 break; | |
180 | |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
181 case NODE_FUNDECL: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
182 nargs = 3; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
183 break; |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
184 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
185 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
186 while (nargs--) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
187 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
188 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
|
189 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
190 va_end(args); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
191 return r; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
192 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
193 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
194 void node_destroy(node_t *node) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
195 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
196 node_t *n; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
197 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
198 while (node -> children) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
199 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
200 n = node -> children -> next_child; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
201 node_destroy(node -> children); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
202 node -> children = n; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
203 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
204 lw_free(node -> strval); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
205 lw_free(node); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
206 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
207 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
208 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
|
209 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
210 node_t *tmp; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
211 |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
212 if (!nn) |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
213 return; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
214 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
215 nn -> parent = node; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
216 nn -> next_child = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
217 if (node -> children) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
218 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
219 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
|
220 /* do nothing */ ; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
221 tmp -> next_child = nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
222 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
223 else |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
224 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
225 node -> children = nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
226 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
227 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
228 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
229 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
|
230 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
231 node_t **pp; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
232 node_t *np; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
233 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
234 if (!node) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
235 node = nn -> parent; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
236 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
237 pp = &(node -> children); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
238 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
|
239 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
240 if (np -> next_child == nn) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
241 break; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
242 pp = &((*pp) -> next_child); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
243 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
244 if (!np) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
245 return; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
246 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
247 *pp = nn -> next_child; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
248 nn -> parent = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
249 nn -> next_child = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
250 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
251 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
252 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
|
253 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
254 node_removechild(node, nn); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
255 node_destroy(nn); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
256 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
257 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
258 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
|
259 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
260 node_t *nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
261 int i; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
262 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
263 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
|
264 fputc(' ', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
265 fprintf(f, "(%s ", node_names[node -> type]); |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
266 if (node -> strval) |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
267 fprintf(f, "\"%s\" ", node -> strval); |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
268 fputc('\n', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
269 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
|
270 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
|
271 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
|
272 fputc(' ', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
273 fputc(')', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
274 fputc('\n', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
275 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
276 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
277 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
|
278 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
279 node_display_aux(node, f, 0); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
280 } |