Add error message if a variable is already declared

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
Slendi 2024-04-27 09:19:02 +03:00
parent b170e9a424
commit 2469b38464

View File

@ -107,7 +107,8 @@ ast_to_type :: proc(node: ^Node) -> ^Type {
if res != nil {
return type_create_struct(value)
}
fmt.panicf("Unhandled identifier in ast_to_type: %s", value)
append(&g_message_list, message_create(.Error, fmt.aprintf("Unknown type: %s", value), node.range))
return nil
}
} else if node.kind == .Pointer {
return type_create_pointer(ast_to_type(node.children[0]))
@ -548,6 +549,15 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
)
ast.return_type = ast_to_type(ast.children[1])
case .VariableDeclaration:
name_sum := get_character_sum_of_dyn_arr(&ast.children[0].value.([dynamic]u8))
if name_sum in scope_stack[len(scope_stack) - 1].variable_definitions {
append(
&g_message_list,
message_create(.Error, "A variable is already declared with the same name", ast.range),
)
return
}
if ast.children[2] != nil {
type_check(ast.children[2], ast)
if ast.children[1] == nil {
@ -568,14 +578,14 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
ast.range,
),
)
return
}
} else {
ast.return_type = ast_to_type(ast.children[1])
}
scope_stack[len(scope_stack) - 1].variable_definitions[get_character_sum_of_dyn_arr(&ast.children[0].value.([dynamic]u8))] =
ast.return_type
scope_stack[len(scope_stack) - 1].variable_mutability_definitions[get_character_sum_of_dyn_arr(&ast.children[0].value.([dynamic]u8))] =
!ast.value.(bool)
scope_stack[len(scope_stack) - 1].variable_definitions[name_sum] = ast.return_type
scope_stack[len(scope_stack) - 1].variable_mutability_definitions[name_sum] = !ast.value.(bool)
case .If:
type_check(ast.children[0], ast)
if ast.children[0].return_type == nil || ast.children[0].return_type.kind != .Integer {