Add can builtins for List.push and List.repeat

This commit is contained in:
Richard Feldman 2020-06-29 22:25:57 -04:00
parent d7203be7ad
commit d744567f76

View File

@ -53,9 +53,11 @@ pub fn builtin_defs(var_store: &mut VarStore) -> MutMap<Symbol, Def> {
Symbol::LIST_LEN => list_len,
Symbol::LIST_GET => list_get,
Symbol::LIST_SET => list_set,
Symbol::LIST_PUSH => list_push,
Symbol::LIST_FIRST => list_first,
Symbol::LIST_IS_EMPTY => list_is_empty,
Symbol::LIST_SINGLE => list_single,
Symbol::LIST_REPEAT => list_repeat,
Symbol::NUM_ADD => num_add,
Symbol::NUM_SUB => num_sub,
Symbol::NUM_MUL => num_mul,
@ -474,6 +476,20 @@ fn list_is_empty(symbol: Symbol, var_store: &mut VarStore) -> Def {
defn(symbol, vec![Symbol::ARG_1], var_store, body)
}
/// List.repeat : elem, Int -> List elem
fn list_repeat(symbol: Symbol, var_store: &mut VarStore) -> Def {
let body = RunLowLevel {
op: LowLevel::ListRepeat,
args: vec![
(var_store.fresh(), Var(Symbol::ARG_1)),
(var_store.fresh(), Var(Symbol::ARG_2)),
],
ret_var: var_store.fresh(),
};
defn(symbol, vec![Symbol::ARG_1, Symbol::ARG_2], var_store, body)
}
/// List.single : elem -> List elem
fn list_single(symbol: Symbol, var_store: &mut VarStore) -> Def {
let body = RunLowLevel {
@ -627,6 +643,23 @@ fn list_set(symbol: Symbol, var_store: &mut VarStore) -> Def {
)
}
/// List.push : List elem, elem -> List elem
fn list_push(symbol: Symbol, var_store: &mut VarStore) -> Def {
let list_var = var_store.fresh();
let elem_var = var_store.fresh();
let body = RunLowLevel {
op: LowLevel::ListPush,
args: vec![
(list_var, Var(Symbol::ARG_1)),
(elem_var, Var(Symbol::ARG_2)),
],
ret_var: list_var,
};
defn(symbol, vec![Symbol::ARG_1, Symbol::ARG_2], var_store, body)
}
/// Num.rem : Int, Int -> Result Int [ DivByZero ]*
fn num_rem(symbol: Symbol, var_store: &mut VarStore) -> Def {
let num_var = var_store.fresh();