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
|
#include "ast_accessors.h"
static struct ast *_get_from_command(struct ast *ast, size_t index)
{
(void)ast;
(void)index;
return NULL;
}
static struct ast *_get_from_if(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_if *)ast)->condition;
case 1:
return ((struct ast_if *)ast)->then_body;
case 2:
return ((struct ast_if *)ast)->else_body;
default:
return NULL;
}
}
static struct ast *_get_from_list(struct ast *ast, size_t index)
{
if (index >= ((struct ast_list *)ast)->nb_children)
{
return NULL;
}
return ((struct ast_list *)ast)->children[index];
}
static struct ast *_get_from_logical(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_logical *)ast)->left;
case 1:
return ((struct ast_logical *)ast)->right;
default:
return NULL;
}
}
static struct ast *_get_from_pipeline(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_pipeline *)ast)->left;
case 1:
return ((struct ast_pipeline *)ast)->right;
default:
return NULL;
}
}
static struct ast *_get_from_redirection(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_redirection *)ast)->expr;
default:
return NULL;
}
}
static struct ast *_get_from_while(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_while *)ast)->cond;
case 1:
return ((struct ast_while *)ast)->body;
default:
return NULL;
}
}
static struct ast *_get_from_assign(struct ast *ast, size_t index)
{
(void)ast;
(void)index;
return NULL;
}
static struct ast *_get_from_for(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_for *)ast)->left;
case 1:
return ((struct ast_for *)ast)->right;
default:
return NULL;
}
}
static struct ast *_get_from_function(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_function *)ast)->body;
default:
return NULL;
}
}
static struct ast *_get_from_subshell(struct ast *ast, size_t index)
{
switch (index)
{
case 0:
return ((struct ast_subshell *)ast)->body;
default:
return NULL;
}
}
typedef struct ast *(*ast_getters)(struct ast *, size_t);
static const ast_getters ast_getters_ltable[] = {
[AST_COMMAND] = _get_from_command,
[AST_IF] = _get_from_if,
[AST_LOGICAL] = _get_from_logical,
[AST_LIST] = _get_from_list,
[AST_PIPELINE] = _get_from_pipeline,
[AST_REDIRECTION] = _get_from_redirection,
[AST_WHILE] = _get_from_while,
[AST_ASSIGN] = _get_from_assign,
[AST_FOR] = _get_from_for,
[AST_FUNCTION] = _get_from_function,
[AST_SUBSHELL] = _get_from_subshell
};
struct ast *get_i(struct ast *ast, size_t index)
{
return ast_getters_ltable[ast->type](ast, index);
}
struct ast *get_left(struct ast *ast)
{
return get_i(ast, 0);
}
struct ast *get_right(struct ast *ast)
{
return get_i(ast, 1);
}
static void _set_on_command(struct ast *ast, struct ast *child, size_t index)
{
(void)ast;
(void)child;
(void)index;
return;
}
static void _set_on_if(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_if *)ast)->condition = child;
return;
case 1:
((struct ast_if *)ast)->then_body = child;
return;
case 2:
((struct ast_if *)ast)->else_body = child;
return;
default:
return;
}
}
static void _set_on_logical(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_logical *)ast)->left = child;
break;
case 1:
((struct ast_logical *)ast)->right = child;
break;
default:
return;
}
}
static void _set_on_list(struct ast *ast, struct ast *child, size_t index)
{
if (index > ((struct ast_list *)ast)->nb_children)
{
return;
}
if (index == ((struct ast_list *)ast)->nb_children)
{
((struct ast_list *)ast)->nb_children++;
((struct ast_list *)ast)->children = realloc(
((struct ast_list *)ast)->children,
((struct ast_list *)ast)->nb_children * sizeof(struct ast_command));
}
((struct ast_list *)ast)->children[index] = child;
}
static void _set_on_pipeline(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_pipeline *)ast)->left = child;
break;
case 1:
((struct ast_pipeline *)ast)->right = child;
break;
default:
return;
}
}
static void _set_on_redirection(struct ast *ast, struct ast *child,
size_t index)
{
switch (index)
{
case 0:
((struct ast_redirection *)ast)->expr = child;
break;
default:
return;
}
}
static void _set_on_while(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_while *)ast)->cond = child;
break;
case 1:
((struct ast_while *)ast)->body = child;
break;
default:
return;
}
}
static void _set_on_assign(struct ast *ast, struct ast *child, size_t index)
{
(void)ast;
(void)child;
(void)index;
return;
}
static void _set_on_for(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_for *)ast)->left = child;
return;
case 1:
((struct ast_for *)ast)->right = child;
return;
default:
return;
}
}
static void _set_on_function(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_function *)ast)->body = child;
default:
return;
}
}
static void _set_on_subshell(struct ast *ast, struct ast *child, size_t index)
{
switch (index)
{
case 0:
((struct ast_subshell *)ast)->body = child;
default:
return;
}
}
typedef void (*ast_setters)(struct ast *, struct ast *, size_t);
static const ast_setters ast_setters_ltable[] = {
[AST_COMMAND] = _set_on_command, [AST_IF] = _set_on_if,
[AST_LOGICAL] = _set_on_logical, [AST_LIST] = _set_on_list,
[AST_PIPELINE] = _set_on_pipeline, [AST_REDIRECTION] = _set_on_redirection,
[AST_WHILE] = _set_on_while, [AST_ASSIGN] = _set_on_assign,
[AST_FOR] = _set_on_for, [AST_FUNCTION] = _set_on_function,
[AST_SUBSHELL] = _set_on_subshell
};
void set_i(struct ast *ast, struct ast *child, size_t index)
{
ast_setters_ltable[ast->type](ast, child, index);
}
void set_left(struct ast *ast, struct ast *child)
{
set_i(ast, child, 0);
}
void set_right(struct ast *ast, struct ast *child)
{
set_i(ast, child, 1);
}
|