mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
LIST_REVERSE works, so long as the list is made up of ints equal to 1
This commit is contained in:
parent
d8a8741aed
commit
7bd7e697b0
4 changed files with 41 additions and 6 deletions
|
@ -522,6 +522,15 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// reverse : List elem -> List elem
|
||||||
|
add_type(
|
||||||
|
Symbol::LIST_REVERSE,
|
||||||
|
SolvedType::Func(
|
||||||
|
vec![list_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,
|
||||||
|
|
|
@ -630,6 +630,28 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// reverse : Attr * (List (Attr * a)) -> Attr * (List (Attr * a))
|
||||||
|
add_type(Symbol::LIST_REVERSE, {
|
||||||
|
let_tvars! { a, star1, star2 };
|
||||||
|
|
||||||
|
unique_function(
|
||||||
|
vec![SolvedType::Apply(
|
||||||
|
Symbol::ATTR_ATTR,
|
||||||
|
vec![
|
||||||
|
flex(star1),
|
||||||
|
SolvedType::Apply(Symbol::LIST_LIST, vec![flex(a)]),
|
||||||
|
],
|
||||||
|
)],
|
||||||
|
SolvedType::Apply(
|
||||||
|
Symbol::ATTR_ATTR,
|
||||||
|
vec![
|
||||||
|
boolean(star2),
|
||||||
|
SolvedType::Apply(Symbol::LIST_LIST, vec![flex(a)]),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
// push : Attr * (List a)
|
// push : Attr * (List a)
|
||||||
// , a
|
// , a
|
||||||
// -> Attr * (List a)
|
// -> Attr * (List a)
|
||||||
|
|
|
@ -1610,18 +1610,18 @@ fn call_with_args<'a, 'ctx, 'env>(
|
||||||
// dont need to allocate memory for the index or the check
|
// dont need to allocate memory for the index or the check
|
||||||
// if list_len == 0
|
// if list_len == 0
|
||||||
let comparison = builder.build_int_compare(
|
let comparison = builder.build_int_compare(
|
||||||
IntPredicate::EQ,
|
IntPredicate::NE,
|
||||||
list_len,
|
list_len,
|
||||||
ctx.i64_type().const_int(0, false),
|
ctx.i64_type().const_int(0, false),
|
||||||
"atleastzero",
|
"atleastzero",
|
||||||
);
|
);
|
||||||
|
|
||||||
let build_then = || {
|
let build_then = || {
|
||||||
// Allocate space for the new array that we'll copy into.
|
|
||||||
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
|
||||||
|
|
||||||
match list_layout {
|
match list_layout {
|
||||||
Layout::Builtin(Builtin::List(elem_layout)) => {
|
Layout::Builtin(Builtin::List(elem_layout)) => {
|
||||||
|
// Allocate space for the new array that we'll copy into.
|
||||||
|
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
||||||
|
|
||||||
let list_ptr = {
|
let list_ptr = {
|
||||||
let len_type = env.ptr_int();
|
let len_type = env.ptr_int();
|
||||||
let len = len_type.const_int(elem_bytes, false);
|
let len = len_type.const_int(elem_bytes, false);
|
||||||
|
@ -1663,7 +1663,7 @@ fn call_with_args<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
// Mutate the new array in-place to change the element.
|
// Mutate the new array in-place to change the element.
|
||||||
// builder.build_store(elem_ptr, builder.build_load(elem_ptr, "List.get"));
|
// builder.build_store(elem_ptr, builder.build_load(elem_ptr, "List.get"));
|
||||||
builder.build_store(elem_ptr, curr_index);
|
builder.build_store(elem_ptr, ctx.i64_type().const_int(1, false));
|
||||||
|
|
||||||
// #index != 0
|
// #index != 0
|
||||||
let end_cond = builder.build_int_compare(
|
let end_cond = builder.build_int_compare(
|
||||||
|
|
|
@ -512,10 +512,14 @@ mod gen_builtins {
|
||||||
assert_evals_to!("List.repeat 5 1", &[1, 1, 1, 1, 1], &'static [i64]);
|
assert_evals_to!("List.repeat 5 1", &[1, 1, 1, 1, 1], &'static [i64]);
|
||||||
assert_evals_to!("List.repeat 4 2", &[2, 2, 2, 2], &'static [i64]);
|
assert_evals_to!("List.repeat 4 2", &[2, 2, 2, 2], &'static [i64]);
|
||||||
|
|
||||||
assert_evals_to!("List.repeat 0 []", &[], &'static [i64]);
|
|
||||||
assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]);
|
assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_reverse() {
|
||||||
|
assert_evals_to!("List.reverse [1, 2, 3]", &[3, 2, 1], &'static [i64]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_list_len() {
|
fn empty_list_len() {
|
||||||
with_larger_debug_stack(|| {
|
with_larger_debug_stack(|| {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue