mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 00:24:34 +00:00
List.single implementation
This commit is contained in:
parent
d79f1af126
commit
97cb4a737e
5 changed files with 109 additions and 1 deletions
|
@ -507,6 +507,12 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
|||
),
|
||||
);
|
||||
|
||||
// single : a -> List a
|
||||
add_type(
|
||||
Symbol::LIST_SINGLE,
|
||||
SolvedType::Func(vec![flex(TVAR1)], Box::new(list_type(flex(TVAR1)))),
|
||||
);
|
||||
|
||||
// len : List * -> Int
|
||||
add_type(
|
||||
Symbol::LIST_LEN,
|
||||
|
|
|
@ -593,6 +593,28 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
|||
)
|
||||
});
|
||||
|
||||
add_type(Symbol::LIST_SINGLE, {
|
||||
let u = UVAR1;
|
||||
let v = UVAR2;
|
||||
let star = UVAR4;
|
||||
|
||||
let a = TVAR1;
|
||||
|
||||
unique_function(
|
||||
vec![SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![disjunction(u, vec![v]), flex(a)],
|
||||
)],
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
boolean(star),
|
||||
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
|
||||
],
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
// push : Attr (w | u | v) (List (Attr u a))
|
||||
// , Attr (u | v) a
|
||||
// -> Attr * (List (Attr u a))
|
||||
|
|
|
@ -435,7 +435,7 @@ pub fn build_expr<'a, 'ctx, 'env>(
|
|||
if elems.is_empty() {
|
||||
let struct_type = collection(ctx, env.ptr_bytes);
|
||||
|
||||
// THe pointer should be null (aka zero) and the length should be zero,
|
||||
// The pointer should be null (aka zero) and the length should be zero,
|
||||
// so the whole struct should be a const_zero
|
||||
BasicValueEnum::StructValue(struct_type.const_zero())
|
||||
} else {
|
||||
|
@ -1402,6 +1402,74 @@ fn call_with_args<'a, 'ctx, 'env>(
|
|||
Symbol::FLOAT_ROUND => call_intrinsic(LLVM_LROUND_I64_F64, env, args),
|
||||
Symbol::LIST_SET => list_set(parent, args, env, InPlace::Clone),
|
||||
Symbol::LIST_SET_IN_PLACE => list_set(parent, args, env, InPlace::InPlace),
|
||||
Symbol::LIST_SINGLE => {
|
||||
// List.single : a -> List a
|
||||
debug_assert!(args.len() == 1);
|
||||
|
||||
let (elem, elem_layout) = args[0];
|
||||
|
||||
let builder = env.builder;
|
||||
let ctx = env.context;
|
||||
|
||||
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
|
||||
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
||||
|
||||
let ptr = {
|
||||
let bytes_len = elem_bytes;
|
||||
let len_type = env.ptr_int();
|
||||
let len = len_type.const_int(bytes_len, false);
|
||||
|
||||
env.builder
|
||||
.build_array_malloc(elem_type, len, "create_list_ptr")
|
||||
.unwrap()
|
||||
|
||||
// TODO check if malloc returned null; if so, runtime error for OOM!
|
||||
};
|
||||
|
||||
// Put the element into the list
|
||||
let elem_ptr = unsafe {
|
||||
builder.build_in_bounds_gep(
|
||||
ptr,
|
||||
&[ctx.i32_type().const_int(
|
||||
// 0 as in 0 index of our new list
|
||||
0 as u64, false,
|
||||
)],
|
||||
"index",
|
||||
)
|
||||
};
|
||||
|
||||
builder.build_store(elem_ptr, elem);
|
||||
|
||||
let ptr_bytes = env.ptr_bytes;
|
||||
let int_type = ptr_int(ctx, ptr_bytes);
|
||||
let ptr_as_int = builder.build_ptr_to_int(ptr, int_type, "list_cast_ptr");
|
||||
let struct_type = collection(ctx, ptr_bytes);
|
||||
let len = BasicValueEnum::IntValue(env.ptr_int().const_int(1, false));
|
||||
|
||||
let mut struct_val;
|
||||
|
||||
// Store the pointer
|
||||
struct_val = builder
|
||||
.build_insert_value(
|
||||
struct_type.get_undef(),
|
||||
ptr_as_int,
|
||||
Builtin::WRAPPER_PTR,
|
||||
"insert_ptr",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Store the length
|
||||
struct_val = builder
|
||||
.build_insert_value(struct_val, len, Builtin::WRAPPER_LEN, "insert_len")
|
||||
.unwrap();
|
||||
|
||||
//
|
||||
builder.build_bitcast(
|
||||
struct_val.into_struct_value(),
|
||||
collection(ctx, ptr_bytes),
|
||||
"cast_collection",
|
||||
)
|
||||
}
|
||||
Symbol::INT_DIV_UNSAFE => {
|
||||
debug_assert!(args.len() == 2);
|
||||
|
||||
|
|
|
@ -489,6 +489,17 @@ mod gen_builtins {
|
|||
);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn list_push() {
|
||||
// assert_evals_to!("List.push [] 1", &[1], &'static [i64]);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn list_single() {
|
||||
assert_evals_to!("List.single 1", &[1], &'static [i64]);
|
||||
assert_evals_to!("List.single 5.6", &[5.6], &'static [f64]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_list_len() {
|
||||
with_larger_debug_stack(|| {
|
||||
|
|
|
@ -691,6 +691,7 @@ define_builtins! {
|
|||
14 LIST_CONCAT: "concat"
|
||||
15 LIST_FIRST: "first"
|
||||
16 LIST_FIRST_ARG: "first#list"
|
||||
17 LIST_SINGLE: "single"
|
||||
}
|
||||
7 RESULT: "Result" => {
|
||||
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue