annotate lwcc/tree.c @ 508:10f62dc61a75

Fix bad usage of sprintf() Usage of sprintf() to append to a string in the form of sprintf(buf, "%s...", buf...) is undefined, regardless whether it worked on a lot of older systems. It was always a bad idea and it now breaks on current glibc and gcc development environments. The moral: if any of your code uses sprintf() in a way similar to the above, fix it. It may not fail in a benign way.
author William Astle <lost@l-w.ca>
date Sun, 10 May 2020 22:38:24 -0600
parents 7e8298f7bc0a
children
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",
506
7e8298f7bc0a Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents: 502
diff changeset
93 "TYPECAST",
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
94 };
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
95
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
96
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
97
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
98 node_t *node_create(int type, ...)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
99 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
100 node_t *r;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
101 int nargs = 0;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
102 va_list args;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
103
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
104 va_start(args, type);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
105 r = lw_alloc(sizeof(node_t));
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
106 memset(r, 0, sizeof(node_t));
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
107 r -> type = type;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
108
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
109 switch (type)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
110 {
501
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
111 case NODE_OPER_PLUS:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
112 case NODE_OPER_MINUS:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
113 case NODE_OPER_TIMES:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
114 case NODE_OPER_DIVIDE:
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
115 case NODE_OPER_MOD:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
116 case NODE_OPER_BWAND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
117 case NODE_OPER_BWXOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
118 case NODE_OPER_BWOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
119 case NODE_OPER_BAND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
120 case NODE_OPER_BOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
121 case NODE_OPER_SUBSCRIPT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
122 case NODE_OPER_PTRMEM:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
123 case NODE_OPER_OBJMEM:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
124 case NODE_OPER_ASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
125 case NODE_OPER_ADDASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
126 case NODE_OPER_SUBASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
127 case NODE_OPER_MULASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
128 case NODE_OPER_DIVASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
129 case NODE_OPER_MODASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
130 case NODE_OPER_LSH:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
131 case NODE_OPER_LSHASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
132 case NODE_OPER_RSH:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
133 case NODE_OPER_RSHASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
134 case NODE_OPER_BWANDASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
135 case NODE_OPER_BWORASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
136 case NODE_OPER_BWXORASS:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
137 case NODE_OPER_LT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
138 case NODE_OPER_LE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
139 case NODE_OPER_GT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
140 case NODE_OPER_GE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
141 case NODE_OPER_EQ:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
142 case NODE_OPER_NE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
143 case NODE_OPER_COMMA:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
144 nargs = 2;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
145 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
146
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
147 case NODE_OPER_FNCALL:
501
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
148 nargs = 2;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
149 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 498
diff changeset
150
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
151 case NODE_DECL:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
152 nargs = 2;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
153 break;
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
154
506
7e8298f7bc0a Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents: 502
diff changeset
155 case NODE_TYPECAST:
7e8298f7bc0a Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents: 502
diff changeset
156 nargs = 2;
7e8298f7bc0a Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents: 502
diff changeset
157 break;
7e8298f7bc0a Add basic syntax for typecasting
William Astle <lost@l-w.ca>
parents: 502
diff changeset
158
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
159 case NODE_OPER_POSTINC:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
160 case NODE_OPER_POSTDEC:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
161 nargs = 1;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
162 break;
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
163
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
164 case NODE_TYPE_PTR:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
165 nargs = 1;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
166 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
167
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
168 case NODE_IDENT:
498
1bd2d590d734 Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents: 314
diff changeset
169 case NODE_CONST_INT:
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
170 r -> strval = lw_strdup(va_arg(args, char *));
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
171 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
172
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
173 case NODE_FUNDEF:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
174 nargs = 4;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
175 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
176
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
177 case NODE_OPER_COND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
178 nargs = 3;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
179 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
180
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
181 case NODE_FUNDECL:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
182 nargs = 3;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
183 break;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
184 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
185
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
186 while (nargs--)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
187 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
188 node_addchild(r, va_arg(args, node_t *));
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
189 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
190 va_end(args);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
191 return r;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
192 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
193
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
194 void node_destroy(node_t *node)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
195 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
196 node_t *n;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
197
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
198 while (node -> children)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
199 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
200 n = node -> children -> next_child;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
201 node_destroy(node -> children);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
202 node -> children = n;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
203 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
204 lw_free(node -> strval);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
205 lw_free(node);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
206 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
207
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
208 void node_addchild(node_t *node, node_t *nn)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
209 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
210 node_t *tmp;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
211
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
212 if (!nn)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
213 return;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
214
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
215 nn -> parent = node;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
216 nn -> next_child = NULL;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
217 if (node -> children)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
218 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
219 for (tmp = node -> children; tmp -> next_child; tmp = tmp -> next_child)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
220 /* do nothing */ ;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
221 tmp -> next_child = nn;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
222 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
223 else
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
224 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
225 node -> children = nn;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
226 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
227 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
228
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
229 void node_removechild(node_t *node, node_t *nn)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
230 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
231 node_t **pp;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
232 node_t *np;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
233
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
234 if (!node)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
235 node = nn -> parent;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
236
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
237 pp = &(node -> children);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
238 for (np = node -> children; np; np = np -> next_child)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
239 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
240 if (np -> next_child == nn)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
241 break;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
242 pp = &((*pp) -> next_child);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
243 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
244 if (!np)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
245 return;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
246
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
247 *pp = nn -> next_child;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
248 nn -> parent = NULL;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
249 nn -> next_child = NULL;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
250 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
251
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
252 void node_removechild_destroy(node_t *node, node_t *nn)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
253 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
254 node_removechild(node, nn);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
255 node_destroy(nn);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
256 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
257
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
258 static void node_display_aux(node_t *node, FILE *f, int level)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
259 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
260 node_t *nn;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
261 int i;
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
262
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
263 for (i = 0; i < level * 4; i++)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
264 fputc(' ', f);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
265 fprintf(f, "(%s ", node_names[node -> type]);
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
266 if (node -> strval)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
267 fprintf(f, "\"%s\" ", node -> strval);
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
268 fputc('\n', f);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
269 for (nn = node -> children; nn; nn = nn -> next_child)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
270 node_display_aux(nn, f, level + 1);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
271 for (i = 0; i < level * 4; i++)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
272 fputc(' ', f);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
273 fputc(')', f);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
274 fputc('\n', f);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
275 }
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
276
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
277 void node_display(node_t *node, FILE *f)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
278 {
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
279 node_display_aux(node, f, 0);
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
280 }