mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 22:09:09 +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| {
|
||||
num.exportPow(T, ROC_BUILTINS ++ "." ++ NUM ++ ".pow_int.");
|
||||
num.exportDivCeil(T, ROC_BUILTINS ++ "." ++ NUM ++ ".div_ceil.");
|
||||
num.exportParseInt(T, ROC_BUILTINS ++ "." ++ NUM ++ ".to_int.");
|
||||
}
|
||||
|
||||
inline for (FLOATS) |T| {
|
||||
|
|
|
@ -2,6 +2,42 @@ const std = @import("std");
|
|||
const always_inline = std.builtin.CallOptions.Modifier.always_inline;
|
||||
const math = std.math;
|
||||
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 {
|
||||
comptime var f = struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue