Skip to content

Commit

Permalink
impl validation for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 9, 2024
1 parent 2bbc26d commit ad3dc18
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,9 @@ struct resolver_entity *
resolver_result_entity_root(struct resolver_result *result);
struct resolver_entity *resolver_entity_next(struct resolver_entity *entity);
struct resolver_entity *resolver_result_entity(struct resolver_result *result);
struct resolver_entity *
resolver_get_variable_from_local_scope(struct resolver_process *process,
const char *var_name);

int codegen(struct compile_process *process);
struct code_generator *codegenerator_new(struct compile_process *process);
Expand Down
8 changes: 8 additions & 0 deletions resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,14 @@ struct resolver_entity *resolver_get_variable(struct resolver_result *result,
RESOLVER_ENTITY_TYPE_VARIABLE);
}

struct resolver_entity *
resolver_get_variable_from_local_scope(struct resolver_process *process,
const char *var_name) {
struct resolver_result *result = resolver_new_result(process);
return resolver_get_entity_in_scope(
result, process, resolver_scope_current(process), var_name);
}

struct resolver_entity *resolver_get_function_in_scope(
struct resolver_result *result, struct resolver_process *process,
const char *func_name, struct resolver_scope *scope) {
Expand Down
4 changes: 1 addition & 3 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
int test() {}

int test() { return 0; }
int test(int x, int x) { return 0; }
14 changes: 13 additions & 1 deletion validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ void validate_body(struct body *body) {

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
validate_variable(func_arg_var_node);
}

void validate_function_args(struct function_args *func_args) {
Expand Down

0 comments on commit ad3dc18

Please sign in to comment.