Mercurial > hg > index.cgi
annotate lwcc/tree.c @ 502:14a40f8bb4eb
Add various operators to lwcc
Add various binary and ternary operators to lwcc, but only those which can
work with constant operands. Seems like variables are probably required
next.
author | William Astle <lost@l-w.ca> |
---|---|
date | Wed, 25 Sep 2019 20:23:49 -0600 |
parents | f3e9732973f1 |
children | 7e8298f7bc0a |
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", | |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
93 }; |
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 node_t *node_create(int type, ...) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
98 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
99 node_t *r; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
100 int nargs = 0; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
101 va_list args; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
102 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
103 va_start(args, type); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
104 r = lw_alloc(sizeof(node_t)); |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
105 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
|
106 r -> type = type; |
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 switch (type) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
109 { |
501
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
110 case NODE_OPER_PLUS: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
111 case NODE_OPER_MINUS: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
112 case NODE_OPER_TIMES: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
113 case NODE_OPER_DIVIDE: |
502 | 114 case NODE_OPER_MOD: |
115 case NODE_OPER_BWAND: | |
116 case NODE_OPER_BWXOR: | |
117 case NODE_OPER_BWOR: | |
118 case NODE_OPER_BAND: | |
119 case NODE_OPER_BOR: | |
120 case NODE_OPER_SUBSCRIPT: | |
121 case NODE_OPER_PTRMEM: | |
122 case NODE_OPER_OBJMEM: | |
123 case NODE_OPER_ASS: | |
124 case NODE_OPER_ADDASS: | |
125 case NODE_OPER_SUBASS: | |
126 case NODE_OPER_MULASS: | |
127 case NODE_OPER_DIVASS: | |
128 case NODE_OPER_MODASS: | |
129 case NODE_OPER_LSH: | |
130 case NODE_OPER_LSHASS: | |
131 case NODE_OPER_RSH: | |
132 case NODE_OPER_RSHASS: | |
133 case NODE_OPER_BWANDASS: | |
134 case NODE_OPER_BWORASS: | |
135 case NODE_OPER_BWXORASS: | |
136 case NODE_OPER_LT: | |
137 case NODE_OPER_LE: | |
138 case NODE_OPER_GT: | |
139 case NODE_OPER_GE: | |
140 case NODE_OPER_EQ: | |
141 case NODE_OPER_NE: | |
142 case NODE_OPER_COMMA: | |
143 nargs = 2; | |
144 break; | |
145 | |
146 case NODE_OPER_FNCALL: | |
501
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
147 nargs = 2; |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
148 break; |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
149 |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
150 case NODE_DECL: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
151 nargs = 2; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
152 break; |
502 | 153 |
154 case NODE_OPER_POSTINC: | |
155 case NODE_OPER_POSTDEC: | |
156 nargs = 1; | |
157 break; | |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
158 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
159 case NODE_TYPE_PTR: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
160 nargs = 1; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
161 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
162 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
163 case NODE_IDENT: |
498
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
164 case NODE_CONST_INT: |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
165 r -> strval = lw_strdup(va_arg(args, char *)); |
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_FUNDEF: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
169 nargs = 4; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
170 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
171 |
502 | 172 case NODE_OPER_COND: |
173 nargs = 3; | |
174 break; | |
175 | |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
176 case NODE_FUNDECL: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
177 nargs = 3; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
178 break; |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
179 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
180 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
181 while (nargs--) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
182 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
183 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
|
184 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
185 va_end(args); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
186 return r; |
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
189 void node_destroy(node_t *node) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
190 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
191 node_t *n; |
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 while (node -> children) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
194 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
195 n = node -> children -> next_child; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
196 node_destroy(node -> children); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
197 node -> children = n; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
198 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
199 lw_free(node -> strval); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
200 lw_free(node); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
201 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
202 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
203 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
|
204 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
205 node_t *tmp; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
206 |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
207 if (!nn) |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
208 return; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
209 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
210 nn -> parent = node; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
211 nn -> next_child = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
212 if (node -> children) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
213 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
214 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
|
215 /* do nothing */ ; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
216 tmp -> next_child = nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
217 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
218 else |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
219 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
220 node -> children = nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
221 } |
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
224 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
|
225 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
226 node_t **pp; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
227 node_t *np; |
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 if (!node) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
230 node = nn -> parent; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
231 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
232 pp = &(node -> children); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
233 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
|
234 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
235 if (np -> next_child == nn) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
236 break; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
237 pp = &((*pp) -> next_child); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
238 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
239 if (!np) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
240 return; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
241 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
242 *pp = nn -> next_child; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
243 nn -> parent = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
244 nn -> next_child = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
245 } |
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 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
|
248 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
249 node_removechild(node, nn); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
250 node_destroy(nn); |
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
253 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
|
254 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
255 node_t *nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
256 int i; |
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 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
|
259 fputc(' ', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
260 fprintf(f, "(%s ", node_names[node -> type]); |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
261 if (node -> strval) |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
262 fprintf(f, "\"%s\" ", node -> strval); |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
263 fputc('\n', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 fputc(' ', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
268 fputc(')', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
269 fputc('\n', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
270 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
271 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
272 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
|
273 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
274 node_display_aux(node, f, 0); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
275 } |