This commit is contained in:
Chad Stearns 2020-05-09 17:56:10 -04:00
parent 41af22a2ac
commit 1d1f620090
7 changed files with 80 additions and 7 deletions

View file

@ -30,6 +30,7 @@ pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> {
list_get(var_store),
list_first(var_store),
int_div(var_store),
int_abs(var_store),
]
}
@ -104,6 +105,59 @@ fn list_get(var_store: &VarStore) -> Def {
)
}
/// Int.abs : Int -> Int
fn int_abs(var_store: &VarStore) -> Def {
use crate::expr::Expr::*;
use crate::pattern::Pattern::*;
let args = vec![(
var_store.fresh(),
no_region(Identifier(Symbol::INT_ABS_ARG)),
)];
let body = If {
branch_var: var_store.fresh(),
cond_var: var_store.fresh(),
branches: vec![(
// if-condition
no_region(
// Int.isLt
call(
Symbol::INT_LT,
vec![Int(var_store.fresh(), 0), Var(Symbol::INT_ABS_ARG)],
var_store,
),
),
// int is at least 0, so just pass it along
no_region(Var(Symbol::INT_ABS_ARG)),
)],
final_else: Box::new(
// int is below 0, so multiply it by -1
no_region(call(
Symbol::NUM_MUL,
vec![Var(Symbol::INT_ABS_ARG), Int(var_store.fresh(), -1)],
var_store,
)),
),
};
let expr = Closure(
var_store.fresh(),
Symbol::INT_ABS,
Recursive::NotRecursive,
args,
Box::new((no_region(body), var_store.fresh())),
);
Def {
loc_pattern: no_region(Identifier(Symbol::INT_ABS)),
loc_expr: no_region(expr),
expr_var: var_store.fresh(),
pattern_vars: SendMap::default(),
annotation: None,
}
}
/// Int.div : Int, Int -> Result Int [ DivByZero ]*
fn int_div(var_store: &VarStore) -> Def {
use crate::expr::Expr::*;
@ -146,8 +200,8 @@ fn int_div(var_store: &VarStore) -> Def {
call(
Symbol::INT_DIV_UNSAFE,
vec![
(Var(Symbol::INT_DIV_ARG_NUMERATOR)),
(Var(Symbol::INT_DIV_ARG_DENOMINATOR)),
Var(Symbol::INT_DIV_ARG_NUMERATOR),
Var(Symbol::INT_DIV_ARG_DENOMINATOR),
],
var_store,
),