mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 06:44:46 +00:00
Merge pull request #2360 from rtfeldman/builtins-using-builtins
using builtins from builtins
This commit is contained in:
commit
eacbb956cf
3 changed files with 85 additions and 79 deletions
|
@ -44,6 +44,22 @@ macro_rules! macro_magic {
|
||||||
/// delegates to the compiler-internal List.getUnsafe function to do the actual
|
/// delegates to the compiler-internal List.getUnsafe function to do the actual
|
||||||
/// 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.
|
||||||
|
|
||||||
|
/// Does a builtin depend on any other builtins?
|
||||||
|
///
|
||||||
|
/// NOTE: you are supposed to give all symbols that are relied on,
|
||||||
|
/// even those that are relied on transitively!
|
||||||
|
pub fn builtin_dependencies(symbol: Symbol) -> &'static [Symbol] {
|
||||||
|
match symbol {
|
||||||
|
// Symbol::LIST_SORT_ASC => &[Symbol::LIST_SORT_WITH, Symbol::NUM_COMPARE],
|
||||||
|
Symbol::LIST_PRODUCT => &[Symbol::LIST_WALK, Symbol::NUM_MUL],
|
||||||
|
Symbol::LIST_SUM => &[Symbol::LIST_WALK, Symbol::NUM_ADD],
|
||||||
|
Symbol::LIST_JOIN_MAP => &[Symbol::LIST_WALK, Symbol::LIST_CONCAT],
|
||||||
|
_ => &[],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implementation for a builtin
|
||||||
pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def> {
|
pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def> {
|
||||||
debug_assert!(symbol.is_builtin());
|
debug_assert!(symbol.is_builtin());
|
||||||
|
|
||||||
|
@ -3273,15 +3289,22 @@ fn list_sum(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
let list_var = var_store.fresh();
|
let list_var = var_store.fresh();
|
||||||
let closure_var = var_store.fresh();
|
let closure_var = var_store.fresh();
|
||||||
|
|
||||||
let body = RunLowLevel {
|
let function = (
|
||||||
op: LowLevel::ListWalk,
|
var_store.fresh(),
|
||||||
args: vec![
|
Loc::at_zero(Expr::Var(Symbol::LIST_WALK)),
|
||||||
(list_var, Var(Symbol::ARG_1)),
|
var_store.fresh(),
|
||||||
(num_var, num(var_store.fresh(), 0)),
|
|
||||||
(closure_var, list_sum_add(num_var, var_store)),
|
|
||||||
],
|
|
||||||
ret_var,
|
ret_var,
|
||||||
};
|
);
|
||||||
|
|
||||||
|
let body = Expr::Call(
|
||||||
|
Box::new(function),
|
||||||
|
vec![
|
||||||
|
(list_var, Loc::at_zero(Var(Symbol::ARG_1))),
|
||||||
|
(num_var, Loc::at_zero(num(var_store.fresh(), 0))),
|
||||||
|
(closure_var, Loc::at_zero(Var(Symbol::NUM_ADD))),
|
||||||
|
],
|
||||||
|
CalledVia::Space,
|
||||||
|
);
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
symbol,
|
symbol,
|
||||||
|
@ -3292,60 +3315,34 @@ fn list_sum(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_sum_add(num_var: Variable, var_store: &mut VarStore) -> Expr {
|
|
||||||
let body = RunLowLevel {
|
|
||||||
op: LowLevel::NumAdd,
|
|
||||||
args: vec![(num_var, Var(Symbol::ARG_3)), (num_var, Var(Symbol::ARG_4))],
|
|
||||||
ret_var: num_var,
|
|
||||||
};
|
|
||||||
|
|
||||||
defn_help(
|
|
||||||
Symbol::LIST_SUM_ADD,
|
|
||||||
vec![(num_var, Symbol::ARG_3), (num_var, Symbol::ARG_4)],
|
|
||||||
var_store,
|
|
||||||
body,
|
|
||||||
num_var,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// List.product : List (Num a) -> Num a
|
/// List.product : List (Num a) -> Num a
|
||||||
fn list_product(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
fn list_product(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
let num_var = var_store.fresh();
|
let num_var = var_store.fresh();
|
||||||
let ret_var = num_var;
|
|
||||||
let list_var = var_store.fresh();
|
let list_var = var_store.fresh();
|
||||||
let closure_var = var_store.fresh();
|
let closure_var = var_store.fresh();
|
||||||
|
|
||||||
let body = RunLowLevel {
|
let function = (
|
||||||
op: LowLevel::ListWalk,
|
var_store.fresh(),
|
||||||
args: vec![
|
Loc::at_zero(Expr::Var(Symbol::LIST_WALK)),
|
||||||
(list_var, Var(Symbol::ARG_1)),
|
var_store.fresh(),
|
||||||
(num_var, num(var_store.fresh(), 1)),
|
num_var,
|
||||||
(closure_var, list_product_mul(num_var, var_store)),
|
);
|
||||||
|
|
||||||
|
let body = Expr::Call(
|
||||||
|
Box::new(function),
|
||||||
|
vec![
|
||||||
|
(list_var, Loc::at_zero(Var(Symbol::ARG_1))),
|
||||||
|
(num_var, Loc::at_zero(num(var_store.fresh(), 1))),
|
||||||
|
(closure_var, Loc::at_zero(Var(Symbol::NUM_MUL))),
|
||||||
],
|
],
|
||||||
ret_var,
|
CalledVia::Space,
|
||||||
};
|
);
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
symbol,
|
symbol,
|
||||||
vec![(list_var, Symbol::ARG_1)],
|
vec![(list_var, Symbol::ARG_1)],
|
||||||
var_store,
|
var_store,
|
||||||
body,
|
body,
|
||||||
ret_var,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn list_product_mul(num_var: Variable, var_store: &mut VarStore) -> Expr {
|
|
||||||
let body = RunLowLevel {
|
|
||||||
op: LowLevel::NumMul,
|
|
||||||
args: vec![(num_var, Var(Symbol::ARG_3)), (num_var, Var(Symbol::ARG_4))],
|
|
||||||
ret_var: num_var,
|
|
||||||
};
|
|
||||||
|
|
||||||
defn_help(
|
|
||||||
Symbol::LIST_PRODUCT_MUL,
|
|
||||||
vec![(num_var, Symbol::ARG_3), (num_var, Symbol::ARG_4)],
|
|
||||||
var_store,
|
|
||||||
body,
|
|
||||||
num_var,
|
num_var,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,6 +181,17 @@ where
|
||||||
references.insert(*symbol);
|
references.insert(*symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add any builtins used by other builtins
|
||||||
|
let transitive_builtins: Vec<Symbol> = references
|
||||||
|
.iter()
|
||||||
|
.filter(|s| s.is_builtin())
|
||||||
|
.map(|s| crate::builtins::builtin_dependencies(*s))
|
||||||
|
.flatten()
|
||||||
|
.copied()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
references.extend(transitive_builtins);
|
||||||
|
|
||||||
// NOTE previously we inserted builtin defs into the list of defs here
|
// NOTE previously we inserted builtin defs into the list of defs here
|
||||||
// this is now done later, in file.rs.
|
// this is now done later, in file.rs.
|
||||||
|
|
||||||
|
|
|
@ -1082,36 +1082,34 @@ define_builtins! {
|
||||||
24 LIST_MAP2: "map2"
|
24 LIST_MAP2: "map2"
|
||||||
25 LIST_MAP3: "map3"
|
25 LIST_MAP3: "map3"
|
||||||
26 LIST_PRODUCT: "product"
|
26 LIST_PRODUCT: "product"
|
||||||
27 LIST_SUM_ADD: "#sumadd"
|
27 LIST_WALK_UNTIL: "walkUntil"
|
||||||
28 LIST_PRODUCT_MUL: "#productmul"
|
28 LIST_RANGE: "range"
|
||||||
29 LIST_WALK_UNTIL: "walkUntil"
|
29 LIST_SORT_WITH: "sortWith"
|
||||||
30 LIST_RANGE: "range"
|
30 LIST_DROP: "drop"
|
||||||
31 LIST_SORT_WITH: "sortWith"
|
31 LIST_SWAP: "swap"
|
||||||
32 LIST_DROP: "drop"
|
32 LIST_DROP_AT: "dropAt"
|
||||||
33 LIST_SWAP: "swap"
|
33 LIST_DROP_LAST: "dropLast"
|
||||||
34 LIST_DROP_AT: "dropAt"
|
34 LIST_MIN: "min"
|
||||||
35 LIST_DROP_LAST: "dropLast"
|
35 LIST_MIN_LT: "#minlt"
|
||||||
36 LIST_MIN: "min"
|
36 LIST_MAX: "max"
|
||||||
37 LIST_MIN_LT: "#minlt"
|
37 LIST_MAX_GT: "#maxGt"
|
||||||
38 LIST_MAX: "max"
|
38 LIST_MAP4: "map4"
|
||||||
39 LIST_MAX_GT: "#maxGt"
|
39 LIST_DROP_FIRST: "dropFirst"
|
||||||
40 LIST_MAP4: "map4"
|
40 LIST_JOIN_MAP: "joinMap"
|
||||||
41 LIST_DROP_FIRST: "dropFirst"
|
41 LIST_JOIN_MAP_CONCAT: "#joinMapConcat"
|
||||||
42 LIST_JOIN_MAP: "joinMap"
|
42 LIST_ANY: "any"
|
||||||
43 LIST_JOIN_MAP_CONCAT: "#joinMapConcat"
|
43 LIST_TAKE_FIRST: "takeFirst"
|
||||||
44 LIST_ANY: "any"
|
44 LIST_TAKE_LAST: "takeLast"
|
||||||
45 LIST_TAKE_FIRST: "takeFirst"
|
45 LIST_FIND: "find"
|
||||||
46 LIST_TAKE_LAST: "takeLast"
|
46 LIST_FIND_RESULT: "#find_result" // symbol used in the definition of List.find
|
||||||
47 LIST_FIND: "find"
|
47 LIST_SUBLIST: "sublist"
|
||||||
48 LIST_FIND_RESULT: "#find_result" // symbol used in the definition of List.find
|
48 LIST_INTERSPERSE: "intersperse"
|
||||||
49 LIST_SUBLIST: "sublist"
|
49 LIST_INTERSPERSE_CLOS: "#intersperseClos"
|
||||||
50 LIST_INTERSPERSE: "intersperse"
|
50 LIST_SPLIT: "split"
|
||||||
51 LIST_INTERSPERSE_CLOS: "#intersperseClos"
|
51 LIST_SPLIT_CLOS: "#splitClos"
|
||||||
52 LIST_SPLIT: "split"
|
52 LIST_ALL: "all"
|
||||||
53 LIST_SPLIT_CLOS: "#splitClos"
|
53 LIST_DROP_IF: "dropIf"
|
||||||
54 LIST_ALL: "all"
|
54 LIST_DROP_IF_PREDICATE: "#dropIfPred"
|
||||||
55 LIST_DROP_IF: "dropIf"
|
|
||||||
56 LIST_DROP_IF_PREDICATE: "#dropIfPred"
|
|
||||||
}
|
}
|
||||||
5 RESULT: "Result" => {
|
5 RESULT: "Result" => {
|
||||||
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue