mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-22 00:09:33 +03:00
Implement Str.fromInt for any integer type
This commit is contained in:
parent
c18f306a7e
commit
491a480371
@ -4,6 +4,7 @@ const utils = @import("utils.zig");
|
||||
|
||||
const ROC_BUILTINS = "roc_builtins";
|
||||
const NUM = "num";
|
||||
const STR = "str";
|
||||
|
||||
// Dec Module
|
||||
const dec = @import("dec.zig");
|
||||
@ -115,7 +116,6 @@ comptime {
|
||||
exportStrFn(str.strConcatC, "concat");
|
||||
exportStrFn(str.strJoinWithC, "joinWith");
|
||||
exportStrFn(str.strNumberOfBytes, "number_of_bytes");
|
||||
exportStrFn(str.strFromIntC, "from_int");
|
||||
exportStrFn(str.strFromFloatC, "from_float");
|
||||
exportStrFn(str.strEqual, "equal");
|
||||
exportStrFn(str.strToUtf8C, "to_utf8");
|
||||
@ -123,6 +123,10 @@ comptime {
|
||||
exportStrFn(str.fromUtf8RangeC, "from_utf8_range");
|
||||
exportStrFn(str.repeat, "repeat");
|
||||
exportStrFn(str.strTrim, "trim");
|
||||
|
||||
inline for (INTEGERS) |T| {
|
||||
str.exportFromInt(T, ROC_BUILTINS ++ "." ++ STR ++ ".from_int.");
|
||||
}
|
||||
}
|
||||
|
||||
// Utils
|
||||
|
@ -413,9 +413,14 @@ pub fn strNumberOfBytes(string: RocStr) callconv(.C) usize {
|
||||
}
|
||||
|
||||
// Str.fromInt
|
||||
pub fn strFromIntC(int: i64) callconv(.C) RocStr {
|
||||
// prepare for having multiple integer types in the future
|
||||
return @call(.{ .modifier = always_inline }, strFromIntHelp, .{ i64, int });
|
||||
pub fn exportFromInt(comptime T: type, comptime name: []const u8) void {
|
||||
comptime var f = struct {
|
||||
fn func(int: T) callconv(.C) RocStr {
|
||||
return @call(.{ .modifier = always_inline }, strFromIntHelp, .{ T, int });
|
||||
}
|
||||
}.func;
|
||||
|
||||
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
|
||||
}
|
||||
|
||||
fn strFromIntHelp(comptime T: type, int: T) RocStr {
|
||||
|
@ -135,7 +135,7 @@ pub const STR_STARTS_WITH: &str = "roc_builtins.str.starts_with";
|
||||
pub const STR_STARTS_WITH_CODE_PT: &str = "roc_builtins.str.starts_with_code_point";
|
||||
pub const STR_ENDS_WITH: &str = "roc_builtins.str.ends_with";
|
||||
pub const STR_NUMBER_OF_BYTES: &str = "roc_builtins.str.number_of_bytes";
|
||||
pub const STR_FROM_INT: &str = "roc_builtins.str.from_int";
|
||||
pub const STR_FROM_INT: IntrinsicName = int_intrinsic!("roc_builtins.str.from_int");
|
||||
pub const STR_FROM_FLOAT: &str = "roc_builtins.str.from_float";
|
||||
pub const STR_EQUAL: &str = "roc_builtins.str.equal";
|
||||
pub const STR_TO_UTF8: &str = "roc_builtins.str.to_utf8";
|
||||
|
@ -11,7 +11,7 @@ use roc_builtins::bitcode;
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_mono::layout::{Builtin, Layout};
|
||||
|
||||
use super::build::load_symbol;
|
||||
use super::build::{intwidth_from_builtin, load_symbol, load_symbol_and_layout};
|
||||
|
||||
pub static CHAR_LAYOUT: Layout = Layout::Builtin(Builtin::Int8);
|
||||
|
||||
@ -265,9 +265,33 @@ pub fn str_from_int<'a, 'ctx, 'env>(
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
int_symbol: Symbol,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let int = load_symbol(scope, &int_symbol);
|
||||
let (int, int_layout) = load_symbol_and_layout(scope, &int_symbol);
|
||||
|
||||
call_bitcode_fn(env, &[int], bitcode::STR_FROM_INT)
|
||||
match int_layout {
|
||||
Layout::Builtin(builtin) => match builtin {
|
||||
Builtin::Usize
|
||||
| Builtin::Int128
|
||||
| Builtin::Int64
|
||||
| Builtin::Int32
|
||||
| Builtin::Int16
|
||||
| Builtin::Int8 => {
|
||||
let intwidth = intwidth_from_builtin(*builtin, env.ptr_bytes);
|
||||
call_bitcode_fn(env, &[int], &bitcode::STR_FROM_INT[intwidth])
|
||||
}
|
||||
_ => {
|
||||
unreachable!(
|
||||
"Compiler bug: tried to convert numeric on invalid builtin layout: ({:?})",
|
||||
int_layout
|
||||
);
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
unreachable!(
|
||||
"Compiler bug: tried to convert numeric on invalid layout: {:?}",
|
||||
int_layout
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Str.toUtf8 : Str -> List U8
|
||||
|
Loading…
Reference in New Issue
Block a user