mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 13:59:08 +00:00
isZero, isPositive, isNegative
This commit is contained in:
parent
40cd77ec95
commit
a01bdd66c5
6 changed files with 198 additions and 87 deletions
|
@ -34,9 +34,60 @@ pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
|
|||
int_rem(var_store),
|
||||
int_is_odd(var_store),
|
||||
int_is_even(var_store),
|
||||
int_is_zero(var_store),
|
||||
int_is_positive(var_store),
|
||||
int_is_negative(var_store),
|
||||
]
|
||||
}
|
||||
|
||||
/// Int.isNegative : Int -> Bool
|
||||
fn int_is_negative(var_store: &VarStore) -> Def {
|
||||
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) -> Def {
|
||||
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) -> Def {
|
||||
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 {
|
||||
use crate::expr::Expr::*;
|
||||
|
|
|
@ -29,18 +29,24 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn f64_sqrt() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("Float.sqrt 144", 12.0, f64);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f64_round() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("Float.round 3.6", 4, i64);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn f64_abs() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("Float.abs -4.7", 4.7, f64);
|
||||
assert_evals_to!("Float.abs 5.8", 5.8, f64);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -51,7 +57,9 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn empty_list_literal() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("[]", &[], &'static [i64]);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -61,6 +69,7 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn gen_if_fn() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -78,6 +87,7 @@ mod gen_builtins {
|
|||
-1,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -95,6 +105,7 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn gen_add_f64() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -104,6 +115,7 @@ mod gen_builtins {
|
|||
6.5,
|
||||
f64
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -147,6 +159,7 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn gen_add_i64() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -156,6 +169,7 @@ mod gen_builtins {
|
|||
6,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -257,6 +271,26 @@ mod gen_builtins {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_is_zero() {
|
||||
assert_evals_to!("Int.isZero 0", true, bool);
|
||||
assert_evals_to!("Int.isZero 1", false, bool);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_is_positive() {
|
||||
assert_evals_to!("Int.isPositive 0", false, bool);
|
||||
assert_evals_to!("Int.isPositive 1", true, bool);
|
||||
assert_evals_to!("Int.isPositive -5", false, bool);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_is_negative() {
|
||||
assert_evals_to!("Int.isNegative 0", false, bool);
|
||||
assert_evals_to!("Int.isNegative 3", false, bool);
|
||||
assert_evals_to!("Int.isNegative -2", false, bool);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gen_is_odd() {
|
||||
assert_evals_to!("Int.isOdd 4", false, bool);
|
||||
|
@ -387,6 +421,7 @@ mod gen_builtins {
|
|||
}
|
||||
#[test]
|
||||
fn tail_call_elimination() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -401,6 +436,7 @@ mod gen_builtins {
|
|||
500000500000,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
#[test]
|
||||
fn int_negate() {
|
||||
|
@ -425,12 +461,16 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn empty_list_len() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("List.len []", 0, usize);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_int_list_len() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("List.len [ 12, 9, 6, 3 ]", 4, usize);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -472,11 +512,14 @@ mod gen_builtins {
|
|||
|
||||
#[test]
|
||||
fn empty_list_is_empty() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!("List.isEmpty []", true, bool);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_int_list() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -488,10 +531,12 @@ mod gen_builtins {
|
|||
12,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_empty_list() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -503,6 +548,7 @@ mod gen_builtins {
|
|||
-1,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -13,7 +13,7 @@ mod helpers;
|
|||
|
||||
#[cfg(test)]
|
||||
mod gen_primitives {
|
||||
use crate::helpers::{can_expr, infer_expr, uniq_expr, CanExprOut};
|
||||
use crate::helpers::{can_expr, infer_expr, uniq_expr, with_larger_debug_stack, CanExprOut};
|
||||
use bumpalo::Bump;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::JitFunction;
|
||||
|
@ -406,6 +406,7 @@ mod gen_primitives {
|
|||
|
||||
#[test]
|
||||
fn gen_chained_defs() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -421,9 +422,11 @@ mod gen_primitives {
|
|||
1337,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
#[test]
|
||||
fn gen_nested_defs() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -461,5 +464,6 @@ mod gen_primitives {
|
|||
1337,
|
||||
i64
|
||||
);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ mod helpers;
|
|||
|
||||
#[cfg(test)]
|
||||
mod gen_tags {
|
||||
use crate::helpers::{can_expr, infer_expr, uniq_expr, CanExprOut};
|
||||
use crate::helpers::{can_expr, infer_expr, uniq_expr, with_larger_debug_stack, CanExprOut};
|
||||
use bumpalo::Bump;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::execution_engine::JitFunction;
|
||||
|
@ -213,6 +213,7 @@ mod gen_tags {
|
|||
|
||||
#[test]
|
||||
fn even_odd() {
|
||||
with_larger_debug_stack(|| {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -234,6 +235,7 @@ mod gen_tags {
|
|||
true,
|
||||
bool
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -623,6 +623,12 @@ define_builtins! {
|
|||
29 INT_IS_ODD_ARG: "isOdd#arg"
|
||||
30 INT_IS_EVEN: "isEven"
|
||||
31 INT_IS_EVEN_ARG: "isEven#arg"
|
||||
32 INT_IS_ZERO: "isZero"
|
||||
33 INT_IS_ZERO_ARG: "isZero#arg"
|
||||
34 INT_IS_POSITIVE: "isPositive"
|
||||
35 INT_IS_POSITIVE_ARG: "isPositive#arg"
|
||||
36 INT_IS_NEGATIVE: "isNegative"
|
||||
37 INT_IS_NEGATIVE_ARG: "isNegative#arg"
|
||||
}
|
||||
3 FLOAT: "Float" => {
|
||||
0 FLOAT_FLOAT: "Float" imported // the Float.Float type alias
|
||||
|
|
|
@ -116,6 +116,7 @@ mod test_uniq_solve {
|
|||
|
||||
#[test]
|
||||
fn empty_list_literal() {
|
||||
with_larger_debug_stack(|| {
|
||||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
|
@ -124,6 +125,7 @@ mod test_uniq_solve {
|
|||
),
|
||||
"Attr * (List *)",
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue