List.mapWithIndex

This commit is contained in:
Folkert 2021-02-17 22:29:15 +01:00
parent 7bd228ca9b
commit 65ab08728a
11 changed files with 117 additions and 5 deletions

View file

@ -7,8 +7,8 @@ use crate::llvm::build_hash::generic_hash;
use crate::llvm::build_list::{
allocate_list, empty_list, empty_polymorphic_list, list_append, list_concat, list_contains,
list_get_unsafe, list_join, list_keep_errs, list_keep_if, list_keep_oks, list_len, list_map,
list_prepend, list_repeat, list_reverse, list_set, list_single, list_sum, list_walk,
list_walk_backwards,
list_map_with_index, list_prepend, list_repeat, list_reverse, list_set, list_single, list_sum,
list_walk, list_walk_backwards,
};
use crate::llvm::build_str::{
str_concat, str_count_graphemes, str_ends_with, str_from_float, str_from_int, str_join_with,
@ -3642,6 +3642,24 @@ fn run_low_level<'a, 'ctx, 'env>(
_ => unreachable!("invalid list layout"),
}
}
ListMapWithIndex => {
// List.map : List before, (before -> after) -> List after
debug_assert_eq!(args.len(), 2);
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
let (func, func_layout) = load_symbol_and_layout(scope, &args[1]);
match list_layout {
Layout::Builtin(Builtin::EmptyList) => {
return empty_list(env);
}
Layout::Builtin(Builtin::List(_, element_layout)) => {
list_map_with_index(env, layout_ids, func, func_layout, list, element_layout)
}
_ => unreachable!("invalid list layout"),
}
}
ListKeepIf => {
// List.keepIf : List elem, (elem -> Bool) -> List elem
debug_assert_eq!(args.len(), 2);

View file

@ -1169,6 +1169,49 @@ pub fn list_map<'a, 'ctx, 'env>(
transform_layout: &Layout<'a>,
list: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
) -> BasicValueEnum<'ctx> {
list_map_generic(
env,
layout_ids,
transform,
transform_layout,
list,
element_layout,
bitcode::LIST_MAP,
&[element_layout.clone()],
)
}
/// List.mapWithIndex : List before, (Nat, before -> after) -> List after
pub fn list_map_with_index<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
transform: BasicValueEnum<'ctx>,
transform_layout: &Layout<'a>,
list: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
) -> BasicValueEnum<'ctx> {
list_map_generic(
env,
layout_ids,
transform,
transform_layout,
list,
element_layout,
bitcode::LIST_MAP_WITH_INDEX,
&[Layout::Builtin(Builtin::Usize), element_layout.clone()],
)
}
fn list_map_generic<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_ids: &mut LayoutIds<'a>,
transform: BasicValueEnum<'ctx>,
transform_layout: &Layout<'a>,
list: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
op: &str,
argument_layouts: &[Layout<'a>],
) -> BasicValueEnum<'ctx> {
let builder = env.builder;
@ -1186,7 +1229,7 @@ pub fn list_map<'a, 'ctx, 'env>(
env.builder.build_store(transform_ptr, transform);
let stepper_caller =
build_transform_caller(env, layout_ids, transform_layout, &[element_layout.clone()])
build_transform_caller(env, layout_ids, transform_layout, argument_layouts)
.as_global_value()
.as_pointer_value();
@ -1212,7 +1255,7 @@ pub fn list_map<'a, 'ctx, 'env>(
old_element_width.into(),
new_element_width.into(),
],
&bitcode::LIST_MAP,
op,
);
complex_bitcast(