Add dropLast to tests and parser

This commit is contained in:
Chelsea Troy 2021-10-21 23:02:26 -05:00
parent 51de420ee7
commit 49a832d757
No known key found for this signature in database
GPG Key ID: A631885A970636C2
7 changed files with 77 additions and 0 deletions

View File

@ -934,6 +934,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
Box::new(list_type(flex(TVAR1))),
);
// dropLast : List elem -> List elem
add_top_level_function_type!(
Symbol::LIST_DROP_LAST,
vec![list_type(flex(TVAR1))],
Box::new(list_type(flex(TVAR1))),
);
// swap : List elem, Nat, Nat -> List elem
add_top_level_function_type!(
Symbol::LIST_SWAP,

View File

@ -341,6 +341,28 @@ pub fn list_drop_at<'a, 'ctx, 'env>(
)
}
/// List.dropLast : List elem -> List elem
pub fn list_drop_last<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
original_wrapper: StructValue<'ctx>,
count: IntValue<'ctx>,
element_layout: &Layout<'a>,
) -> BasicValueEnum<'ctx> {
let dec_element_fn = build_dec_wrapper(env, layout_ids, element_layout);
call_bitcode_fn_returns_list(
env,
&[
pass_list_cc(env, original_wrapper.into()),
env.alignment_intvalue(element_layout),
layout_width(env, element_layout),
count.into(),
dec_element_fn.as_global_value().as_pointer_value().into(),
],
bitcode::LIST_DROP_LAST,
)
}
/// List.set : List elem, Nat, elem -> List elem
pub fn list_set<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,

View File

@ -42,6 +42,7 @@ pub enum LowLevel {
ListSortWith,
ListDrop,
ListDropAt,
ListDropLast,
ListSwap,
DictSize,
DictEmpty,
@ -129,6 +130,7 @@ macro_rules! first_order {
| ListSet
| ListDrop
| ListDropAt
| ListDropLast
| ListSingle
| ListRepeat
| ListReverse

View File

@ -1015,6 +1015,7 @@ define_builtins! {
32 LIST_DROP: "drop"
33 LIST_SWAP: "swap"
34 LIST_DROP_AT: "dropAt"
35 LIST_DROP_LAST: "dropLast"
}
5 RESULT: "Result" => {
0 RESULT_RESULT: "Result" imported // the Result.Result type alias

View File

@ -947,6 +947,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
ListAppend => arena.alloc_slice_copy(&[owned, owned]),
ListDrop => arena.alloc_slice_copy(&[owned, irrelevant]),
ListDropAt => arena.alloc_slice_copy(&[owned, irrelevant]),
ListDropLast => arena.alloc_slice_copy(&[owned]),
ListSwap => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
Eq | NotEq => arena.alloc_slice_copy(&[borrowed, borrowed]),

View File

@ -3733,6 +3733,18 @@ mod solve_expr {
);
}
#[test]
fn list_drop_at() {
infer_eq_without_problem(
indoc!(
r#"
List.dropLast
"#
),
"List a -> List a",
);
}
#[test]
fn function_that_captures_nothing_is_not_captured() {
// we should make sure that a function that doesn't capture anything it not itself captured

View File

@ -235,6 +235,38 @@ fn list_drop_at_mutable() {
);
}
#[test]
fn list_drop_last() {
assert_evals_to!(
"List.dropLast [1, 2, 3]",
RocList::from_slice(&[1, 2]),
RocList<i64>
);
assert_evals_to!("List.dropLast []", RocList::from_slice(&[]), RocList<i64>);
assert_evals_to!("List.dropLast [0]", RocList::from_slice(&[]), RocList<i64>);
}
#[test]
fn list_drop_last_mutable() {
assert_evals_to!(
indoc!(
r#"
list : List I64
list = [ if True then 4 else 4, 5, 6 ]
{ newList: List.dropLast list, original: list }
"#
),
(
// new_list
RocList::from_slice(&[4, 5]),
// original
RocList::from_slice(&[4, 5, 6]),
),
(RocList<i64>, RocList<i64>,)
);
}
#[test]
fn list_swap() {
assert_evals_to!("List.swap [] 0 1", RocList::from_slice(&[]), RocList<i64>);