Adds castToNat

This commit is contained in:
Joshua Hoeflich 2021-08-18 11:02:20 -05:00
parent 7068da7144
commit 92da003fba
8 changed files with 23 additions and 2 deletions

View File

@ -81,6 +81,7 @@ comptime {
exportNumFn(num.asin, "asin");
exportNumFn(num.bytesToU16C, "bytes_to_u16");
exportNumFn(num.bytesToU32C, "bytes_to_u32");
exportNumFn(num.castToNat, "cast_to_nat");
exportNumFn(num.round, "round");
}

View File

@ -45,6 +45,10 @@ fn bytesToU32(arg: RocList, position: usize) u32 {
return 41;
}
pub fn castToNat(num: i64) callconv(.C) usize {
return @intCast(usize, num);
}
pub fn round(num: f64) callconv(.C) i64 {
return @floatToInt(i32, (@round(num)));
}

View File

@ -10,6 +10,7 @@ pub const NUM_IS_FINITE: &str = "roc_builtins.num.is_finite";
pub const NUM_POW_INT: &str = "roc_builtins.num.pow_int";
pub const NUM_BYTES_TO_U16: &str = "roc_builtins.num.bytes_to_u16";
pub const NUM_BYTES_TO_U32: &str = "roc_builtins.num.bytes_to_u32";
pub const NUM_CAST_TO_NAT: &str = "roc_builtins.num.cast_to_nat";
pub const NUM_ROUND: &str = "roc_builtins.num.round";
pub const STR_INIT: &str = "roc_builtins.str.init";

View File

@ -501,6 +501,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
Box::new(float_type(flex(TVAR1))),
);
// castToNat : Num a -> Nat
add_top_level_function_type!(
Symbol::NUM_CAST_TO_NAT,
vec![int_type(flex(TVAR1))],
Box::new(nat_type()),
);
// bytesToU16 : List U8, Nat -> U16
add_top_level_function_type!(
Symbol::NUM_BYTES_TO_U16,

View File

@ -4738,6 +4738,11 @@ fn run_low_level<'a, 'ctx, 'env>(
}
}
}
NumCastToNat => {
debug_assert_eq!(args.len(), 1);
let num = load_symbol(scope, &args[0]).into_int_value();
call_bitcode_fn(env, &[num.into()], bitcode::NUM_CAST_TO_NAT)
}
NumBytesToU16 => {
debug_assert_eq!(args.len(), 2);
let list = load_symbol(scope, &args[0]).into_struct_value();

View File

@ -89,6 +89,7 @@ pub enum LowLevel {
NumAsin,
NumBytesToU16,
NumBytesToU32,
NumCastToNat,
NumBitwiseAnd,
NumBitwiseXor,
NumBitwiseOr,
@ -125,8 +126,8 @@ impl LowLevel {
| NumSqrtUnchecked | NumLogUnchecked | NumRound | NumToFloat | NumPow | NumCeiling
| NumPowInt | NumFloor | NumIsFinite | NumAtan | NumAcos | NumAsin | NumBitwiseAnd
| NumBitwiseXor | NumBitwiseOr | NumShiftLeftBy | NumShiftRightBy | NumBytesToU16
| NumBytesToU32 | NumShiftRightZfBy | NumIntCast | Eq | NotEq | And | Or | Not
| Hash | ExpectTrue => false,
| NumBytesToU32 | NumCastToNat | NumShiftRightZfBy | NumIntCast | Eq | NotEq | And
| Or | Not | Hash | ExpectTrue => false,
ListMap | ListMap2 | ListMap3 | ListMapWithIndex | ListKeepIf | ListWalk
| ListWalkUntil | ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith

View File

@ -893,6 +893,7 @@ define_builtins! {
102 NUM_DEC: "Dec" imported // the Num.Dectype alias
103 NUM_BYTES_TO_U16: "bytesToU16"
104 NUM_BYTES_TO_U32: "bytesToU32"
105 NUM_CAST_TO_NAT: "#castToNat"
}
2 BOOL: "Bool" => {
0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias

View File

@ -1014,6 +1014,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
| NumIntCast => arena.alloc_slice_copy(&[irrelevant]),
NumBytesToU16 => arena.alloc_slice_copy(&[borrowed, irrelevant]),
NumBytesToU32 => arena.alloc_slice_copy(&[borrowed, irrelevant]),
NumCastToNat => arena.alloc_slice_copy(&[irrelevant]),
StrStartsWith | StrEndsWith => arena.alloc_slice_copy(&[owned, borrowed]),
StrStartsWithCodePt => arena.alloc_slice_copy(&[borrowed, irrelevant]),
StrFromUtf8 => arena.alloc_slice_copy(&[owned]),