mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
Merge remote-tracking branch 'origin/trunk' into specialize-separately
This commit is contained in:
commit
245a9fc951
40 changed files with 2118 additions and 1621 deletions
|
@ -1,6 +1,5 @@
|
|||
use crate::def::Def;
|
||||
use crate::expr::{Expr, Recursive};
|
||||
use roc_collections::all::{MutMap, SendMap};
|
||||
use roc_collections::all::MutMap;
|
||||
use roc_module::ident::TagName;
|
||||
use roc_module::low_level::LowLevel;
|
||||
use roc_module::operator::CalledVia;
|
||||
|
@ -25,7 +24,7 @@ use roc_types::subs::{VarStore, Variable};
|
|||
/// 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,
|
||||
/// which works fine because it doesn't involve any open tag unions.
|
||||
pub fn builtin_defs(var_store: &VarStore) -> MutMap<Symbol, Def> {
|
||||
pub fn builtin_defs(var_store: &VarStore) -> MutMap<Symbol, Expr> {
|
||||
mut_map! {
|
||||
Symbol::LIST_LEN => list_len(var_store),
|
||||
Symbol::LIST_GET => list_get(var_store),
|
||||
|
@ -34,12 +33,151 @@ pub fn builtin_defs(var_store: &VarStore) -> MutMap<Symbol, Def> {
|
|||
Symbol::INT_ABS => int_abs(var_store),
|
||||
Symbol::INT_REM => int_rem(var_store),
|
||||
Symbol::INT_IS_ODD => int_is_odd(var_store),
|
||||
Symbol::INT_IS_EVEN => int_is_even(var_store)
|
||||
Symbol::INT_IS_EVEN => int_is_even(var_store),
|
||||
Symbol::INT_IS_ZERO => int_is_zero(var_store),
|
||||
Symbol::INT_IS_POSITIVE => int_is_positive(var_store),
|
||||
Symbol::INT_IS_NEGATIVE => int_is_negative(var_store),
|
||||
Symbol::FLOAT_IS_POSITIVE => float_is_positive(var_store),
|
||||
Symbol::FLOAT_IS_NEGATIVE => float_is_negative(var_store),
|
||||
Symbol::FLOAT_IS_ZERO => float_is_zero(var_store),
|
||||
Symbol::FLOAT_TAN => float_tan(var_store),
|
||||
}
|
||||
}
|
||||
|
||||
/// Float.tan : Float -> Float
|
||||
fn float_tan(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::FLOAT_TAN,
|
||||
vec![Symbol::FLOAT_TAN_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::FLOAT_DIV,
|
||||
vec![
|
||||
call(
|
||||
Symbol::FLOAT_SIN,
|
||||
vec![Var(Symbol::FLOAT_TAN_ARG)],
|
||||
var_store,
|
||||
),
|
||||
call(
|
||||
Symbol::FLOAT_COS,
|
||||
vec![Var(Symbol::FLOAT_TAN_ARG)],
|
||||
var_store,
|
||||
),
|
||||
],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Float.isZero : Float -> Bool
|
||||
fn float_is_zero(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::FLOAT_IS_ZERO,
|
||||
vec![Symbol::FLOAT_IS_ZERO_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::FLOAT_EQ,
|
||||
vec![
|
||||
Float(var_store.fresh(), 0.0),
|
||||
Var(Symbol::FLOAT_IS_ZERO_ARG),
|
||||
],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Float.isNegative : Float -> Bool
|
||||
fn float_is_negative(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::FLOAT_IS_NEGATIVE,
|
||||
vec![Symbol::FLOAT_IS_NEGATIVE_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::FLOAT_GT,
|
||||
vec![
|
||||
Float(var_store.fresh(), 0.0),
|
||||
Var(Symbol::FLOAT_IS_NEGATIVE_ARG),
|
||||
],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Float.isPositive : Float -> Bool
|
||||
fn float_is_positive(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::FLOAT_IS_POSITIVE,
|
||||
vec![Symbol::FLOAT_IS_POSITIVE_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::FLOAT_GT,
|
||||
vec![
|
||||
Var(Symbol::FLOAT_IS_POSITIVE_ARG),
|
||||
Float(var_store.fresh(), 0.0),
|
||||
],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Int.isNegative : Int -> Bool
|
||||
fn int_is_negative(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::INT_IS_NEGATIVE,
|
||||
vec![Symbol::INT_IS_NEGATIVE_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::NUM_LT,
|
||||
vec![Var(Symbol::INT_IS_NEGATIVE_ARG), Int(var_store.fresh(), 0)],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Int.isPositive : Int -> Bool
|
||||
fn int_is_positive(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::INT_IS_POSITIVE,
|
||||
vec![Symbol::INT_IS_POSITIVE_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::NUM_GT,
|
||||
vec![Var(Symbol::INT_IS_POSITIVE_ARG), Int(var_store.fresh(), 0)],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Int.isZero : Int -> Bool
|
||||
fn int_is_zero(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
Symbol::INT_IS_ZERO,
|
||||
vec![Symbol::INT_IS_ZERO_ARG],
|
||||
var_store,
|
||||
call(
|
||||
Symbol::INT_EQ_I64,
|
||||
vec![Var(Symbol::INT_IS_ZERO_ARG), Int(var_store.fresh(), 0)],
|
||||
var_store,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Int.isOdd : Int -> Bool
|
||||
fn int_is_odd(var_store: &VarStore) -> Def {
|
||||
fn int_is_odd(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -62,7 +200,7 @@ fn int_is_odd(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// Int.isEven : Int -> Bool
|
||||
fn int_is_even(var_store: &VarStore) -> Def {
|
||||
fn int_is_even(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -85,7 +223,7 @@ fn int_is_even(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// List.len : List * -> Int
|
||||
fn list_len(var_store: &VarStore) -> Def {
|
||||
fn list_len(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
// Polymorphic wrapper around LowLevel::ListLen
|
||||
|
@ -100,7 +238,7 @@ fn list_len(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
|
||||
fn list_get(var_store: &VarStore) -> Def {
|
||||
fn list_get(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -169,7 +307,7 @@ fn list_get(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// Int.rem : Int, Int -> Int
|
||||
fn int_rem(var_store: &VarStore) -> Def {
|
||||
fn int_rem(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -216,7 +354,7 @@ fn int_rem(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// Int.abs : Int -> Int
|
||||
fn int_abs(var_store: &VarStore) -> Def {
|
||||
fn int_abs(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -253,7 +391,7 @@ fn int_abs(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// Int.div : Int, Int -> Result Int [ DivByZero ]*
|
||||
fn int_div(var_store: &VarStore) -> Def {
|
||||
fn int_div(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -312,7 +450,7 @@ fn int_div(var_store: &VarStore) -> Def {
|
|||
}
|
||||
|
||||
/// List.first : List elem -> Result elem [ ListWasEmpty ]*
|
||||
fn list_first(var_store: &VarStore) -> Def {
|
||||
fn list_first(var_store: &VarStore) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
|
||||
defn(
|
||||
|
@ -404,7 +542,7 @@ fn call(symbol: Symbol, args: Vec<Expr>, var_store: &VarStore) -> Expr {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) -> Def {
|
||||
fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) -> Expr {
|
||||
use crate::expr::Expr::*;
|
||||
use crate::pattern::Pattern::*;
|
||||
|
||||
|
@ -413,19 +551,11 @@ fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) ->
|
|||
.map(|symbol| (var_store.fresh(), no_region(Identifier(symbol))))
|
||||
.collect();
|
||||
|
||||
let expr = Closure(
|
||||
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