Merge branch 'trunk' into str_trim_left

This commit is contained in:
Michael Downey 2021-11-09 19:43:26 -05:00 committed by GitHub
commit 07cd3850d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
62 changed files with 3288 additions and 7733 deletions

View file

@ -1093,6 +1093,41 @@ fn call_spec(
add_loop(builder, block, state_type, init_state, loop_body)
}
ListFindUnsafe { xs } => {
let list = env.symbols[xs];
// ListFindUnsafe returns { value: v, found: Bool=Int1 }
let output_layouts = vec![arg_layouts[0], Layout::Builtin(Builtin::Int1)];
let output_layout = Layout::Struct(&output_layouts);
let output_type = layout_spec(builder, &output_layout)?;
let loop_body = |builder: &mut FuncDefBuilder, block, output| {
let bag = builder.add_get_tuple_field(block, list, LIST_BAG_INDEX)?;
let element = builder.add_bag_get(block, bag)?;
let _is_found = call_function!(builder, block, [element]);
// We may or may not use the element we got from the list in the output struct,
// depending on whether we found the element to satisfy the "find" predicate.
// If we did find the element, our output "changes" to be a record including that element.
let found_branch = builder.add_block();
let new_output =
builder.add_unknown_with(block, &[element], output_type)?;
let not_found_branch = builder.add_block();
builder.add_choice(
block,
&[
BlockExpr(found_branch, new_output),
BlockExpr(not_found_branch, output),
],
)
};
// Assume the output is initially { found: False, value: \empty }
let output_state = builder.add_unknown_with(block, &[], output_type)?;
add_loop(builder, block, output_type, output_state, loop_body)
}
}
}
}

View file

@ -618,7 +618,8 @@ impl<'a> BorrowInfState<'a> {
| ListKeepIf { xs }
| ListKeepOks { xs }
| ListKeepErrs { xs }
| ListAny { xs } => {
| ListAny { xs }
| ListFindUnsafe { xs } => {
// own the list if the function wants to own the element
if !function_ps[0].borrow {
self.own_var(*xs);
@ -960,11 +961,13 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
arena.alloc_slice_copy(&[owned, owned, function, closure_data])
}
ListSortWith => arena.alloc_slice_copy(&[owned, function, closure_data]),
ListFindUnsafe => arena.alloc_slice_copy(&[owned, function, closure_data]),
// TODO when we have lists with capacity (if ever)
// List.append should own its first argument
ListAppend => arena.alloc_slice_copy(&[owned, owned]),
ListTakeFirst => arena.alloc_slice_copy(&[owned, irrelevant]),
ListTakeLast => arena.alloc_slice_copy(&[owned, irrelevant]),
ListDrop => arena.alloc_slice_copy(&[owned, irrelevant]),
ListDropAt => arena.alloc_slice_copy(&[owned, irrelevant]),
ListSwap => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),

View file

@ -531,7 +531,8 @@ impl<'a> Context<'a> {
| ListKeepIf { xs }
| ListKeepOks { xs }
| ListKeepErrs { xs }
| ListAny { xs } => {
| ListAny { xs }
| ListFindUnsafe { xs } => {
let borrows = [function_ps[0].borrow, FUNCTION, CLOSURE_DATA];
let b = self.add_dec_after_lowlevel(arguments, &borrows, b, b_live_vars);

View file

@ -4164,6 +4164,11 @@ pub fn with_hole<'a>(
match_on_closure_argument!(ListMap4, [xs, ys, zs, ws])
}
ListFindUnsafe => {
debug_assert_eq!(arg_symbols.len(), 2);
let xs = arg_symbols[0];
match_on_closure_argument!(ListFindUnsafe, [xs])
}
_ => {
let call = self::Call {
call_type: CallType::LowLevel {

View file

@ -50,6 +50,9 @@ pub enum HigherOrder {
ListAny {
xs: Symbol,
},
ListFindUnsafe {
xs: Symbol,
},
DictWalk {
xs: Symbol,
state: Symbol,
@ -71,6 +74,7 @@ impl HigherOrder {
HigherOrder::ListKeepOks { .. } => 1,
HigherOrder::ListKeepErrs { .. } => 1,
HigherOrder::ListSortWith { .. } => 2,
HigherOrder::ListFindUnsafe { .. } => 1,
HigherOrder::DictWalk { .. } => 2,
HigherOrder::ListAny { .. } => 1,
}
@ -97,6 +101,7 @@ enum FirstOrder {
ListGetUnsafe,
ListSet,
ListTakeFirst,
ListTakeLast,
ListDrop,
ListDropAt,
ListSingle,