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
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>
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
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
60 "OPER_MOD",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
61 "OPER_COND",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
62 "OPER_FNCALL",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
63 "OPER_SUBSCRIPT",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
64 "OPER_POSTINC",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
65 "OPER_POSTDEC",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
66 "OPER_PTRMEM",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
67 "OPER_OBJMEM",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
68 "OPER_LSH",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
69 "OPER_RSH",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
70 "OPER_LT",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
71 "OPER_LE",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
72 "OPER_GT",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
73 "OPER_GE",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
74 "OPER_EQ",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
75 "OPER_NE",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
76 "OPER_BWAND",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
77 "OPER_BWXOR",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
78 "OPER_BWOR",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
79 "OPER_BAND",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
80 "OPER_BOR",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
81 "OPER_ASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
82 "OPER_ADDASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
83 "OPER_SUBASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
84 "OPER_MULASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
85 "OPER_DIVASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
86 "OPER_MODASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
87 "OPER_LSHASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
88 "OPER_RSHASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
89 "OPER_BWANDASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
90 "OPER_BWXORASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
91 "OPER_BWORASS",
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
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
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
114 case NODE_OPER_MOD:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
115 case NODE_OPER_BWAND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
116 case NODE_OPER_BWXOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
117 case NODE_OPER_BWOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
118 case NODE_OPER_BAND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
119 case NODE_OPER_BOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
120 case NODE_OPER_SUBSCRIPT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
121 case NODE_OPER_PTRMEM:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
122 case NODE_OPER_OBJMEM:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
123 case NODE_OPER_ASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
124 case NODE_OPER_ADDASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
125 case NODE_OPER_SUBASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
126 case NODE_OPER_MULASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
127 case NODE_OPER_DIVASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
128 case NODE_OPER_MODASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
129 case NODE_OPER_LSH:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
130 case NODE_OPER_LSHASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
131 case NODE_OPER_RSH:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
132 case NODE_OPER_RSHASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
133 case NODE_OPER_BWANDASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
134 case NODE_OPER_BWORASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
135 case NODE_OPER_BWXORASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
136 case NODE_OPER_LT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
137 case NODE_OPER_LE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
138 case NODE_OPER_GT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
139 case NODE_OPER_GE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
140 case NODE_OPER_EQ:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
141 case NODE_OPER_NE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
142 case NODE_OPER_COMMA:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
143 nargs = 2;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
144 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
145
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
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
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
153
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
154 case NODE_OPER_POSTINC:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
155 case NODE_OPER_POSTDEC:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
156 nargs = 1;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
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
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
172 case NODE_OPER_COND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
173 nargs = 3;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
174 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
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 }