mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 05:49:08 +00:00
sketch of StrToNum
This commit is contained in:
parent
d37ddeb162
commit
69fc1ad7e7
7 changed files with 60 additions and 0 deletions
|
@ -94,6 +94,7 @@ comptime {
|
||||||
inline for (INTEGERS) |T| {
|
inline for (INTEGERS) |T| {
|
||||||
num.exportPow(T, ROC_BUILTINS ++ "." ++ NUM ++ ".pow_int.");
|
num.exportPow(T, ROC_BUILTINS ++ "." ++ NUM ++ ".pow_int.");
|
||||||
num.exportDivCeil(T, ROC_BUILTINS ++ "." ++ NUM ++ ".div_ceil.");
|
num.exportDivCeil(T, ROC_BUILTINS ++ "." ++ NUM ++ ".div_ceil.");
|
||||||
|
num.exportParseInt(T, ROC_BUILTINS ++ "." ++ NUM ++ ".to_int.");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline for (FLOATS) |T| {
|
inline for (FLOATS) |T| {
|
||||||
|
|
|
@ -2,6 +2,42 @@ const std = @import("std");
|
||||||
const always_inline = std.builtin.CallOptions.Modifier.always_inline;
|
const always_inline = std.builtin.CallOptions.Modifier.always_inline;
|
||||||
const math = std.math;
|
const math = std.math;
|
||||||
const RocList = @import("list.zig").RocList;
|
const RocList = @import("list.zig").RocList;
|
||||||
|
const RocStr = @import("str.zig").RocStr;
|
||||||
|
|
||||||
|
pub fn NumParseResult(comptime T: type) type {
|
||||||
|
return extern struct {
|
||||||
|
errorcode: u8, // 0 indicates success
|
||||||
|
value: T,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exportParseInt(comptime T: type, comptime name: []const u8) void {
|
||||||
|
comptime var f = struct {
|
||||||
|
fn func(input: T, buf: RocStr) callconv(.C) NumParseResult(T) {
|
||||||
|
// a radix of 0 will make zig determine the radix from the frefix:
|
||||||
|
// * A prefix of "0b" implies radix=2,
|
||||||
|
// * A prefix of "0o" implies radix=8,
|
||||||
|
// * A prefix of "0x" implies radix=16,
|
||||||
|
// * Otherwise radix=10 is assumed.
|
||||||
|
const radix = 0;
|
||||||
|
if (std.fmt.parseInt(T, buf.asSlice(), radix)) |success| {
|
||||||
|
return .{ .errorcode = 0, .value = success };
|
||||||
|
} else |err| {
|
||||||
|
return .{ .errorcode = 1, .value = 0 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.func;
|
||||||
|
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exportParseFloat(comptime T: type, comptime name: []const u8) void {
|
||||||
|
comptime var f = struct {
|
||||||
|
fn func(input: T, buf: []const u8) callconv(.C) bool {
|
||||||
|
return std.fmt.parseFloat(T, buf);
|
||||||
|
}
|
||||||
|
}.func;
|
||||||
|
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
|
||||||
|
}
|
||||||
|
|
||||||
pub fn exportPow(comptime T: type, comptime name: []const u8) void {
|
pub fn exportPow(comptime T: type, comptime name: []const u8) void {
|
||||||
comptime var f = struct {
|
comptime var f = struct {
|
||||||
|
|
|
@ -242,6 +242,9 @@ 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_NUMBER_OF_BYTES: &str = "roc_builtins.str.number_of_bytes";
|
||||||
pub const STR_FROM_INT: IntrinsicName = int_intrinsic!("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_FROM_FLOAT: &str = "roc_builtins.str.from_float";
|
||||||
|
pub const STR_TO_INT: IntrinsicName = int_intrinsic!("roc_builtins.str.to_int");
|
||||||
|
pub const STR_TO_FLOAT: IntrinsicName = float_intrinsic!("roc_builtins.str.to_float");
|
||||||
|
pub const STR_TO_DECIMAL: &str = "roc_builtins.str.to_decimal";
|
||||||
pub const STR_EQUAL: &str = "roc_builtins.str.equal";
|
pub const STR_EQUAL: &str = "roc_builtins.str.equal";
|
||||||
pub const STR_TO_UTF8: &str = "roc_builtins.str.to_utf8";
|
pub const STR_TO_UTF8: &str = "roc_builtins.str.to_utf8";
|
||||||
pub const STR_FROM_UTF8: &str = "roc_builtins.str.from_utf8";
|
pub const STR_FROM_UTF8: &str = "roc_builtins.str.from_utf8";
|
||||||
|
|
|
@ -5268,6 +5268,23 @@ fn run_low_level<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
str_ends_with(env, scope, args[0], args[1])
|
str_ends_with(env, scope, args[0], args[1])
|
||||||
}
|
}
|
||||||
|
StrToNum => {
|
||||||
|
debug_assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
let (string, _string_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||||
|
|
||||||
|
// match on the return layout to figure out which zig builtin we need
|
||||||
|
let intrinsic = match layout {
|
||||||
|
Layout::Builtin(Builtin::Int(int_width)) => &bitcode::STR_TO_INT[*int_width],
|
||||||
|
Layout::Builtin(Builtin::Float(float_width)) => {
|
||||||
|
&bitcode::STR_TO_FLOAT[*float_width]
|
||||||
|
}
|
||||||
|
Layout::Builtin(Builtin::Decimal) => bitcode::STR_TO_DECIMAL,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
call_bitcode_fn(env, &[string], intrinsic)
|
||||||
|
}
|
||||||
StrFromInt => {
|
StrFromInt => {
|
||||||
// Str.fromInt : Int -> Str
|
// Str.fromInt : Int -> Str
|
||||||
debug_assert_eq!(args.len(), 1);
|
debug_assert_eq!(args.len(), 1);
|
||||||
|
|
|
@ -38,6 +38,7 @@ pub fn decode_low_level<'a>(
|
||||||
StrEndsWith => return BuiltinCall(bitcode::STR_ENDS_WITH),
|
StrEndsWith => return BuiltinCall(bitcode::STR_ENDS_WITH),
|
||||||
StrSplit => return NotImplemented, // needs Array
|
StrSplit => return NotImplemented, // needs Array
|
||||||
StrCountGraphemes => return NotImplemented, // test needs Array
|
StrCountGraphemes => return NotImplemented, // test needs Array
|
||||||
|
StrToNum => return NotImplemented, // choose builtin based on storage size
|
||||||
StrFromInt => return NotImplemented, // choose builtin based on storage size
|
StrFromInt => return NotImplemented, // choose builtin based on storage size
|
||||||
StrFromUtf8 => return NotImplemented, // needs Array
|
StrFromUtf8 => return NotImplemented, // needs Array
|
||||||
StrTrimLeft => return BuiltinCall(bitcode::STR_TRIM_LEFT),
|
StrTrimLeft => return BuiltinCall(bitcode::STR_TRIM_LEFT),
|
||||||
|
|
|
@ -13,6 +13,7 @@ pub enum LowLevel {
|
||||||
StrEndsWith,
|
StrEndsWith,
|
||||||
StrSplit,
|
StrSplit,
|
||||||
StrCountGraphemes,
|
StrCountGraphemes,
|
||||||
|
StrToNum,
|
||||||
StrFromInt,
|
StrFromInt,
|
||||||
StrFromUtf8,
|
StrFromUtf8,
|
||||||
StrFromUtf8Range,
|
StrFromUtf8Range,
|
||||||
|
|
|
@ -944,6 +944,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
|
||||||
StrTrimLeft => arena.alloc_slice_copy(&[owned]),
|
StrTrimLeft => arena.alloc_slice_copy(&[owned]),
|
||||||
StrTrimRight => arena.alloc_slice_copy(&[owned]),
|
StrTrimRight => arena.alloc_slice_copy(&[owned]),
|
||||||
StrSplit => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
StrSplit => arena.alloc_slice_copy(&[borrowed, borrowed]),
|
||||||
|
StrToNum => arena.alloc_slice_copy(&[borrowed]),
|
||||||
ListSingle => arena.alloc_slice_copy(&[irrelevant]),
|
ListSingle => arena.alloc_slice_copy(&[irrelevant]),
|
||||||
ListRepeat => arena.alloc_slice_copy(&[irrelevant, borrowed]),
|
ListRepeat => arena.alloc_slice_copy(&[irrelevant, borrowed]),
|
||||||
ListReverse => arena.alloc_slice_copy(&[owned]),
|
ListReverse => arena.alloc_slice_copy(&[owned]),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue