Get recursive structs and arrays working too

This commit is contained in:
Sofia 2025-07-20 19:59:03 +03:00
parent d631e80267
commit be76331a47

View File

@ -685,11 +685,17 @@ impl mir::Expression {
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
let TypeKind::Array(elem_ty, _) = array_ty else { dbg!(&array_ty);
let TypeKind::Ptr(inner) = array_ty else {
panic!();
};
let TypeKind::Array(elem_ty, _) = *inner else {
panic!(); panic!();
}; };
let elem_value = if state.should_load { if state.should_load {
Some(StackValue(
kind.derive(
scope scope
.block .block
.build(Instr::Load( .build(Instr::Load(
@ -697,12 +703,13 @@ impl mir::Expression {
val_t.get_type(scope.type_values, scope.types), val_t.get_type(scope.type_values, scope.types),
)) ))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location) .maybe_location(&mut scope.block, location),
),
*elem_ty,
))
} else { } else {
ptr Some(StackValue(kind.derive(ptr), TypeKind::Ptr(elem_ty)))
}; }
Some(StackValue(kind.derive(elem_value), *elem_ty))
} }
mir::ExprKind::Array(expressions) => { mir::ExprKind::Array(expressions) => {
let stack_value_list = expressions let stack_value_list = expressions
@ -766,14 +773,17 @@ impl mir::Expression {
mir::ExprKind::Accessed(expression, type_kind, field) => { mir::ExprKind::Accessed(expression, type_kind, field) => {
let struct_val = expression.codegen(scope, state).unwrap(); let struct_val = expression.codegen(scope, state).unwrap();
let TypeKind::CustomType(name) = &struct_val.1 else { let TypeKind::Ptr(inner) = &struct_val.1 else {
panic!("tried accessing non-pointer");
};
let TypeKind::CustomType(name) = *inner.clone() else {
panic!("tried accessing non-custom-type"); panic!("tried accessing non-custom-type");
}; };
let TypeDefinitionKind::Struct(struct_ty) = let TypeDefinitionKind::Struct(struct_ty) =
scope.get_typedef(&name).unwrap().kind.clone(); scope.get_typedef(&name).unwrap().kind.clone();
let idx = struct_ty.find_index(field).unwrap(); let idx = struct_ty.find_index(field).unwrap();
let mut value = scope let value = scope
.block .block
.build(Instr::GetStructElemPtr(struct_val.instr(), idx as u32)) .build(Instr::GetStructElemPtr(struct_val.instr(), idx as u32))
.unwrap(); .unwrap();
@ -781,19 +791,24 @@ impl mir::Expression {
// value.maybe_location(&mut scope.block, location); // value.maybe_location(&mut scope.block, location);
if state.should_load { if state.should_load {
value = scope Some(StackValue(
struct_val.0.derive(
scope
.block .block
.build(Instr::Load( .build(Instr::Load(
value, value,
type_kind.get_type(scope.type_values, scope.types), type_kind.get_type(scope.type_values, scope.types),
)) ))
.unwrap(); .unwrap(),
} ),
Some(StackValue(
struct_val.0.derive(value),
struct_ty.get_field_ty(&field).unwrap().clone(), struct_ty.get_field_ty(&field).unwrap().clone(),
)) ))
} else {
Some(StackValue(
struct_val.0.derive(value),
TypeKind::Ptr(Box::new(struct_ty.get_field_ty(&field).unwrap().clone())),
))
}
} }
mir::ExprKind::Struct(name, items) => { mir::ExprKind::Struct(name, items) => {
let struct_ty = Type::CustomType(*scope.type_values.get(name)?); let struct_ty = Type::CustomType(*scope.type_values.get(name)?);