Compare commits

...

3 Commits

Author SHA1 Message Date
David Mak de21aaf907 WIP - Fix string-like format 2024-08-12 17:25:41 +08:00
David Mak 4920989c00 WIP - Fix bool format 2024-08-12 17:25:41 +08:00
David Mak dbbc4ed78c WIP - Fix expected type of Tuple 2024-08-12 17:25:39 +08:00
1 changed files with 31 additions and 18 deletions

View File

@ -804,21 +804,29 @@ fn polymorphic_print<'ctx>(
}
match &*ctx.unifier.get_ty_immutable(ty) {
TypeEnum::TTuple { ty, is_vararg_ctx: false } => {
TypeEnum::TTuple { ty: tys, is_vararg_ctx: false } => {
debug_assert_eq!(
tys.len() as u32,
value.into_struct_value().count_fields(),
"Expected {} types for {}, got [{}] ({} types) in corresponding LLVM type",
tys.len(),
ctx.unifier.stringify(ty),
value.into_struct_value().get_fields().map(|field| field.get_type().to_string()).join(", "),
value.into_struct_value().count_fields(),
);
fmt.push('(');
flush(ctx, generator, &mut fmt, &mut args);
let tuple_vals = ty
let tuple_vals = tys
.iter()
.enumerate()
.map(|(i, ty)| {
(
*ty,
ValueEnum::from(ctx.build_in_bounds_gep_and_load(
value.into_pointer_value(),
&[llvm_i32.const_int(i as u64, false)],
None,
)),
unsafe {
ValueEnum::from(value.into_struct_value().get_field_at_index_unchecked(i as u32))
},
)
})
.collect_vec();
@ -841,18 +849,18 @@ fn polymorphic_print<'ctx>(
fmt.push_str("%.*s");
let true_str = ctx.gen_string(generator, "True");
let true_data = unsafe { true_str.get_field_at_index_unchecked(0) }.into_pointer_value();
let true_len = unsafe { true_str.get_field_at_index_unchecked(1) }.into_int_value();
let false_str = ctx.gen_string(generator, "False");
let false_data = unsafe { false_str.get_field_at_index_unchecked(0) }.into_pointer_value();
let false_len = unsafe { false_str.get_field_at_index_unchecked(1) }.into_int_value();
args.push(
ctx.builder
.build_select(
generator.bool_to_i1(ctx, value.into_int_value()),
true_str,
false_str,
"",
)
.unwrap(),
);
let bool_val = generator.bool_to_i1(ctx, value.into_int_value());
args.extend([
ctx.builder.build_select(bool_val, true_len, false_len, "").unwrap(),
ctx.builder.build_select(bool_val, true_data, false_data, "").unwrap(),
]);
}
TypeEnum::TObj { obj_id, .. }
@ -888,7 +896,12 @@ fn polymorphic_print<'ctx>(
} else {
fmt.push_str("%.*s");
}
args.push(value);
let str = value.into_struct_value();
let str_data = unsafe { str.get_field_at_index_unchecked(0) }.into_pointer_value();
let str_len = unsafe { str.get_field_at_index_unchecked(1) }.into_int_value();
args.extend(&[str_len.into(), str_data.into()]);
}
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::List.id() => {