mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-15 04:10:14 +03:00
Support Num.toStr for dec
This commit is contained in:
parent
b7c312d449
commit
66b8d145a9
@ -131,7 +131,7 @@ pub const RocDec = extern struct {
|
||||
return (c -% 48) <= 9;
|
||||
}
|
||||
|
||||
pub fn toStr(self: RocDec) ?RocStr {
|
||||
pub fn toStr(self: RocDec) RocStr {
|
||||
// Special case
|
||||
if (self.num == 0) {
|
||||
return RocStr.init("0.0", 3);
|
||||
@ -1065,6 +1065,10 @@ pub fn fromStr(arg: RocStr) callconv(.C) num_.NumParseResult(i128) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toStr(arg: RocDec) callconv(.C) RocStr {
|
||||
return @call(.{ .modifier = always_inline }, RocDec.toStr, .{arg});
|
||||
}
|
||||
|
||||
pub fn fromF64C(arg: f64) callconv(.C) i128 {
|
||||
return if (@call(.{ .modifier = always_inline }, RocDec.fromF64, .{arg})) |dec| dec.num else @panic("TODO runtime exception failing convert f64 to RocDec");
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ const dec = @import("dec.zig");
|
||||
|
||||
comptime {
|
||||
exportDecFn(dec.fromStr, "from_str");
|
||||
exportDecFn(dec.toStr, "to_str");
|
||||
exportDecFn(dec.fromF64C, "from_f64");
|
||||
exportDecFn(dec.eqC, "eq");
|
||||
exportDecFn(dec.neqC, "neq");
|
||||
|
@ -372,6 +372,7 @@ pub const LIST_APPEND_UNSAFE: &str = "roc_builtins.list.append_unsafe";
|
||||
pub const LIST_RESERVE: &str = "roc_builtins.list.reserve";
|
||||
|
||||
pub const DEC_FROM_STR: &str = "roc_builtins.dec.from_str";
|
||||
pub const DEC_TO_STR: &str = "roc_builtins.dec.to_str";
|
||||
pub const DEC_FROM_F64: &str = "roc_builtins.dec.from_f64";
|
||||
pub const DEC_EQ: &str = "roc_builtins.dec.eq";
|
||||
pub const DEC_NEQ: &str = "roc_builtins.dec.neq";
|
||||
|
@ -13,7 +13,7 @@ use crate::llvm::build_list::{
|
||||
list_replace_unsafe, list_reserve, list_sort_with, list_sublist, list_swap,
|
||||
list_symbol_to_c_abi, list_to_c_abi, list_with_capacity, pass_update_mode,
|
||||
};
|
||||
use crate::llvm::build_str::{str_from_float, str_from_int};
|
||||
use crate::llvm::build_str::{dec_to_str, str_from_float, str_from_int};
|
||||
use crate::llvm::compare::{generic_eq, generic_neq};
|
||||
use crate::llvm::convert::{
|
||||
self, argument_type_from_layout, basic_type_from_builtin, basic_type_from_layout,
|
||||
@ -5825,6 +5825,7 @@ fn run_low_level<'a, 'ctx, 'env>(
|
||||
Layout::Builtin(Builtin::Float(float_width)) => {
|
||||
str_from_float(env, num, *float_width)
|
||||
}
|
||||
Layout::Builtin(Builtin::Decimal) => dec_to_str(env, num),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
@ -112,10 +112,28 @@ pub fn str_from_float<'a, 'ctx, 'env>(
|
||||
call_str_bitcode_fn(env, &[float], &bitcode::STR_FROM_FLOAT[float_width])
|
||||
}
|
||||
|
||||
/// Dec.toStr : Dec -> Str
|
||||
pub fn dec_to_str<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
dec: BasicValueEnum<'ctx>,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let float = load_symbol(scope, &int_symbol);
|
||||
let dec = dec.into_int_value();
|
||||
|
||||
call_str_bitcode_fn(env, &[float], bitcode::STR_FROM_FLOAT)
|
||||
let int_64 = env.context.i128_type().const_int(64, false);
|
||||
let int_64_type = env.context.i64_type();
|
||||
|
||||
let dec_right_shift = env
|
||||
.builder
|
||||
.build_right_shift(dec, int_64, false, "dec_left_bits");
|
||||
|
||||
let right_bits = env.builder.build_int_cast(dec, int_64_type, "");
|
||||
let left_bits = env.builder.build_int_cast(dec_right_shift, int_64_type, "");
|
||||
|
||||
call_str_bitcode_fn(
|
||||
env,
|
||||
&[right_bits.into(), left_bits.into()],
|
||||
bitcode::DEC_TO_STR,
|
||||
)
|
||||
}
|
||||
|
||||
/// Str.equal : Str, Str -> Bool
|
||||
|
@ -3102,6 +3102,30 @@ fn num_to_str_f64() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn num_to_str_dec() {
|
||||
use roc_std::RocStr;
|
||||
|
||||
assert_evals_to!(r#"Num.toStr -10.75dec"#, RocStr::from("-10.75"), RocStr);
|
||||
assert_evals_to!(r#"Num.toStr -1.75dec"#, RocStr::from("-1.75"), RocStr);
|
||||
assert_evals_to!(r#"Num.toStr 0dec"#, RocStr::from("0.0"), RocStr);
|
||||
assert_evals_to!(r#"Num.toStr 1.75dec"#, RocStr::from("1.75"), RocStr);
|
||||
assert_evals_to!(r#"Num.toStr 10.75dec"#, RocStr::from("10.75"), RocStr);
|
||||
|
||||
assert_evals_to!(
|
||||
r#"Num.toStr 170141183460469.105727dec"#,
|
||||
RocStr::from("170141183460469.105727"),
|
||||
RocStr
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
r#"Num.toStr -170141183460469.105727dec"#,
|
||||
RocStr::from("-170141183460469.105727"),
|
||||
RocStr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn u8_addition_greater_than_i8() {
|
||||
|
Loading…
Reference in New Issue
Block a user