Mercurial > hg > index.cgi
annotate lwcc/tree.c @ 501:f3e9732973f1
Add basic integer operations to lwcc
Add +, -, *, and / to lwcc parser and code generator. Multiplication and
division require helper functions in a yet to be created support library.
These operations are integer only for the moment.
author | William Astle <lost@l-w.ca> |
---|---|
date | Tue, 24 Sep 2019 22:07:56 -0600 |
parents | 1bd2d590d734 |
children | 14a40f8bb4eb |
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", |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
60 }; |
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 |
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 node_t *node_create(int type, ...) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
65 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
66 node_t *r; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
67 int nargs = 0; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
68 va_list args; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
69 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
70 va_start(args, type); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
71 r = lw_alloc(sizeof(node_t)); |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
72 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
|
73 r -> type = type; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
74 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
75 switch (type) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
76 { |
501
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
77 case NODE_OPER_PLUS: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
78 case NODE_OPER_MINUS: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
79 case NODE_OPER_TIMES: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
80 case NODE_OPER_DIVIDE: |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
81 nargs = 2; |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
82 break; |
f3e9732973f1
Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents:
498
diff
changeset
|
83 |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
84 case NODE_DECL: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
85 nargs = 2; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
86 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
87 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
88 case NODE_TYPE_PTR: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
89 nargs = 1; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
90 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
91 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
92 case NODE_IDENT: |
498
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
93 case NODE_CONST_INT: |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
94 r -> strval = lw_strdup(va_arg(args, char *)); |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
95 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
96 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
97 case NODE_FUNDEF: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
98 nargs = 4; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
99 break; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
100 |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
101 case NODE_FUNDECL: |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
102 nargs = 3; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
103 break; |
312
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
106 while (nargs--) |
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 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
|
109 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
110 va_end(args); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
111 return r; |
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
114 void node_destroy(node_t *node) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
115 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
116 node_t *n; |
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 while (node -> children) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
119 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
120 n = node -> children -> next_child; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
121 node_destroy(node -> children); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
122 node -> children = n; |
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 lw_free(node -> strval); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
125 lw_free(node); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
126 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
127 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
128 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
|
129 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
130 node_t *tmp; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
131 |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
132 if (!nn) |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
133 return; |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
134 |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
135 nn -> parent = node; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
136 nn -> next_child = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
137 if (node -> children) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
138 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
139 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
|
140 /* do nothing */ ; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
141 tmp -> next_child = nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
142 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
143 else |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
144 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
145 node -> children = nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
146 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
147 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
148 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
149 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
|
150 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
151 node_t **pp; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
152 node_t *np; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
153 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
154 if (!node) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
155 node = nn -> parent; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
156 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
157 pp = &(node -> children); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
158 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
|
159 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
160 if (np -> next_child == nn) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
161 break; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
162 pp = &((*pp) -> next_child); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
163 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
164 if (!np) |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
165 return; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
166 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
167 *pp = nn -> next_child; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
168 nn -> parent = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
169 nn -> next_child = NULL; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
170 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
171 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
172 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
|
173 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
174 node_removechild(node, nn); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
175 node_destroy(nn); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
176 } |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
177 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
178 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
|
179 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
180 node_t *nn; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
181 int i; |
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 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
|
184 fputc(' ', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
185 fprintf(f, "(%s ", node_names[node -> type]); |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
186 if (node -> strval) |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
187 fprintf(f, "\"%s\" ", node -> strval); |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
188 fputc('\n', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
189 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
|
190 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
|
191 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
|
192 fputc(' ', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
193 fputc(')', f); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
194 fputc('\n', f); |
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
197 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
|
198 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
199 node_display_aux(node, f, 0); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
200 } |