Int.isOdd and Int.isEven functions in code gen

This commit is contained in:
Chad Stearns 2020-05-13 18:57:15 -04:00
parent 7df4771f7b
commit c8d62bd838
3 changed files with 64 additions and 0 deletions

View file

@ -32,9 +32,57 @@ pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
int_div(var_store),
int_abs(var_store),
int_rem(var_store),
int_is_odd(var_store),
int_is_even(var_store),
]
}
/// Int.isOdd : Int -> Bool
fn int_is_odd(var_store: &VarStore) -> Def {
use crate::expr::Expr::*;
defn(
Symbol::INT_IS_ODD,
vec![Symbol::INT_IS_ODD_ARG],
var_store,
call(
Symbol::INT_EQ_I64,
vec![
call(
Symbol::INT_REM_UNSAFE,
vec![Var(Symbol::INT_IS_ODD_ARG), Int(var_store.fresh(), 2)],
var_store,
),
Int(var_store.fresh(), 1),
],
var_store,
),
)
}
/// Int.isEven : Int -> Bool
fn int_is_even(var_store: &VarStore) -> Def {
use crate::expr::Expr::*;
defn(
Symbol::INT_IS_EVEN,
vec![Symbol::INT_IS_EVEN_ARG],
var_store,
call(
Symbol::INT_EQ_I64,
vec![
call(
Symbol::INT_REM_UNSAFE,
vec![Var(Symbol::INT_IS_EVEN_ARG), Int(var_store.fresh(), 2)],
var_store,
),
Int(var_store.fresh(), 0),
],
var_store,
),
)
}
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
fn list_get(var_store: &VarStore) -> Def {
use crate::expr::Expr::*;