diff --git a/src/ast.odin b/src/ast.odin index 963a646..6190c94 100644 --- a/src/ast.odin +++ b/src/ast.odin @@ -432,11 +432,12 @@ node_create_struct_or_union :: proc( return } -node_create_enum :: proc(range: SourceLocation, data: EnumValue) -> (ret: ^Node) { +node_create_enum :: proc(range: SourceLocation, data: EnumValue, name: [dynamic]u8) -> (ret: ^Node) { ret = new(Node) ret^ = { kind = .Enum, enum_value = data, + value = name, } return } diff --git a/src/main.odin b/src/main.odin index 1d0e644..4c6c629 100644 --- a/src/main.odin +++ b/src/main.odin @@ -46,20 +46,20 @@ main :: proc() { os.exit(1) } } - //clear(&g_message_list) - //type_check(ast, nil) - //if len(g_message_list) > 0 { - // contains_errors := false - // for &msg in g_message_list { - // message_print(&msg, &data) - // if msg.level == .Error || msg.level == .Fatal { - // contains_errors = true - // } - // } - // if contains_errors { - // os.exit(1) - // } - //} + clear(&g_message_list) + type_check(ast, nil) + if len(g_message_list) > 0 { + contains_errors := false + for &msg in g_message_list { + message_print(&msg, &data) + if msg.level == .Error || msg.level == .Fatal { + contains_errors = true + } + } + if contains_errors { + os.exit(1) + } + } node_print(ast) diff --git a/src/parser.odin b/src/parser.odin index e46b271..d5eb3f8 100644 --- a/src/parser.odin +++ b/src/parser.odin @@ -277,7 +277,7 @@ parser_parse_enum_definition :: proc(parser: ^Parser) -> ^Node { enum_value.type = type_node - return node_create_enum(range, enum_value) + return node_create_enum(range, enum_value, name) } @(private = "file") diff --git a/src/type_checker.odin b/src/type_checker.odin index f338bb8..4080f3c 100644 --- a/src/type_checker.odin +++ b/src/type_checker.odin @@ -39,6 +39,7 @@ Scope :: struct { variable_mutability_definitions: map[int]bool, // A map to a variable's mutability function_return_type: ^Type, structure_definitions: map[int]^Struct, + enum_definitions: map[int]^EnumValue, } find_struct :: proc(name: [dynamic]u8) -> ^Struct { @@ -665,7 +666,8 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) { } } scope_leave() - case .Struct: + case .Struct: // Nothing + case .Enum: // Nothing case .StructInitializer: for child in ast.children { type_check(child, ast) @@ -802,6 +804,23 @@ find_function_definitions :: proc(ast_: ^Node) -> (ret: [dynamic]^FunctionType) } scope_stack[len(scope_stack) - 1].structure_definitions[get_character_sum_of_dyn_arr(&ast.children[0].value.([dynamic]u8))] = struct_ + case .Enum: + if ast.enum_value.type.kind != .Identifier { + append( + &g_message_list, + message_create(.Error, "The type of this struct must be a scalar", ast.enum_value.type.range), + ) + } + + type_ptr := ast_to_type(ast.enum_value.type) + if type_ptr.kind != .Integer { + append( + &g_message_list, + message_create(.Error, "The type of this struct must be an integer", ast.enum_value.type.range), + ) + } + scope_stack[len(scope_stack) - 1].enum_definitions[get_character_sum_of_dyn_arr(&ast.value.([dynamic]u8))] = + &ast.enum_value case: } }