-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
171 lines (148 loc) · 4.41 KB
/
ast.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
#include <stdlib.h>
#include "dynamic_array.h"
#include "ast.h"
static struct ast *ast_create(enum ast_type type, union ast_data data)
{
struct ast *ast = malloc(sizeof(struct ast));
ast->type = type;
ast->data = data;
ast->async = false;
return ast;
}
struct ast *ast_create_script()
{
struct ast *ast;
union ast_data data;
data.script.expressions = dynamic_array_create(4, sizeof(struct ast*));
ast = ast_create(AST_TYPE_SCRIPT, data);
return ast;
}
struct ast *ast_create_command()
{
struct ast *ast;
union ast_data data;
data.command.words = dynamic_array_create(4, sizeof(char *));
data.command.redirects = dynamic_array_create(4, sizeof(struct ast_data_expression_redirect*));
ast = ast_create(AST_TYPE_COMMAND, data);
return ast;
}
struct ast *ast_create_pipeline()
{
struct ast *ast;
union ast_data data;
data.pipeline.expressions = dynamic_array_create(4, sizeof(struct ast*));
ast = ast_create(AST_TYPE_PIPELINE, data);
return ast;
}
struct ast *ast_create_logical_expression(enum ast_data_logical_expression_type type)
{
struct ast *ast;
union ast_data data;
data.logical_expression.type = type;
data.logical_expression.left = NULL;
data.logical_expression.right = NULL;
ast = ast_create(AST_TYPE_LOGICAL_EXPRESSION, data);
return ast;
}
struct ast *ast_create_subshell()
{
struct ast *ast;
union ast_data data;
data.subshell.script = NULL;
data.subshell.redirects = dynamic_array_create(4, sizeof(struct ast_data_expression_redirect*));
ast = ast_create(AST_TYPE_SUBSHELL, data);
return ast;
}
struct ast_data_expression_redirect *ast_data_expression_redirect_create(enum ast_data_expression_redirect_type type, char *file)
{
struct ast_data_expression_redirect *redirect = malloc(sizeof(struct ast_data_expression_redirect));
redirect->type = type;
redirect->file = file;
return redirect;
}
static void ast_destroy_script(struct ast *ast)
{
struct dynamic_array *expressions_array = ast->data.script.expressions;
int i;
struct ast **expressions = expressions_array->ptr;
for (i = 0; i < expressions_array->len; i++) {
ast_destroy(expressions[i]);
}
free(expressions);
free(expressions_array);
}
static void ast_data_expression_redirect_destroy(struct ast_data_expression_redirect *redirect)
{
free(redirect->file);
free(redirect);
}
static void ast_destroy_command(struct ast *ast)
{
int i;
struct dynamic_array *words_array = ast->data.command.words;
struct dynamic_array *redirects_array = ast->data.command.redirects;
char **words = words_array->ptr;
struct ast_data_expression_redirect **redirects = redirects_array->ptr;
for (i = 0; i < words_array->len; i++) {
free(words[i]);
}
for (i = 0; i < redirects_array->len; i++) {
ast_data_expression_redirect_destroy(redirects[i]);
}
free(words);
free(words_array);
free(redirects);
free(redirects_array);
}
static void ast_destroy_pipeline(struct ast *ast)
{
struct dynamic_array *expressions_array = ast->data.pipeline.expressions;
int i;
struct ast **expressions = expressions_array->ptr;
for (i = 0; i < expressions_array->len; i++) {
ast_destroy(expressions[i]);
}
free(expressions);
free(expressions_array);
}
static void ast_destroy_logical_expression(struct ast *ast)
{
ast_destroy(ast->data.logical_expression.left);
ast_destroy(ast->data.logical_expression.right);
}
static void ast_destroy_subshell(struct ast *ast)
{
int i;
struct dynamic_array *redirects_array = ast->data.subshell.redirects;
struct ast_data_expression_redirect **redirects = redirects_array->ptr;
for (i = 0; i < redirects_array->len; i++) {
ast_data_expression_redirect_destroy(redirects[i]);
}
free(redirects);
free(redirects_array);
ast_destroy(ast->data.subshell.script);
}
void ast_destroy(struct ast *ast)
{
if (!ast) {
return;
}
switch (ast->type) {
case AST_TYPE_SCRIPT:
ast_destroy_script(ast);
break;
case AST_TYPE_COMMAND:
ast_destroy_command(ast);
break;
case AST_TYPE_PIPELINE:
ast_destroy_pipeline(ast);
break;
case AST_TYPE_LOGICAL_EXPRESSION:
ast_destroy_logical_expression(ast);
break;
case AST_TYPE_SUBSHELL:
ast_destroy_subshell(ast);
break;
}
free(ast);
}