TODO: Actually implement the functions

This commit is contained in:
Joshua Hoeflich 2021-08-14 19:28:13 -05:00
parent 9dad304e95
commit 22e781259d
10 changed files with 84 additions and 10 deletions

View file

@ -80,6 +80,7 @@ comptime {
exportNumFn(num.acos, "acos");
exportNumFn(num.asin, "asin");
exportNumFn(num.bytesToU16C, "bytes_to_u16");
exportNumFn(num.bytesToU32C, "bytes_to_u32");
}
// Str Module

View file

@ -23,8 +23,8 @@ pub fn asin(num: f64) callconv(.C) f64 {
return @call(.{ .modifier = always_inline }, math.asin, .{num});
}
/// TODO: Obviously, this should not be an alias for x + 1.
/// fix me!
/// TODO: Obviously, this function should do something more interesting
/// than return the number 40. Fix me!
pub fn bytesToU16C(arg: RocList, position: usize) callconv(.C) u16 {
return @call(.{ .modifier = always_inline }, bytesToU16, .{arg, position});
}
@ -33,3 +33,14 @@ fn bytesToU16(arg: RocList, position: usize) u16 {
const exampleAnswer: u16 = 40;
return 40;
}
/// TODO: Obviously, this function should do something more interesting
/// than return the number 40. Fix me!
pub fn bytesToU32C(arg: RocList, position: usize) callconv(.C) u32 {
return @call(.{ .modifier = always_inline }, bytesToU32, .{arg, position});
}
fn bytesToU32(arg: RocList, position: usize) u32 {
const exampleAnswer: u32 = 41;
return 41;
}

View file

@ -9,7 +9,7 @@ pub const NUM_ATAN: &str = "roc_builtins.num.atan";
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 STR_INIT: &str = "roc_builtins.str.init";
pub const STR_COUNT_SEGMENTS: &str = "roc_builtins.str.count_segments";

View file

@ -508,6 +508,13 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
Box::new(u16_type()),
);
// bytesToU32 : List U8, Nat -> U32
add_top_level_function_type!(
Symbol::NUM_BYTES_TO_U32,
vec![list_type(u8_type()), nat_type()],
Box::new(u32_type()),
);
// Bool module
// and : Bool, Bool -> Bool

View file

@ -162,6 +162,7 @@ pub fn builtin_defs_map(symbol: Symbol, var_store: &mut VarStore) -> Option<Def>
NUM_ACOS => num_acos,
NUM_ASIN => num_asin,
NUM_BYTES_TO_U16 => num_bytes_to_u16,
NUM_BYTES_TO_U32 => num_bytes_to_u32,
NUM_MAX_INT => num_max_int,
NUM_MIN_INT => num_min_int,
NUM_BITWISE_AND => num_bitwise_and,
@ -1089,7 +1090,7 @@ fn num_asin(symbol: Symbol, var_store: &mut VarStore) -> Def {
)
}
/// Num.bytesToU16 : List U8, Nat -> Nat
/// Num.bytesToU16 : List U8, Nat -> U16
fn num_bytes_to_u16(symbol: Symbol, var_store: &mut VarStore) -> Def {
let list_var = var_store.fresh();
let nat_var = var_store.fresh();
@ -1113,6 +1114,30 @@ fn num_bytes_to_u16(symbol: Symbol, var_store: &mut VarStore) -> Def {
)
}
/// Num.bytesToU32 : List U8, Nat -> U32
fn num_bytes_to_u32(symbol: Symbol, var_store: &mut VarStore) -> Def {
let list_var = var_store.fresh();
let nat_var = var_store.fresh();
let ret_var = var_store.fresh();
let body = RunLowLevel {
op: LowLevel::NumBytesToU32,
args: vec![
(list_var, Var(Symbol::ARG_1)),
(nat_var, Var(Symbol::ARG_2)),
],
ret_var,
};
defn(
symbol,
vec![(list_var, Symbol::ARG_1), (nat_var, Symbol::ARG_2)],
var_store,
body,
ret_var,
)
}
/// Num.bitwiseAnd : Int a, Int a -> Int a
fn num_bitwise_and(symbol: Symbol, var_store: &mut VarStore) -> Def {
num_binop(symbol, var_store, LowLevel::NumBitwiseAnd)

View file

@ -4756,6 +4756,24 @@ fn run_low_level<'a, 'ctx, 'env>(
bitcode::NUM_BYTES_TO_U16,
)
}
NumBytesToU32 => {
debug_assert_eq!(args.len(), 2);
let list = load_symbol(scope, &args[0]).into_struct_value();
let position = load_symbol(scope, &args[1]);
call_bitcode_fn(
env,
&[
complex_bitcast(
env.builder,
list.into(),
env.context.i128_type().into(),
"to_i128",
),
position.into(),
],
bitcode::NUM_BYTES_TO_U32,
)
}
NumCompare => {
use inkwell::FloatPredicate;

View file

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

View file

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

View file

@ -1012,8 +1012,8 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
NumAbs | NumNeg | NumSin | NumCos | NumSqrtUnchecked | NumLogUnchecked | NumRound
| NumCeiling | NumFloor | NumToFloat | Not | NumIsFinite | NumAtan | NumAcos | NumAsin
| NumIntCast => arena.alloc_slice_copy(&[irrelevant]),
// TODO: Fix me! I am wrong!
NumBytesToU16 => arena.alloc_slice_copy(&[borrowed, irrelevant]),
NumBytesToU32 => arena.alloc_slice_copy(&[borrowed, irrelevant]),
StrStartsWith | StrEndsWith => arena.alloc_slice_copy(&[owned, borrowed]),
StrStartsWithCodePt => arena.alloc_slice_copy(&[borrowed, irrelevant]),
StrFromUtf8 => arena.alloc_slice_copy(&[owned]),

View file

@ -1621,13 +1621,24 @@ mod gen_num {
assert_evals_to!("Num.isMultipleOf -9223372036854775808 -1", true, bool);
}
// TODO: Once we know what the function should do, let's give these real names!
#[test]
fn num_bytes_to_u16_ascii() {
fn potato_1() {
assert_evals_to!("Num.bytesToU16 [] 1", 40, u16);
}
#[test]
fn num_bytes_to_u16_ascii_2() {
fn potato_2() {
assert_evals_to!("Num.bytesToU16 [] 0", 40, u16);
}
#[test]
fn potato_3() {
assert_evals_to!("Num.bytesToU32 [] 1", 41, u32);
}
#[test]
fn potato_4() {
assert_evals_to!("Num.bytesToU32 [] 0", 41, u32);
}
}