feat(wasm): start StrToNum

This commit is contained in:
rvcas 2021-12-27 19:08:21 -05:00
parent 715c441b25
commit cd42f034b5
2 changed files with 220 additions and 1 deletions

View file

@ -46,7 +46,14 @@ pub fn dispatch_low_level<'a>(
return NotImplemented; return NotImplemented;
} }
StrCountGraphemes => return BuiltinCall(bitcode::STR_COUNT_GRAPEHEME_CLUSTERS), StrCountGraphemes => return BuiltinCall(bitcode::STR_COUNT_GRAPEHEME_CLUSTERS),
StrToNum => return NotImplemented, // choose builtin based on storage size StrToNum => match ret_layout {
WasmLayout::Primitive(_, _) => return NotImplemented,
WasmLayout::StackMemory { size, .. } => {
// how do I decided Float vs. Int vs. Dec
// how do I find out if it's signed or unsigned
return BuiltinCall(bitcode::STR_TO_INT);
}
}, // choose builtin based on storage size
StrFromInt => { StrFromInt => {
// This does not get exposed in user space. We switched to NumToStr instead. // This does not get exposed in user space. We switched to NumToStr instead.
// We can probably just leave this as NotImplemented. We may want remove this LowLevel. // We can probably just leave this as NotImplemented. We may want remove this LowLevel.

View file

@ -1121,3 +1121,215 @@ fn str_trim_right_small_to_small_shared() {
(RocStr, RocStr) (RocStr, RocStr)
); );
} }
#[test]
fn str_to_nat() {
assert_evals_to!(
indoc!(
r#"
when Str.toNat "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
usize
);
}
#[test]
fn str_to_i128() {
assert_evals_to!(
indoc!(
r#"
when Str.toI128 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
i128
);
}
#[test]
fn str_to_u128() {
assert_evals_to!(
indoc!(
r#"
when Str.toU128 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
u128
);
}
#[test]
fn str_to_i64() {
assert_evals_to!(
indoc!(
r#"
when Str.toI64 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
i64
);
}
#[test]
fn str_to_u64() {
assert_evals_to!(
indoc!(
r#"
when Str.toU64 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
u64
);
}
#[test]
fn str_to_i32() {
assert_evals_to!(
indoc!(
r#"
when Str.toI32 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
i32
);
}
#[test]
fn str_to_u32() {
assert_evals_to!(
indoc!(
r#"
when Str.toU32 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
u32
);
}
#[test]
fn str_to_i16() {
assert_evals_to!(
indoc!(
r#"
when Str.toI16 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
i16
);
}
#[test]
fn str_to_u16() {
assert_evals_to!(
indoc!(
r#"
when Str.toU16 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
u16
);
}
#[test]
fn str_to_i8() {
assert_evals_to!(
indoc!(
r#"
when Str.toI8 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
i8
);
}
#[test]
fn str_to_u8() {
assert_evals_to!(
indoc!(
r#"
when Str.toU8 "1" is
Ok n -> n
Err _ -> 0
"#
),
1,
u8
);
}
#[test]
fn str_to_f64() {
assert_evals_to!(
indoc!(
r#"
when Str.toF64 "1.0" is
Ok n -> n
Err _ -> 0
"#
),
1.0,
f64
);
}
#[test]
fn str_to_f32() {
assert_evals_to!(
indoc!(
r#"
when Str.toF32 "1.0" is
Ok n -> n
Err _ -> 0
"#
),
1.0,
f32
);
}
#[test]
fn str_to_dec() {
use roc_std::RocDec;
assert_evals_to!(
indoc!(
r#"
when Str.toDec "1.0" is
Ok n -> n
Err _ -> 0
"#
),
RocDec::from_str("1.0").unwrap(),
RocDec
);
}