-
-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds List.last #816
Adds List.last #816
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def> | |
LIST_SET => list_set, | ||
LIST_APPEND => list_append, | ||
LIST_FIRST => list_first, | ||
LIST_LAST => list_last, | ||
LIST_IS_EMPTY => list_is_empty, | ||
LIST_SINGLE => list_single, | ||
LIST_REPEAT => list_repeat, | ||
|
@@ -152,6 +153,7 @@ pub fn builtin_defs(var_store: &mut VarStore) -> MutMap<Symbol, Def> { | |
Symbol::LIST_SET => list_set, | ||
Symbol::LIST_APPEND => list_append, | ||
Symbol::LIST_FIRST => list_first, | ||
Symbol::LIST_LAST => list_last, | ||
Symbol::LIST_IS_EMPTY => list_is_empty, | ||
Symbol::LIST_SINGLE => list_single, | ||
Symbol::LIST_REPEAT => list_repeat, | ||
|
@@ -1897,6 +1899,105 @@ fn list_first(symbol: Symbol, var_store: &mut VarStore) -> Def { | |
) | ||
} | ||
|
||
/// List.last : List elem -> Result elem [ ListWasEmpty ]* | ||
/// | ||
/// List.last : | ||
/// Attr (* | u) (List (Attr u a)), | ||
/// -> Attr * (Result (Attr u a) (Attr * [ OutOfBounds ]*)) | ||
fn list_last(symbol: Symbol, var_store: &mut VarStore) -> Def { | ||
let arg_var = var_store.fresh(); | ||
let bool_var = var_store.fresh(); | ||
let list_var = var_store.fresh(); | ||
let len_var = var_store.fresh(); | ||
let num_var = var_store.fresh(); | ||
let list_elem_var = var_store.fresh(); | ||
let ret_var = var_store.fresh(); | ||
|
||
// Perform a bounds check. If it passes, delegate to List.getUnsafe. | ||
let body = If { | ||
cond_var: bool_var, | ||
branch_var: var_store.fresh(), | ||
branches: vec![( | ||
// if-condition | ||
no_region( | ||
// List.len list != 0 | ||
RunLowLevel { | ||
op: LowLevel::NotEq, | ||
args: vec![ | ||
(len_var, Int(num_var, 0)), | ||
( | ||
len_var, | ||
RunLowLevel { | ||
op: LowLevel::ListLen, | ||
args: vec![(list_var, Var(Symbol::ARG_1))], | ||
ret_var: len_var, | ||
}, | ||
), | ||
], | ||
ret_var: bool_var, | ||
}, | ||
), | ||
// list was not empty | ||
no_region( | ||
// Ok (List.getUnsafe list (Num.sub (List.len list) 1)) | ||
tag( | ||
"Ok", | ||
vec![ | ||
// List.getUnsafe list (Num.sub (List.len list) 1) | ||
RunLowLevel { | ||
op: LowLevel::ListGetUnsafe, | ||
args: vec![ | ||
(list_var, Var(Symbol::ARG_1)), | ||
( | ||
len_var, | ||
// Num.sub (List.len list) 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's cool There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the right approach for now, but actually we should use (I say "should do" because currently only |
||
RunLowLevel { | ||
op: LowLevel::NumSub, | ||
args: vec![ | ||
( | ||
arg_var, | ||
// List.len list | ||
RunLowLevel { | ||
op: LowLevel::ListLen, | ||
args: vec![(list_var, Var(Symbol::ARG_1))], | ||
ret_var: len_var, | ||
}, | ||
), | ||
(arg_var, Int(num_var, 1)), | ||
], | ||
ret_var: len_var, | ||
}, | ||
), | ||
], | ||
ret_var: list_elem_var, | ||
}, | ||
], | ||
var_store, | ||
), | ||
), | ||
)], | ||
final_else: Box::new( | ||
// list was empty | ||
no_region( | ||
// Err ListWasEmpty | ||
tag( | ||
"Err", | ||
vec![tag("ListWasEmpty", Vec::new(), var_store)], | ||
var_store, | ||
), | ||
), | ||
), | ||
}; | ||
|
||
defn( | ||
symbol, | ||
vec![(list_var, Symbol::ARG_1)], | ||
var_store, | ||
body, | ||
ret_var, | ||
) | ||
} | ||
|
||
#[inline(always)] | ||
fn no_region<T>(value: T) -> Located<T> { | ||
Located { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure enough! I've filed a bug for this: #818