-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.c
247 lines (204 loc) · 6.76 KB
/
validator.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
#include "compiler.h"
#include "helpers/vector.h"
static struct compile_process *validator_current_compile_process;
static struct node *current_function;
void validate_variable(struct node *var_node);
void validate_body(struct body *body);
void validation_new_scope(int flags) {
resolver_default_new_scope(validator_current_compile_process->resolver,
flags);
}
void validation_finish_scope() {
resolver_default_finish_scope(validator_current_compile_process->resolver);
}
struct node *validation_next_tree_node() {
return vector_peek_ptr(validator_current_compile_process->node_tree_vec);
}
void validate_init(struct compile_process *process) {
validator_current_compile_process = process;
vector_set_peek_pointer(process->node_tree_vec, 0);
symresolver_new_table(process);
}
void validate_destroy(struct compile_process *process) {
symresolver_end_table(process);
vector_set_peek_pointer(process->node_tree_vec, 0);
}
void validate_symbol_unique(const char *name, const char *type,
struct node *node) {
struct symbol *sym =
symresolver_get_symbol(validator_current_compile_process, name);
if (sym) {
compiler_node_error(node, "symbol %s already defined", name);
}
}
void validate_identifier(struct node *node) {
struct resolver_result *result =
resolver_follow(validator_current_compile_process->resolver, node);
if (!resolver_result_ok(result)) {
compiler_error(validator_current_compile_process, "identifier %s not found",
node->sval);
}
}
void validate_expressionable(struct node *node) {
switch (node->type) {
case NODE_TYPE_IDENTIFIER:
validate_identifier(node);
break;
}
}
void validate_return_node(struct node *node) {
if (node->stmt.return_stmt.exp) {
if (datatype_is_void_no_ptr(¤t_function->func.rtype)) {
compiler_node_error(node, "returning value from void function");
}
validate_expressionable(node->stmt.return_stmt.exp);
}
}
void validate_if_stmt(struct node *node) {
validation_new_scope(0);
validate_expressionable(node->stmt.if_stmt.cond_node);
validate_body(&node->stmt.if_stmt.body_node->body);
validation_finish_scope();
}
void validate_for_stmt(struct node *node) {
validation_new_scope(0);
validate_expressionable(node->stmt.for_stmt.init);
validate_expressionable(node->stmt.for_stmt.cond);
validate_expressionable(node->stmt.for_stmt.inc);
validate_body(&node->stmt.for_stmt.body->body);
validation_finish_scope();
}
void validate_while_stmt(struct node *node) {
validation_new_scope(0);
validate_expressionable(node->stmt.while_stmt.cond);
validate_body(&node->stmt.while_stmt.body->body);
validation_finish_scope();
}
void validate_do_while_stmt(struct node *node) {
validation_new_scope(0);
validate_expressionable(node->stmt.do_while_stmt.cond);
validate_body(&node->stmt.do_while_stmt.body->body);
validation_finish_scope();
}
void validate_switch_stmt(struct node *node) {
validation_new_scope(0);
validate_expressionable(node->stmt.switch_stmt.exp);
validate_body(&node->stmt.switch_stmt.body->body);
validation_finish_scope();
}
void validate_statement(struct node *node) {
switch (node->type) {
case NODE_TYPE_VARIABLE:
validate_variable(node);
break;
case NODE_TYPE_STATEMENT_RETURN:
validate_return_node(node);
break;
case NODE_TYPE_STATEMENT_IF:
validate_if_stmt(node);
break;
case NODE_TYPE_STATEMENT_FOR:
validate_for_stmt(node);
break;
case NODE_TYPE_STATEMENT_WHILE:
validate_while_stmt(node);
break;
case NODE_TYPE_STATEMENT_DO_WHILE:
validate_do_while_stmt(node);
break;
case NODE_TYPE_STATEMENT_SWITCH:
validate_switch_stmt(node);
break;
}
}
void validate_body(struct body *body) {
vector_set_peek_pointer(body->statements, 0);
struct node *statement = vector_peek_ptr(body->statements);
while (statement) {
validate_statement(statement);
statement = vector_peek_ptr(body->statements);
}
}
void validate_function_body(struct node *node) { validate_body(&node->body); }
void validate_variable(struct node *var_node) {
struct resolver_entity *entity = resolver_get_variable_from_local_scope(
validator_current_compile_process->resolver, var_node->var.name);
if (entity) {
compiler_node_error(var_node, "variable %s already defined",
var_node->var.name);
}
resolver_default_new_scope_entity(validator_current_compile_process->resolver,
var_node, 0, 0);
}
void validate_function_arg(struct node *func_arg_var_node) {
validate_variable(func_arg_var_node);
}
void validate_function_args(struct function_args *func_args) {
struct vector *func_arg_vec = func_args->args;
vector_set_peek_pointer(func_arg_vec, 0);
struct node *current = vector_peek_ptr(func_arg_vec);
while (current) {
validate_function_arg(current);
current = vector_peek_ptr(func_arg_vec);
}
}
void validate_function_node(struct node *node) {
current_function = node;
if (!(node->flags & NODE_FLAG_IS_FORWARD_DECLARATION)) {
validate_symbol_unique(node->func.name, "function", node);
}
symresolver_register_symbol(validator_current_compile_process,
node->func.name, SYMBOL_TYPE_NODE, node);
validation_new_scope(0);
// validate function args
validate_function_args(&node->func.args);
// validate function body
if (node->func.body_n) {
validate_function_body(node->func.body_n);
}
validation_finish_scope();
}
void validate_struct_node(struct node *node) {
if (!(node->flags & NODE_FLAG_IS_FORWARD_DECLARATION)) {
validate_symbol_unique(node->_struct.name, "struct", node);
}
symresolver_register_symbol(validator_current_compile_process,
node->_struct.name, SYMBOL_TYPE_NODE, node);
}
void validate_union_node(struct node *node) {
if (!(node->flags & NODE_FLAG_IS_FORWARD_DECLARATION)) {
validate_symbol_unique(node->_union.name, "union", node);
}
symresolver_register_symbol(validator_current_compile_process,
node->_union.name, SYMBOL_TYPE_NODE, node);
}
void validate_node(struct node *node) {
switch (node->type) {
case NODE_TYPE_FUNCTION:
validate_function_node(node);
break;
case NODE_TYPE_STRUCT:
validate_struct_node(node);
break;
case NODE_TYPE_UNION:
validate_union_node(node);
break;
}
}
int validate_tree() {
validation_new_scope(0);
struct node *node = validation_next_tree_node();
while (node) {
validate_node(node);
node = validation_next_tree_node();
}
validation_finish_scope();
return VALIDATION_ALL_OK;
}
int validate(struct compile_process *process) {
int res = 0;
validate_init(process);
res = validate_tree();
validate_destroy(process);
return res;
}