-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtree.c
174 lines (167 loc) · 4.48 KB
/
tree.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
/* tree.c: functions for manipulating parse-trees. (create, copy, delete) */
#include "rc.h"
/* make a new node, pass it back to yyparse. Used to generate the parsetree. */
extern Node *mk(enum nodetype t,...) {
va_list ap;
Node *n;
va_start(ap, t);
switch (t) {
default:
panic("unexpected node in mk");
/* NOTREACHED */
case nDup:
n = nalloc(offsetof(Node, u[3]));
n->u[0].i = va_arg(ap, int);
n->u[1].i = va_arg(ap, int);
n->u[2].i = va_arg(ap, int);
break;
case nWord:
n = nalloc(offsetof(Node, u[3]));
n->u[0].s = va_arg(ap, char *);
n->u[1].s = va_arg(ap, char *);
n->u[2].i = va_arg(ap, int);
break;
case nBang: case nNowait:
case nCount: case nFlat: case nRmfn: case nSubshell:
case nVar: case nCase:
n = nalloc(offsetof(Node, u[1]));
n->u[0].p = va_arg(ap, Node *);
break;
case nAndalso: case nAssign: case nBackq: case nBody: case nBrace: case nConcat:
case nElse: case nEpilog: case nIf: case nNewfn: case nCbody:
case nOrelse: case nPre: case nArgs: case nSwitch:
case nMatch: case nVarsub: case nWhile: case nLappend:
n = nalloc(offsetof(Node, u[2]));
n->u[0].p = va_arg(ap, Node *);
n->u[1].p = va_arg(ap, Node *);
break;
case nForin:
n = nalloc(offsetof(Node, u[3]));
n->u[0].p = va_arg(ap, Node *);
n->u[1].p = va_arg(ap, Node *);
n->u[2].p = va_arg(ap, Node *);
break;
case nPipe:
n = nalloc(offsetof(Node, u[4]));
n->u[0].i = va_arg(ap, int);
n->u[1].i = va_arg(ap, int);
n->u[2].p = va_arg(ap, Node *);
n->u[3].p = va_arg(ap, Node *);
break;
case nRedir:
case nNmpipe:
n = nalloc(offsetof(Node, u[3]));
n->u[0].i = va_arg(ap, int);
n->u[1].i = va_arg(ap, int);
n->u[2].p = va_arg(ap, Node *);
break;
}
n->type = t;
va_end(ap);
return n;
}
/* copy a tree to malloc space. Used when storing the definition of a function */
extern Node *treecpy(Node *s, void *(*alloc)(size_t)) {
Node *n;
if (s == NULL)
return NULL;
switch (s->type) {
default:
panic("unexpected node in treecpy");
/* NOTREACHED */
case nDup:
n = (*alloc)(offsetof(Node, u[3]));
n->u[0].i = s->u[0].i;
n->u[1].i = s->u[1].i;
n->u[2].i = s->u[2].i;
break;
case nWord:
n = (*alloc)(offsetof(Node, u[3]));
n->u[0].s = strcpy((char *) (*alloc)(strlen(s->u[0].s) + 1), s->u[0].s);
if (s->u[1].s != NULL) {
size_t i = strlen(s->u[0].s);
n->u[1].s = (*alloc)(i);
memcpy(n->u[1].s, s->u[1].s, i);
} else
n->u[1].s = NULL;
n->u[2].i = s->u[2].i;
break;
case nBang: case nNowait: case nCase:
case nCount: case nFlat: case nRmfn: case nSubshell: case nVar:
n = (*alloc)(offsetof(Node, u[1]));
n->u[0].p = treecpy(s->u[0].p, alloc);
break;
case nAndalso: case nAssign: case nBackq: case nBody: case nBrace: case nConcat:
case nElse: case nEpilog: case nIf: case nNewfn: case nCbody:
case nOrelse: case nPre: case nArgs: case nSwitch:
case nMatch: case nVarsub: case nWhile: case nLappend:
n = (*alloc)(offsetof(Node, u[2]));
n->u[0].p = treecpy(s->u[0].p, alloc);
n->u[1].p = treecpy(s->u[1].p, alloc);
break;
case nForin:
n = (*alloc)(offsetof(Node, u[3]));
n->u[0].p = treecpy(s->u[0].p, alloc);
n->u[1].p = treecpy(s->u[1].p, alloc);
n->u[2].p = treecpy(s->u[2].p, alloc);
break;
case nPipe:
n = (*alloc)(offsetof(Node, u[4]));
n->u[0].i = s->u[0].i;
n->u[1].i = s->u[1].i;
n->u[2].p = treecpy(s->u[2].p, alloc);
n->u[3].p = treecpy(s->u[3].p, alloc);
break;
case nRedir:
case nNmpipe:
n = (*alloc)(offsetof(Node, u[3]));
n->u[0].i = s->u[0].i;
n->u[1].i = s->u[1].i;
n->u[2].p = treecpy(s->u[2].p, alloc);
break;
}
n->type = s->type;
return n;
}
/* free a function definition that is no longer needed */
extern void treefree(Node *s) {
if (s == NULL)
return;
switch (s->type) {
default:
panic("unexpected node in treefree");
/* NOTREACHED */
case nDup:
break;
case nWord:
efree(s->u[0].s);
efree(s->u[1].s);
break;
case nBang: case nNowait:
case nCount: case nFlat: case nRmfn:
case nSubshell: case nVar: case nCase:
treefree(s->u[0].p);
break;
case nAndalso: case nAssign: case nBackq: case nBody: case nBrace: case nConcat:
case nElse: case nEpilog: case nIf: case nNewfn:
case nOrelse: case nPre: case nArgs: case nCbody:
case nSwitch: case nMatch: case nVarsub: case nWhile:
case nLappend:
treefree(s->u[1].p);
treefree(s->u[0].p);
break;
case nForin:
treefree(s->u[2].p);
treefree(s->u[1].p);
treefree(s->u[0].p);
break;
case nPipe:
treefree(s->u[2].p);
treefree(s->u[3].p);
break;
case nRedir:
case nNmpipe:
treefree(s->u[2].p);
}
efree(s);
}