-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodes.c
executable file
·355 lines (303 loc) · 7.69 KB
/
nodes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* nodes.c: functions to allocate an initialize parse-tree nodes
*
* Authors; Dallan Quass
* Jan Jannink
*
* originally by: Mark McAuliffe, University of Wisconsin - Madison, 1991
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "redbase.h"
#include "parser_internal.h"
#include "y.tab.h"
/*
* total number of nodes available for a given parse-tree
*/
#define MAXNODE 100
static NODE nodepool[MAXNODE];
static int nodeptr = 0;
/*
* reset_parser: resets the scanner and parser when a syntax error occurs
*
* No return value
*/
void reset_parser(void)
{
reset_scanner();
nodeptr = 0;
}
static void (*cleanup_func)() = NULL;
/*
* new_query: prepares for a new query by releasing all resources used
* by previous queries.
*
* No return value.
*/
void new_query(void)
{
nodeptr = 0;
reset_charptr();
if(cleanup_func != NULL)
(*cleanup_func)();
}
void register_cleanup_function(void (*func)())
{
cleanup_func = func;
}
/*
* newnode: allocates a new node of the specified kind and returns a pointer
* to it on success. Returns NULL on error.
*/
NODE *newnode(NODEKIND kind)
{
NODE *n;
/* if we've used up all of the nodes then error */
if(nodeptr == MAXNODE){
fprintf(stderr, "out of memory\n");
exit(1);
}
/* get the next node */
n = nodepool + nodeptr;
++nodeptr;
/* initialize the `kind' field */
n -> kind = kind;
return n;
}
/*
* create_table_node: allocates, initializes, and returns a pointer to a new
* create table node having the indicated values.
*/
NODE *create_table_node(char *relname, NODE *attrlist)
{
NODE *n = newnode(N_CREATETABLE);
n -> u.CREATETABLE.relname = relname;
n -> u.CREATETABLE.attrlist = attrlist;
return n;
}
/*
* create_index_node: allocates, initializes, and returns a pointer to a new
* create index node having the indicated values.
*/
NODE *create_index_node(char *relname, char *attrname)
{
NODE *n = newnode(N_CREATEINDEX);
n -> u.CREATEINDEX.relname = relname;
n -> u.CREATEINDEX.attrname = attrname;
return n;
}
/*
* drop_index_node: allocates, initializes, and returns a pointer to a new
* drop index node having the indicated values.
*/
NODE *drop_index_node(char *relname, char *attrname)
{
NODE *n = newnode(N_DROPINDEX);
n -> u.DROPINDEX.relname = relname;
n -> u.DROPINDEX.attrname = attrname;
return n;
}
/*
* drop_table_node: allocates, initializes, and returns a pointer to a new
* drop table node having the indicated values.
*/
NODE *drop_table_node(char *relname)
{
NODE *n = newnode(N_DROPTABLE);
n -> u.DROPTABLE.relname = relname;
return n;
}
/*
* load_node: allocates, initializes, and returns a pointer to a new
* load node having the indicated values.
*/
NODE *load_node(char *relname, char *filename)
{
NODE *n = newnode(N_LOAD);
n -> u.LOAD.relname = relname;
n -> u.LOAD.filename = filename;
return n;
}
/*
* set_node: allocates, initializes, and returns a pointer to a new
* set node having the indicated values.
*/
NODE *set_node(char *paramName, char *string)
{
NODE *n = newnode(N_SET);
n -> u.SET.paramName = paramName;
n -> u.SET.string = string;
return n;
}
/*
* help_node: allocates, initializes, and returns a pointer to a new
* help node having the indicated values.
*/
NODE *help_node(char *relname)
{
NODE *n = newnode(N_HELP);
n -> u.HELP.relname = relname;
return n;
}
/*
* print_node: allocates, initializes, and returns a pointer to a new
* print node having the indicated values.
*/
NODE *print_node(char *relname)
{
NODE *n = newnode(N_PRINT);
n -> u.PRINT.relname = relname;
return n;
}
/*
* query_node: allocates, initializes, and returns a pointer to a new
* query node having the indicated values.
*/
NODE *query_node(NODE *relattrlist, NODE *rellist, NODE *conditionlist)
{
NODE *n = newnode(N_QUERY);
n->u.QUERY.relattrlist = relattrlist;
n->u.QUERY.rellist = rellist;
n->u.QUERY.conditionlist = conditionlist;
return n;
}
/*
* insert_node: allocates, initializes, and returns a pointer to a new
* insert node having the indicated values.
*/
NODE *insert_node(char *relname, NODE *valuelist)
{
NODE *n = newnode(N_INSERT);
n->u.INSERT.relname = relname;
n->u.INSERT.valuelist = valuelist;
return n;
}
/*
* delete_node: allocates, initializes, and returns a pointer to a new
* delete node having the indicated values.
*/
NODE *delete_node(char *relname, NODE *conditionlist)
{
NODE *n = newnode(N_DELETE);
n->u.DELETE.relname = relname;
n->u.DELETE.conditionlist = conditionlist;
return n;
}
/*
* update_node: allocates, initializes, and returns a pointer to a new
* update node having the indicated values.
*/
NODE *update_node(char *relname, NODE *relattr, NODE *relorvalue,
NODE *conditionlist)
{
NODE *n = newnode(N_UPDATE);
n->u.UPDATE.relname = relname;
n->u.UPDATE.relattr = relattr;
n->u.UPDATE.relorvalue = relorvalue;
n->u.UPDATE.conditionlist = conditionlist;
return n;
}
/*
* relattr_node: allocates, initializes, and returns a pointer to a new
* relattr node having the indicated values.
*/
NODE *relattr_node(char *relname, char *attrname)
{
NODE *n = newnode(N_RELATTR);
n -> u.RELATTR.relname = relname;
n -> u.RELATTR.attrname = attrname;
return n;
}
/*
* condition_node: allocates, initializes, and returns a pointer to a new
* condition node having the indicated values.
*/
NODE *condition_node(NODE *lhsRelattr, CompOp op, NODE *rhsRelattrOrValue)
{
NODE *n = newnode(N_CONDITION);
n->u.CONDITION.lhsRelattr = lhsRelattr;
n->u.CONDITION.op = op;
n->u.CONDITION.rhsRelattr =
rhsRelattrOrValue->u.RELATTR_OR_VALUE.relattr;
n->u.CONDITION.rhsValue =
rhsRelattrOrValue->u.RELATTR_OR_VALUE.value;
return n;
}
/*
* value_node: allocates, initializes, and returns a pointer to a new
* value node having the indicated values.
*/
NODE *value_node(AttrType type, void *value)
{
NODE *n = newnode(N_VALUE);
n->u.VALUE.type = type;
switch (type) {
case INT:
n->u.VALUE.ival = *(int *)value;
break;
case FLOAT:
n->u.VALUE.rval = *(float *)value;
break;
case STRING:
n->u.VALUE.sval = (char *)value;
break;
}
return n;
}
/*
* relattr_or_valuenode: allocates, initializes, and returns a pointer to
* a new relattr_or_value node having the indicated values.
*/
NODE *relattr_or_value_node(NODE *relattr, NODE *value)
{
NODE *n = newnode(N_RELATTR_OR_VALUE);
n->u.RELATTR_OR_VALUE.relattr = relattr;
n->u.RELATTR_OR_VALUE.value = value;
return n;
}
/*
* attrtype_node: allocates, initializes, and returns a pointer to a new
* attrtype node having the indicated values.
*/
NODE *attrtype_node(char *attrname, char *type)
{
NODE *n = newnode(N_ATTRTYPE);
n -> u.ATTRTYPE.attrname = attrname;
n -> u.ATTRTYPE.type = type;
return n;
}
/*
* relation_node: allocates, initializes, and returns a pointer to a new
* relation node having the indicated values.
*/
NODE *relation_node(char *relname)
{
NODE *n = newnode(N_RELATION);
n->u.RELATION.relname = relname;
return n;
}
/*
* list_node: allocates, initializes, and returns a pointer to a new
* list node having the indicated values.
*/
NODE *list_node(NODE *n)
{
NODE *list = newnode(N_LIST);
list -> u.LIST.curr = n;
list -> u.LIST.next = NULL;
return list;
}
/*
* prepends node n onto the front of list.
*
* Returns the resulting list.
*/
NODE *prepend(NODE *n, NODE *list)
{
NODE *newlist = newnode(N_LIST);
newlist -> u.LIST.curr = n;
newlist -> u.LIST.next = list;
return newlist;
}