Support Num.toStr for f32, f64

This commit is contained in:
Ayaz Hafiz 2022-07-13 12:13:01 -04:00
parent 6ac9c37e06
commit b7c312d449
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
7 changed files with 79 additions and 14 deletions

View file

@ -5496,7 +5496,14 @@ fn run_low_level<'a, 'ctx, 'env>(
// Str.fromFloat : Float * -> Str
debug_assert_eq!(args.len(), 1);
str_from_float(env, scope, args[0])
let (float, float_layout) = load_symbol_and_layout(scope, &args[0]);
let float_width = match float_layout {
Layout::Builtin(Builtin::Float(float_width)) => *float_width,
_ => unreachable!(),
};
str_from_float(env, float, float_width)
}
StrFromUtf8Range => {
debug_assert_eq!(args.len(), 3);
@ -5815,8 +5822,8 @@ fn run_low_level<'a, 'ctx, 'env>(
str_from_int(env, int, *int_width)
}
Layout::Builtin(Builtin::Float(_float_width)) => {
str_from_float(env, scope, args[0])
Layout::Builtin(Builtin::Float(float_width)) => {
str_from_float(env, num, *float_width)
}
_ => unreachable!(),
}

View file

@ -3,7 +3,7 @@ use crate::llvm::build::{Env, Scope};
use inkwell::builder::Builder;
use inkwell::values::{BasicValueEnum, IntValue, PointerValue, StructValue};
use inkwell::AddressSpace;
use roc_builtins::bitcode::{self, IntWidth};
use roc_builtins::bitcode::{self, FloatWidth, IntWidth};
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout};
use roc_target::PtrWidth;
@ -103,11 +103,15 @@ pub fn decode_from_utf8_result<'a, 'ctx, 'env>(
}
}
/// Str.fromFloat : Int -> Str
/// Str.fromFloat : Float * -> Str
pub fn str_from_float<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,
int_symbol: Symbol,
float: BasicValueEnum<'ctx>,
float_width: FloatWidth,
) -> BasicValueEnum<'ctx> {
call_str_bitcode_fn(env, &[float], &bitcode::STR_FROM_FLOAT[float_width])
}
) -> BasicValueEnum<'ctx> {
let float = load_symbol(scope, &int_symbol);