-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.c
143 lines (120 loc) · 3.49 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
#include "compiler.h"
#include "helpers/vector.h"
static struct compile_process* validator_current_compile_process;
static struct node* current_function;
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_body(struct body* body)
{
vector_set_peek_pointer(body->statements, 0);
struct node* statement = vector_peek_ptr(body->statements);
while (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_node(struct node* node)
{
switch (node->type)
{
case NODE_TYPE_FUNCTION:
validate_function_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;
}