mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +00:00
WIP
This commit is contained in:
parent
6d6ccab513
commit
3af6d5f0b3
5 changed files with 142 additions and 0 deletions
|
@ -513,6 +513,15 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||||
SolvedType::Func(vec![flex(TVAR1)], Box::new(list_type(flex(TVAR1)))),
|
SolvedType::Func(vec![flex(TVAR1)], Box::new(list_type(flex(TVAR1)))),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// repeat : Int, a -> List a
|
||||||
|
add_type(
|
||||||
|
Symbol::LIST_REPEAT,
|
||||||
|
SolvedType::Func(
|
||||||
|
vec![int_type(), flex(TVAR1)],
|
||||||
|
Box::new(list_type(flex(TVAR1))),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// len : List * -> Int
|
// len : List * -> Int
|
||||||
add_type(
|
add_type(
|
||||||
Symbol::LIST_LEN,
|
Symbol::LIST_LEN,
|
||||||
|
|
|
@ -615,6 +615,30 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// repeat : Int, a -> List a
|
||||||
|
add_type(Symbol::LIST_REPEAT, {
|
||||||
|
let u = UVAR1;
|
||||||
|
let v = UVAR2;
|
||||||
|
let star1 = UVAR4;
|
||||||
|
let star2 = UVAR5;
|
||||||
|
|
||||||
|
let a = TVAR1;
|
||||||
|
|
||||||
|
unique_function(
|
||||||
|
vec![
|
||||||
|
int_type(star1),
|
||||||
|
SolvedType::Apply(Symbol::ATTR_ATTR, vec![disjunction(u, vec![v]), flex(a)]),
|
||||||
|
],
|
||||||
|
SolvedType::Apply(
|
||||||
|
Symbol::ATTR_ATTR,
|
||||||
|
vec![
|
||||||
|
boolean(star2),
|
||||||
|
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
// push : Attr (w | u | v) (List (Attr u a))
|
// push : Attr (w | u | v) (List (Attr u a))
|
||||||
// , Attr (u | v) a
|
// , Attr (u | v) a
|
||||||
// -> Attr * (List (Attr u a))
|
// -> Attr * (List (Attr u a))
|
||||||
|
|
|
@ -1470,6 +1470,105 @@ fn call_with_args<'a, 'ctx, 'env>(
|
||||||
"cast_collection",
|
"cast_collection",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Symbol::LIST_REPEAT => {
|
||||||
|
// List.repeat : Int, a -> List a
|
||||||
|
debug_assert!(args.len() == 2);
|
||||||
|
|
||||||
|
let (elem, elem_layout) = args[1];
|
||||||
|
|
||||||
|
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!
|
||||||
|
};
|
||||||
|
|
||||||
|
// List elem length, as in, the number of elements
|
||||||
|
// in the output list. This could be negative, since
|
||||||
|
// it is just whatever value the function is given.
|
||||||
|
let list_elem_len = args[1].0.into_int_value();
|
||||||
|
|
||||||
|
let counter : BasicValueEnum<'ctx> = env.context.i64_type().const_int(0 as u64, true).into();
|
||||||
|
|
||||||
|
let comparison =
|
||||||
|
|
||||||
|
builder.build_int_compare(IntPredicate::ULT, elem_index, len, "bounds_check")
|
||||||
|
|
||||||
|
|
||||||
|
let loop_block = ctx.append_basic_block(parent, "loop");
|
||||||
|
builder.build_unconditional_branch(loop_block);
|
||||||
|
builder.position_at_end(loop_block);
|
||||||
|
|
||||||
|
let loop_step = || {
|
||||||
|
// Put the elements into the list
|
||||||
|
let elem_ptr = unsafe {
|
||||||
|
builder.build_in_bounds_gep(
|
||||||
|
ptr,
|
||||||
|
&[ctx.i32_type().const_int(0 as u64, false)],
|
||||||
|
"index",
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
builder.build_store(elem_ptr, elem);
|
||||||
|
};
|
||||||
|
// // Bounds check: only proceed if index < length.
|
||||||
|
// // Otherwise, return the list unaltered.
|
||||||
|
// let end_condition = builder.build_int_compare(
|
||||||
|
// IntPredicate::SLT,
|
||||||
|
// ctx.i32_type().const_int(0 as u64, true),
|
||||||
|
// list_elem_len,
|
||||||
|
// "loopcond",
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// // let start_alloca = b.create_entry_block_alloca("#");
|
||||||
|
//
|
||||||
|
// let i = builder.build_load()
|
||||||
|
|
||||||
|
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 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,
|
||||||
|
list_elem_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 => {
|
Symbol::INT_DIV_UNSAFE => {
|
||||||
debug_assert!(args.len() == 2);
|
debug_assert!(args.len() == 2);
|
||||||
|
|
||||||
|
|
|
@ -500,6 +500,15 @@ mod gen_builtins {
|
||||||
assert_evals_to!("List.single 5.6", &[5.6], &'static [f64]);
|
assert_evals_to!("List.single 5.6", &[5.6], &'static [f64]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_repeat() {
|
||||||
|
assert_evals_to!("List.repeat -1 1", &[], &'static [i64]);
|
||||||
|
// assert_evals_to!("List.repeat 4 5", &[5, 5, 5, 5], &'static [i64]);
|
||||||
|
|
||||||
|
// assert_evals_to!("List.repeat 0 []", &[], &'static [i64]);
|
||||||
|
// assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_list_len() {
|
fn empty_list_len() {
|
||||||
with_larger_debug_stack(|| {
|
with_larger_debug_stack(|| {
|
||||||
|
|
|
@ -692,6 +692,7 @@ define_builtins! {
|
||||||
15 LIST_FIRST: "first"
|
15 LIST_FIRST: "first"
|
||||||
16 LIST_FIRST_ARG: "first#list"
|
16 LIST_FIRST_ARG: "first#list"
|
||||||
17 LIST_SINGLE: "single"
|
17 LIST_SINGLE: "single"
|
||||||
|
18 LIST_REPEAT: "repeat"
|
||||||
}
|
}
|
||||||
7 RESULT: "Result" => {
|
7 RESULT: "Result" => {
|
||||||
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue