Add some support for recursive structs

Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
Slendi 2024-04-27 05:47:06 +03:00
parent c5747a103e
commit b170e9a424
4 changed files with 96 additions and 50 deletions

View File

@ -1,3 +1,18 @@
struct Vec2 {
x y: f32,
}
struct Rect {
pos size: Vec2,
}
let CONSTPOS :: .Vec2 {20.0 20.0}
let CONSTSZ :: .Vec2 {100.0 100.0}
let CONST :: .Rect {(CONSTPOS) (CONSTSZ)}
\ FIXME: LLVM IR geneartion is borked
\ let CONST :: .Rect { .Vec2 { 20.0 20.0 } .Vec2 { 100.0 100.0 } }
struct Color { struct Color {
r g b a: u8, r g b a: u8,
} }
@ -47,10 +62,10 @@ fn DrawTextWrap(text: []u8, x y size: i32, c: Color) {
DrawText text x y size (ColorToRaylib c) DrawText text x y size (ColorToRaylib c)
} }
let WHITE :: .Color{255 255 255 255} let WHITE :: .Color {255 255 255 255}
let BLACK :: .Color{0 0 0 255} let BLACK :: .Color {0 0 0 255}
let RED :: .Color{255 0 0 255} let RED :: .Color {255 0 0 255}
let BLUE :: .Color{0 0 255 255} let BLUE :: .Color {0 0 255 255}
InitWindow 800 450 Raylib InitWindow 800 450 Raylib
SetTargetFPS 60 SetTargetFPS 60
@ -58,6 +73,10 @@ SetTargetFPS 60
fn GetScreenWidth i32 fn GetScreenWidth i32
fn GetScreenHeight i32 fn GetScreenHeight i32
fn DrawRect(rect: Rect, c: Color) {
DrawRectangleWrap (rect.pos.x as i32) (rect.pos.y as i32) (rect.size.x as i32) (rect.size.y as i32) c
}
let logox := GetScreenWidth / 2 - 128 let logox := GetScreenWidth / 2 - 128
let logoy := GetScreenHeight / 2 - 128 let logoy := GetScreenHeight / 2 - 128
@ -121,6 +140,8 @@ for WindowShouldClose == 0 {
ClearBackgroundWrap WHITE ClearBackgroundWrap WHITE
DrawFPS 20 20 DrawFPS 20 20
DrawRect CONST RED
if state == 0 { if state == 0 {
if (fc/15)%2 != 0 { if (fc/15)%2 != 0 {
DrawRectangleWrap logox logoy 16 16 BLACK DrawRectangleWrap logox logoy 16 16 BLACK

View File

@ -325,20 +325,23 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
mod: LLVMModuleRef, mod: LLVMModuleRef,
builder: LLVMBuilderRef, builder: LLVMBuilderRef,
node: ^Node, node: ^Node,
) -> LLVMValueRef { ) -> (
LLVMValueRef,
LLVMValueRef,
) {
if node.kind == .Integer { if node.kind == .Integer {
return generate_llvm_integer(ctx, mod, builder, node) return generate_llvm_integer(ctx, mod, builder, node), nil
} else if node.kind == .String { } else if node.kind == .String {
return generate_llvm_string(ctx, mod, builder, node) return generate_llvm_string(ctx, mod, builder, node), nil
} else if node.kind == .Float { } else if node.kind == .Float {
return generate_llvm_float(ctx, mod, builder, node) return generate_llvm_float(ctx, mod, builder, node), nil
} else if node.kind == .FunctionCall { } else if node.kind == .FunctionCall {
return generate_llvm_function_call(ctx, mod, builder, node) return generate_llvm_function_call(ctx, mod, builder, node), nil
} else if node.kind == .Identifier { } else if node.kind == .Identifier {
def := llvm_scope_find_definition(&node.value.([dynamic]u8)) def := llvm_scope_find_definition(&node.value.([dynamic]u8))
type := llvm_scope_find_type(&node.value.([dynamic]u8)) type := llvm_scope_find_type(&node.value.([dynamic]u8))
def_value := LLVMBuildLoad2(builder, type, def, "loadtmp") def_value := LLVMBuildLoad2(builder, type, def, "loadtmp")
return def_value return def_value, nil
} else if node.kind == .StructInitializer { } else if node.kind == .StructInitializer {
struct_name := &node.value.([dynamic]u8) struct_name := &node.value.([dynamic]u8)
struct_type := llvm_scope_find_type(struct_name) struct_type := llvm_scope_find_type(struct_name)
@ -347,30 +350,44 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
&g_message_list, &g_message_list,
message_create(.Error, fmt.aprintf("Struct '%s' not found", struct_name), node.range), message_create(.Error, fmt.aprintf("Struct '%s' not found", struct_name), node.range),
) )
return nil return nil, nil
} }
struct_value := g_last_alloca struct_value := g_last_alloca
struct_values := [dynamic]LLVMValueRef{}
for &field, i in node.children[:] { for &field, i in node.children[:] {
last_alloc_a := g_last_alloca
field_value := generate_llvm_expression(ctx, mod, builder, field) field_value := generate_llvm_expression(ctx, mod, builder, field)
g_last_alloca = last_alloc_a
ptr_field := LLVMBuildStructGEP2(builder, struct_type, struct_value, uint(i), "inigep") ptr_field := LLVMBuildStructGEP2(builder, struct_type, struct_value, uint(i), "inigep")
LLVMBuildStore(builder, field_value, ptr_field) LLVMBuildStore(builder, field_value, ptr_field)
append(&struct_values, field_value)
} }
return struct_value return struct_value, nil
} else if node.kind == .FieldAccess { } else if node.kind == .FieldAccess {
struct_name: ^[dynamic]u8
struct_value: LLVMValueRef
if node.children[0].kind == .FieldAccess { if node.children[0].kind == .FieldAccess {
append( struct_name = &node.children[0].return_type.struct_type.name
&g_message_list, _, struct_value = generate_llvm_value(ctx, mod, builder, node.children[0])
message_create(.FIXME, fmt.aprintf("Nested field accesses are not implemented."), node.range), } else if node.children[0].kind == .Identifier {
) struct_name = &node.children[0].value.([dynamic]u8)
return nil } else {
panic("LLVM: Invalid LHS")
}
type_struct := llvm_scope_find_type(struct_name)
if type_struct == nil {
fmt.println("Unable to find struct type definition")
}
def_struct := llvm_scope_find_definition(struct_name)
if struct_value != nil {
def_struct = struct_value
}
if def_struct == nil {
fmt.println("Unable to find struct definition")
} }
def_struct := llvm_scope_find_definition(&node.children[0].value.([dynamic]u8))
type_struct := llvm_scope_find_type(&node.children[0].value.([dynamic]u8))
type_ref := generate_llvm_type_from_node(ctx, mod, builder, node.return_type) type_ref := generate_llvm_type_from_node(ctx, mod, builder, node.return_type)
struct_field_data := LLVMBuildAlloca(builder, type_ref, "struct_field") struct_field_data := LLVMBuildAlloca(builder, type_ref, "struct_field")
@ -383,7 +400,7 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
) )
def_value := LLVMBuildLoad2(builder, type_ref, def, "loadtmp") def_value := LLVMBuildLoad2(builder, type_ref, def, "loadtmp")
return def_value return def_value, def
} }
fmt.panicf("FIXME: Implement other node kinds. Got: {}", node.kind) fmt.panicf("FIXME: Implement other node kinds. Got: {}", node.kind)
@ -507,7 +524,8 @@ generate_llvm :: proc(ctx: LLVMContextRef, mod: LLVMModuleRef, builder: LLVMBuil
node.kind == .Identifier || node.kind == .Identifier ||
node.kind == .StructInitializer || node.kind == .StructInitializer ||
node.kind == .FieldAccess { node.kind == .FieldAccess {
return generate_llvm_value(ctx, mod, builder, node) v, _ := generate_llvm_value(ctx, mod, builder, node)
return v
} }
if node.kind == .FunctionCall { if node.kind == .FunctionCall {
return generate_llvm_function_call(ctx, mod, builder, node) return generate_llvm_function_call(ctx, mod, builder, node)

View File

@ -61,6 +61,19 @@ compare_types :: proc(a: ^Type, b: ^Type) -> (ret: bool, cast_required: bool) {
return return
} }
if (a.kind == .Struct && b.kind != .Struct) {
return false, false
}
if (a.kind != .Struct && b.kind == .Struct) {
return false, false
}
if (a.kind == .Struct && b.kind == .Struct) {
if (compare_dyn_arrs(&a.struct_type.name, &b.struct_type.name)) {
return true, false
}
return false, false
}
if (a.kind == .Integer || a.kind == .Float) && (a.bit_size > b.bit_size) { if (a.kind == .Integer || a.kind == .Float) && (a.bit_size > b.bit_size) {
ret = true ret = true
cast_required = true cast_required = true

View File

@ -268,22 +268,19 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
if rhs.kind != .Identifier { if rhs.kind != .Identifier {
append( append(
&g_message_list, &g_message_list,
message_create(.Error, fmt.aprintf("Field access rhs is not an identifier or field access: {}", rhs.kind), rhs.range), message_create(
) .Error,
fmt.aprintf("Field access rhs is not an identifier or field access: {}", rhs.kind),
rhs.range,
),
)
break break
} }
if lhs.kind == .FieldAccess { if lhs.kind == .FieldAccess {
type_check(lhs, ast) type_check(lhs, ast)
if lhs.return_type.kind != .Struct { if lhs.return_type.kind != .Struct {
append( append(&g_message_list, message_create(.Error, fmt.aprintf("LHS is not a Struct type"), lhs.range))
&g_message_list,
message_create(
.Error,
fmt.aprintf("LHS is not a Struct type"),
lhs.range,
),
)
} }
struct_ = scope_struct_lookup(lhs.return_type.struct_type.name) struct_ = scope_struct_lookup(lhs.return_type.struct_type.name)
@ -308,9 +305,7 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
struct_index: u64 = 0 struct_index: u64 = 0
found_field := false found_field := false
fmt.println("==")
for &field, i in struct_.fields { for &field, i in struct_.fields {
fmt.printf("- %s\n", field.name)
if compare_dyn_arrs(&field.name, &rhs.value.([dynamic]u8)) { if compare_dyn_arrs(&field.name, &rhs.value.([dynamic]u8)) {
ast.return_type = field.type ast.return_type = field.type
found_field = true found_field = true
@ -347,17 +342,13 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
type := scope_variable_lookup(ast.children[0].value.([dynamic]u8)) type := scope_variable_lookup(ast.children[0].value.([dynamic]u8))
if type != nil { if type != nil {
if len(ast.children) != 1 {
append(&g_message_list, message_create(.Error, "Variable is not a function", ast.range))
break
}
name := ast.children[0].value.([dynamic]u8) name := ast.children[0].value.([dynamic]u8)
free(ast.children[0]) free(ast.children[0])
clear(&ast.children) clear(&ast.children)
ast.return_type = type ast.return_type = type
ast.kind = .Identifier ast.kind = .Identifier
ast.value = name ast.value = name
type_check(ast, parent_ast)
} else { } else {
fn := type_check_function_call(ast, parent_ast) fn := type_check_function_call(ast, parent_ast)
if fn != nil { if fn != nil {
@ -394,8 +385,8 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
fmt.aprintf( fmt.aprintf(
"Type mismatch in function call for `%s`: Wanted {}, got {}", "Type mismatch in function call for `%s`: Wanted {}, got {}",
fn.name, fn.name,
param, type_to_string(param),
ast.children[i + 1].return_type, type_to_string(ast.children[i + 1].return_type),
), ),
ast.range, ast.range,
), ),
@ -467,11 +458,11 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
) )
} }
} else if ast.value_token_kind == .Equals || } else if ast.value_token_kind == .Equals ||
ast.value_token_kind == .NotEquals || ast.value_token_kind == .NotEquals ||
ast.value_token_kind == .GreaterThan || ast.value_token_kind == .GreaterThan ||
ast.value_token_kind == .GreaterThanOrEqual || ast.value_token_kind == .GreaterThanOrEqual ||
ast.value_token_kind == .LessThan || ast.value_token_kind == .LessThan ||
ast.value_token_kind == .LessThanOrEqual { ast.value_token_kind == .LessThanOrEqual {
ast.return_type = type_create_integer(1, true) ast.return_type = type_create_integer(1, true)
} }
// FIXME: Verify that the operation is possible // FIXME: Verify that the operation is possible
@ -670,6 +661,8 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
type_check(child, ast) type_check(child, ast)
} }
node_print(ast)
struct_ := find_struct(ast.value.([dynamic]u8)) struct_ := find_struct(ast.value.([dynamic]u8))
if struct_ == nil { if struct_ == nil {
append( append(
@ -697,6 +690,7 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
idx := 0 idx := 0
for &child in ast.children { for &child in ast.children {
fmt.printf("Comp {} and {} (struct f)\n", child.return_type.kind, struct_.fields[idx].type.kind)
ok, cast_required := compare_types(child.return_type, struct_.fields[idx].type) ok, cast_required := compare_types(child.return_type, struct_.fields[idx].type)
if cast_required { if cast_required {
cast_ := node_create_cast({}, child, {}) cast_ := node_create_cast({}, child, {})
@ -710,10 +704,10 @@ type_check :: proc(ast: ^Node, parent_ast: ^Node) {
.Error, .Error,
fmt.aprintf( fmt.aprintf(
"Type mismatch in struct initializer: Wanted {}, got {}", "Type mismatch in struct initializer: Wanted {}, got {}",
struct_.fields[idx].type, type_to_string(struct_.fields[idx].type),
child.return_type, type_to_string(child.return_type),
), ),
ast.range, child.range,
), ),
) )
} }