-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.c
261 lines (216 loc) · 6.12 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "compiler.h"
#include "helpers/vector.h"
static struct compile_process* validator_current_compile_process;
static struct node* current_function;
// < Validator global functions start
void validate_body(struct body* body);
// > Validator global functions end
void validation_new_scope(int flags)
{
resolver_default_new_scope(validator_current_compile_process->resolver, flags);
}
void validation_end_scope()
{
resolver_default_finish_scope(validator_current_compile_process->resolver);
}
struct node* validator_next_tree_node()
{
return vector_peek_ptr(validator_current_compile_process->node_tree_vec);
}
void validator_initialize(struct compile_process* process)
{
validator_current_compile_process = process;
vector_set_peek_pointer(process->node_tree_vec, 0);
symresolver_new_table(process);
}
void validator_destruct(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_of_symbol, struct node* node)
{
struct symbol* sym = symresolver_get_symbol(validator_current_compile_process, name);
if (sym)
{
compiler_node_error(node, "Cannot define %s %s, you have already defines a symbol with the name %s", type_of_symbol, name, name);
}
}
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, "You have already defined the variable %s in the given scope", var_node->var.name);
}
resolver_default_new_scope_entity(validator_current_compile_process->resolver, var_node, 0, 0);
}
void validate_function_argument(struct node* func_argument_var_node)
{
validate_variable(func_argument_var_node);
}
void validate_function_arguments(struct function_arguments* func_arguments)
{
struct vector* func_arg_vec = func_arguments->vector;
vector_set_peek_pointer(func_arg_vec, 0);
struct node* current = vector_peek_ptr(func_arg_vec);
while (current)
{
validate_function_argument(current);
current = vector_peek_ptr(func_arg_vec);
}
}
void validate_identifier(struct node* node)
{
struct resolver_result* result = resolver_follow(validator_current_compile_process->resolver, node);
if (!resolver_result_ok(result))
{
compiler_node_error(node, "The variable %s does not exist", 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, "You are returning a value in a function %s which has a return type of void\n", current_function->func.name);
}
validate_expressionable(node->stmt.return_stmt.exp);
}
}
void validate_if_stmt(struct node* node)
{
validation_new_scope(0);
validate_body(&node->stmt.if_stmt.body_node->body);
validation_end_scope();
}
void validate_else_stmt(struct node* node)
{
validation_new_scope(0);
validate_body(&node->stmt.else_stmt.body_node->body);
validation_end_scope();
}
void validate_for_stmt(struct node* node)
{
validation_new_scope(0);
validate_body(&node->stmt.for_stmt.body_node->body);
validation_end_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_ELSE:
validate_else_stmt(node);
break;
case NODE_TYPE_STATEMENT_FOR:
validate_for_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_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 the function arguments
validate_function_arguments(&node->func.args);
// Validate the function body
if (node->func.body_n)
{
validate_function_body(node->func.body_n);
}
validation_end_scope();
current_function = NULL;
}
void validate_structure_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_VARIABLE:
validate_variable(node);
break;
case NODE_TYPE_FUNCTION:
validate_function_node(node);
break;
case NODE_TYPE_STRUCT:
validate_structure_node(node);
break;
case NODE_TYPE_UNION:
validate_union_node(node);
break;
}
}
int validate_tree()
{
validation_new_scope(0);
struct node* node = validator_next_tree_node();
while (node)
{
validate_node(node);
node = validator_next_tree_node();
}
validation_end_scope();
return VALIDATOR_ALL_OK;
}
int validate(struct compile_process* process)
{
int res = 0;
validator_initialize(process);
res = validate_tree();
validator_destruct(process);
return res;
}