comparison lwcc/tree.c @ 314:a3e277c58df9 ccdev

Checkpoint parser development for lwcc Beginning of lemon based parser for C including all the infrastructure for calling the lemon generated parser. This requires a translation layer from the preprocessor token numbers to the lemon parser token numbers due to the fact that lemon wants to control the token numbers. Eventually when the lemon parser gets replaced with a hand crafted recursive descent parser, this translation will no longer be required. However, in the interest of getting things working sooner rather than later, using something like lemon is beneficial.
author William Astle <lost@l-w.ca>
date Sun, 17 Nov 2013 11:59:36 -0700
parents 41118fb0a8f2
children 1bd2d590d734
comparison
equal deleted inserted replaced
313:73b2bfa17ab0 314:a3e277c58df9
20 */ 20 */
21 21
22 #include <stdarg.h> 22 #include <stdarg.h>
23 #include <string.h> 23 #include <string.h>
24 #include <lw_alloc.h> 24 #include <lw_alloc.h>
25 #include <lw_string.h>
25 26
26 #include "tree.h" 27 #include "tree.h"
27 28
28 static char *node_names[] = { 29 static char *node_names[] = {
29 "NONE", 30 "NONE",
30 "PROGRAM", 31 "PROGRAM",
32 "DECL",
33 "TYPE_CHAR",
34 "TYPE_SHORT",
35 "TYPE_INT",
36 "TYPE_LONG",
37 "TYPE_LONGLONG",
38 "IDENT",
39 "TYPE_PTR",
40 "TYPE_SCHAR",
41 "TYPE_UCHAR",
42 "TYPE_USHORT",
43 "TYPE_UINT",
44 "TYPE_ULONG",
45 "TYPE_ULONGLONG",
46 "TYPE_VOID",
47 "TYPE_FLOAT",
48 "TYPE_DOUBLE",
49 "TYPE_LDOUBLE",
50 "FUNDEF",
51 "FUNDECL",
52 "FUNARGS",
53 "BLOCK",
31 }; 54 };
32 55
33 56
34 57
35 node_t *node_create(int type, ...) 58 node_t *node_create(int type, ...)
38 int nargs = 0; 61 int nargs = 0;
39 va_list args; 62 va_list args;
40 63
41 va_start(args, type); 64 va_start(args, type);
42 r = lw_alloc(sizeof(node_t)); 65 r = lw_alloc(sizeof(node_t));
43 memset(r, sizeof(node_t), 0); 66 memset(r, 0, sizeof(node_t));
44 r -> type = type; 67 r -> type = type;
45 68
46 switch (type) 69 switch (type)
47 { 70 {
71 case NODE_DECL:
72 nargs = 2;
73 break;
74
75 case NODE_TYPE_PTR:
76 nargs = 1;
77 break;
78
79 case NODE_IDENT:
80 r -> strval = lw_strdup(va_arg(args, char *));
81 break;
82
83 case NODE_FUNDEF:
84 nargs = 4;
85 break;
86
87 case NODE_FUNDECL:
88 nargs = 3;
89 break;
48 } 90 }
49 91
50 while (nargs--) 92 while (nargs--)
51 { 93 {
52 node_addchild(r, va_arg(args, node_t *)); 94 node_addchild(r, va_arg(args, node_t *));
70 } 112 }
71 113
72 void node_addchild(node_t *node, node_t *nn) 114 void node_addchild(node_t *node, node_t *nn)
73 { 115 {
74 node_t *tmp; 116 node_t *tmp;
117
118 if (!nn)
119 return;
75 120
76 nn -> parent = node; 121 nn -> parent = node;
77 nn -> next_child = NULL; 122 nn -> next_child = NULL;
78 if (node -> children) 123 if (node -> children)
79 { 124 {
122 int i; 167 int i;
123 168
124 for (i = 0; i < level * 4; i++) 169 for (i = 0; i < level * 4; i++)
125 fputc(' ', f); 170 fputc(' ', f);
126 fprintf(f, "(%s ", node_names[node -> type]); 171 fprintf(f, "(%s ", node_names[node -> type]);
172 if (node -> strval)
173 fprintf(f, "\"%s\" ", node -> strval);
127 fputc('\n', f); 174 fputc('\n', f);
128 for (nn = node -> children; nn; nn = nn -> next_child) 175 for (nn = node -> children; nn; nn = nn -> next_child)
129 node_display_aux(nn, f, level + 1); 176 node_display_aux(nn, f, level + 1);
130 for (i = 0; i < level * 4; i++) 177 for (i = 0; i < level * 4; i++)
131 fputc(' ', f); 178 fputc(' ', f);