Skip to content

Commit

Permalink
impl validation for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 9, 2024
1 parent a1cf0f4 commit 2bbc26d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
10 changes: 10 additions & 0 deletions compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ struct lex_process_functions compiler_lex_functions = {
.peek_char = compile_process_peek_char,
.push_char = compile_process_push_char};

void compiler_node_error(struct node *node, const char *msg, ...) {
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " on line %i, col %i in file %s\n", node->pos.line,
node->pos.col, node->pos.filename);
exit(-1);
}

void compiler_error(struct compile_process *compiler, const char *msg, ...) {
va_list args;
va_start(args, msg);
Expand Down
1 change: 1 addition & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ void compiler_setup_default_include_dir(struct vector *include_dirs);
struct compile_process *compile_include(const char *filename,
struct compile_process *parent_process);

void compiler_node_error(struct node *node, const char *msg, ...);
void compiler_error(struct compile_process *compiler, const char *msg, ...);
void compiler_warning(struct compile_process *compiler, const char *msg, ...);

Expand Down
8 changes: 2 additions & 6 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <stdio.h>
int test() {}

int main() {
FILE *f = fopen("test.txt", "w");
fwrite("Hello, World!\n", 1, 14, f);
fclose(f);
}
int test() { return 0; }
73 changes: 72 additions & 1 deletion validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,78 @@ void validate_destroy(struct compile_process *process) {
vector_set_peek_pointer(process->node_tree_vec, 0);
}

int validate_tree() { return VALIDATION_ALL_OK; }
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_body(struct body *body) {
vector_set_peek_pointer(body->statements, 0);
struct node *statement = vector_peek_ptr(body->statements);
while (statement) {
// validate statement
statement = vector_peek_ptr(body->statements);
}
}

void validate_function_body(struct node *node) { validate_body(&node->body); }

void validate_function_arg(struct node *func_arg_var_node) {
// validate_variable
}

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_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 = 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;
Expand Down

0 comments on commit 2bbc26d

Please sign in to comment.