mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
Re-enable List.get
This commit is contained in:
parent
357cd3bd70
commit
353377c29e
1 changed files with 145 additions and 158 deletions
|
@ -26,174 +26,134 @@ use roc_types::subs::{VarStore, Variable};
|
||||||
/// lookup (if the bounds check passed). That internal function is hardcoded in code gen,
|
/// lookup (if the bounds check passed). That internal function is hardcoded in code gen,
|
||||||
/// which works fine because it doesn't involve any open tag unions.
|
/// which works fine because it doesn't involve any open tag unions.
|
||||||
pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
|
pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
|
||||||
vec![/*list_get(var_store),*/ list_first(var_store)]
|
vec![list_get(var_store), list_first(var_store)]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
|
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
|
||||||
// fn list_get(var_store: &VarStore) -> Def {
|
fn list_get(var_store: &VarStore) -> Def {
|
||||||
// use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
// use crate::pattern::Pattern::*;
|
|
||||||
|
|
||||||
// let args = vec![
|
defn(
|
||||||
// (
|
Symbol::LIST_GET,
|
||||||
// var_store.fresh(),
|
vec![Symbol::LIST_GET_ARG_LIST, Symbol::LIST_GET_ARG_INDEX],
|
||||||
// no_region(Identifier(Symbol::LIST_GET_ARG_LIST)),
|
var_store,
|
||||||
// ),
|
// Perform a bounds check. If it passes, delegate to List.#getUnsafe
|
||||||
// (
|
If {
|
||||||
// var_store.fresh(),
|
cond_var: var_store.fresh(),
|
||||||
// no_region(Identifier(Symbol::LIST_GET_ARG_INDEX)),
|
branch_var: var_store.fresh(),
|
||||||
// ),
|
branches: vec![(
|
||||||
// ];
|
// if-condition
|
||||||
|
no_region(
|
||||||
// // Perform a bounds check. If it passes, delegate to List.#getUnsafe
|
// index < List.len list
|
||||||
// let body = If {
|
call(
|
||||||
// cond_var: var_store.fresh(),
|
Symbol::NUM_LT,
|
||||||
// branch_var: var_store.fresh(),
|
vec![
|
||||||
// branches: vec![(
|
Var(Symbol::LIST_GET_ARG_INDEX),
|
||||||
// // if-condition
|
call(
|
||||||
// no_region(
|
Symbol::LIST_LEN,
|
||||||
// // index < List.len list
|
vec![Var(Symbol::LIST_GET_ARG_LIST)],
|
||||||
// call(
|
var_store,
|
||||||
// Symbol::NUM_LT,
|
),
|
||||||
// vec![
|
],
|
||||||
// Var(Symbol::LIST_GET_ARG_INDEX),
|
var_store,
|
||||||
// call(
|
),
|
||||||
// Symbol::LIST_LEN,
|
),
|
||||||
// vec![Var(Symbol::LIST_GET_ARG_LIST)],
|
// then-branch
|
||||||
// var_store,
|
no_region(
|
||||||
// ),
|
// Ok
|
||||||
// ],
|
tag(
|
||||||
// var_store,
|
"Ok",
|
||||||
// ),
|
vec![
|
||||||
// ),
|
// List.getUnsafe list index
|
||||||
// // then-branch
|
Call(
|
||||||
// no_region(
|
Box::new((
|
||||||
// // Ok
|
var_store.fresh(),
|
||||||
// tag(
|
no_region(Var(Symbol::LIST_GET_UNSAFE)),
|
||||||
// "Ok",
|
var_store.fresh(),
|
||||||
// vec![
|
)),
|
||||||
// // List.getUnsafe list index
|
vec![
|
||||||
// Call(
|
(var_store.fresh(), no_region(Var(Symbol::LIST_GET_ARG_LIST))),
|
||||||
// Box::new((
|
(
|
||||||
// var_store.fresh(),
|
var_store.fresh(),
|
||||||
// no_region(Var(Symbol::LIST_GET_UNSAFE)),
|
no_region(Var(Symbol::LIST_GET_ARG_INDEX)),
|
||||||
// var_store.fresh(),
|
),
|
||||||
// )),
|
],
|
||||||
// vec![
|
CalledVia::Space,
|
||||||
// (var_store.fresh(), no_region(Var(Symbol::LIST_GET_ARG_LIST))),
|
),
|
||||||
// (
|
],
|
||||||
// var_store.fresh(),
|
var_store,
|
||||||
// no_region(Var(Symbol::LIST_GET_ARG_INDEX)),
|
),
|
||||||
// ),
|
),
|
||||||
// ],
|
)],
|
||||||
// CalledVia::Space,
|
final_else: Box::new(
|
||||||
// ),
|
// else-branch
|
||||||
// ],
|
no_region(
|
||||||
// var_store,
|
// Err
|
||||||
// ),
|
tag(
|
||||||
// ),
|
"Err",
|
||||||
// )],
|
vec![tag("OutOfBounds", Vec::new(), var_store)],
|
||||||
// final_else: Box::new(
|
var_store,
|
||||||
// // else-branch
|
),
|
||||||
// no_region(
|
),
|
||||||
// // Err
|
),
|
||||||
// tag(
|
},
|
||||||
// "Err",
|
)
|
||||||
// vec![tag("OutOfBounds", Vec::new(), var_store)],
|
}
|
||||||
// var_store,
|
|
||||||
// ),
|
|
||||||
// ),
|
|
||||||
// ),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// let expr = Closure(
|
|
||||||
// var_store.fresh(),
|
|
||||||
// Symbol::LIST_GET,
|
|
||||||
// Recursive::NotRecursive,
|
|
||||||
// args,
|
|
||||||
// Box::new((no_region(body), var_store.fresh())),
|
|
||||||
// );
|
|
||||||
|
|
||||||
// Def {
|
|
||||||
// loc_pattern: no_region(Identifier(Symbol::LIST_GET)),
|
|
||||||
// loc_expr: no_region(expr),
|
|
||||||
// expr_var: var_store.fresh(),
|
|
||||||
// pattern_vars: SendMap::default(),
|
|
||||||
// annotation: None,
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// List.first : List elem -> Result elem [ ListWasEmpty ]*
|
/// List.first : List elem -> Result elem [ ListWasEmpty ]*
|
||||||
fn list_first(var_store: &VarStore) -> Def {
|
fn list_first(var_store: &VarStore) -> Def {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
use crate::pattern::Pattern::*;
|
|
||||||
|
|
||||||
let args = vec![(
|
defn(
|
||||||
var_store.fresh(),
|
|
||||||
no_region(Identifier(Symbol::LIST_FIRST_ARG)),
|
|
||||||
)];
|
|
||||||
|
|
||||||
// Perform a bounds check. If it passes, delegate to List.getUnsafe.
|
|
||||||
let body = If {
|
|
||||||
// TODO Use "when" instead of "if" so that we can have False be the first branch.
|
|
||||||
// We want that for branch prediction; usually we expect the list to be nonempty.
|
|
||||||
cond_var: var_store.fresh(),
|
|
||||||
branch_var: var_store.fresh(),
|
|
||||||
branches: vec![(
|
|
||||||
// if-condition
|
|
||||||
no_region(
|
|
||||||
// List.isEmpty list
|
|
||||||
call(
|
|
||||||
Symbol::LIST_IS_EMPTY,
|
|
||||||
vec![Var(Symbol::LIST_FIRST_ARG)],
|
|
||||||
var_store,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
// list was empty
|
|
||||||
no_region(
|
|
||||||
// Err ListWasEmpty
|
|
||||||
tag(
|
|
||||||
"Err",
|
|
||||||
vec![tag("ListWasEmpty", Vec::new(), var_store)],
|
|
||||||
var_store,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)],
|
|
||||||
final_else: Box::new(
|
|
||||||
// list was not empty
|
|
||||||
no_region(
|
|
||||||
// Ok (List.#getUnsafe list 0)
|
|
||||||
tag(
|
|
||||||
"Ok",
|
|
||||||
vec![
|
|
||||||
// List.#getUnsafe list 0
|
|
||||||
call(
|
|
||||||
Symbol::LIST_GET_UNSAFE,
|
|
||||||
vec![(Var(Symbol::LIST_FIRST_ARG)), (Int(var_store.fresh(), 0))],
|
|
||||||
var_store,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
var_store,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
let expr = Closure(
|
|
||||||
var_store.fresh(),
|
|
||||||
Symbol::LIST_FIRST,
|
Symbol::LIST_FIRST,
|
||||||
Recursive::NotRecursive,
|
vec![Symbol::LIST_FIRST_ARG],
|
||||||
args,
|
var_store,
|
||||||
Box::new((no_region(body), var_store.fresh())),
|
// Perform a bounds check. If it passes, delegate to List.getUnsafe.
|
||||||
);
|
If {
|
||||||
|
// TODO Use "when" instead of "if" so that we can have False be the first branch.
|
||||||
Def {
|
// We want that for branch prediction; usually we expect the list to be nonempty.
|
||||||
loc_pattern: no_region(Identifier(Symbol::LIST_FIRST)),
|
cond_var: var_store.fresh(),
|
||||||
loc_expr: no_region(expr),
|
branch_var: var_store.fresh(),
|
||||||
expr_var: var_store.fresh(),
|
branches: vec![(
|
||||||
pattern_vars: SendMap::default(),
|
// if-condition
|
||||||
annotation: None,
|
no_region(
|
||||||
}
|
// List.isEmpty list
|
||||||
|
call(
|
||||||
|
Symbol::LIST_IS_EMPTY,
|
||||||
|
vec![Var(Symbol::LIST_FIRST_ARG)],
|
||||||
|
var_store,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
// list was empty
|
||||||
|
no_region(
|
||||||
|
// Err ListWasEmpty
|
||||||
|
tag(
|
||||||
|
"Err",
|
||||||
|
vec![tag("ListWasEmpty", Vec::new(), var_store)],
|
||||||
|
var_store,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)],
|
||||||
|
final_else: Box::new(
|
||||||
|
// list was not empty
|
||||||
|
no_region(
|
||||||
|
// Ok (List.#getUnsafe list 0)
|
||||||
|
tag(
|
||||||
|
"Ok",
|
||||||
|
vec![
|
||||||
|
// List.#getUnsafe list 0
|
||||||
|
call(
|
||||||
|
Symbol::LIST_GET_UNSAFE,
|
||||||
|
vec![(Var(Symbol::LIST_FIRST_ARG)), (Int(var_store.fresh(), 0))],
|
||||||
|
var_store,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
var_store,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -231,3 +191,30 @@ fn call(symbol: Symbol, args: Vec<Expr>, var_store: &VarStore) -> Expr {
|
||||||
CalledVia::Space,
|
CalledVia::Space,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) -> Def {
|
||||||
|
use crate::expr::Expr::*;
|
||||||
|
use crate::pattern::Pattern::*;
|
||||||
|
|
||||||
|
let closure_args = args
|
||||||
|
.into_iter()
|
||||||
|
.map(|symbol| (var_store.fresh(), no_region(Identifier(symbol))))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expr = Closure(
|
||||||
|
var_store.fresh(),
|
||||||
|
fn_name,
|
||||||
|
Recursive::NotRecursive,
|
||||||
|
closure_args,
|
||||||
|
Box::new((no_region(body), var_store.fresh())),
|
||||||
|
);
|
||||||
|
|
||||||
|
Def {
|
||||||
|
loc_pattern: no_region(Identifier(fn_name)),
|
||||||
|
loc_expr: no_region(expr),
|
||||||
|
expr_var: var_store.fresh(),
|
||||||
|
pattern_vars: SendMap::default(),
|
||||||
|
annotation: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue