Skip to content

Commit

Permalink
feat(std): parseUd
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 19, 2023
1 parent 1586bcd commit 0248b60
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
31 changes: 30 additions & 1 deletion src/lib/buzz_std.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export fn parseInt(ctx: *api.NativeCtx) c_int {

const string_slice = string.?[0..len];

const number: i32 = std.fmt.parseInt(i32, string_slice, 10) catch {
const number = std.fmt.parseInt(i32, string_slice, 10) catch {
ctx.vm.bz_push(api.Value.Null);

return 1;
Expand All @@ -113,6 +113,35 @@ export fn parseInt(ctx: *api.NativeCtx) c_int {
return 1;
}

export fn parseUd(ctx: *api.NativeCtx) c_int {
const string_value = ctx.vm.bz_peek(0);

var len: usize = 0;
const string = string_value.bz_valueToString(&len);

if (len == 0) {
ctx.vm.bz_push(api.Value.Null);

return 1;
}

const string_slice = string.?[0..len];

const number = std.fmt.parseInt(u64, string_slice, 10) catch {
ctx.vm.bz_push(api.Value.Null);

return 1;
};

if (api.ObjUserData.bz_newUserData(ctx.vm, number)) |userdata| {
ctx.vm.bz_pushUserData(userdata);

return 1;
} else {
@panic("Out of memory");
}
}

export fn parseFloat(ctx: *api.NativeCtx) c_int {
const string_value = ctx.vm.bz_peek(0);

Expand Down
9 changes: 7 additions & 2 deletions src/lib/std.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export extern fun assert(bool condition, str? message = null) > void;
|| @param value value to print
export extern fun print(str value) > void;

|| Parse number, returns false if string does not represent a number
|| Parse integer, returns false if string does not represent a integer
|| @param string string to parse
|| @return integer parsed or null
export extern fun parseInt(str string) > int?;

|| Parse number, returns false if string does not represent a number
|| Parse float, returns false if string does not represent a float
|| @param string string to parse
|| @return float parsed or null
export extern fun parseFloat(str string) > float?;
Expand All @@ -33,6 +33,11 @@ export extern fun toFloat(int n) > float;
|| @return casted value or 0 if value provided is not a number
export extern fun toUd(any n) > ud;

|| Parse ud, returns false if string does not represent a ud (u64)
|| @param string string to parse
|| @return ud parsed or null
export extern fun parseUd(str string) > ud?;

|| Return ascii char for given byte
export extern fun char(int byte) > str;

Expand Down
7 changes: 6 additions & 1 deletion src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ pub const Obj = struct {

return self_enum_instance.enum_ref == other_enum_instance.enum_ref and self_enum_instance.case == other_enum_instance.case;
},
.UserData => {
const self_userdata: *ObjUserData = ObjUserData.cast(self).?;
const other_userdata: *ObjUserData = ObjUserData.cast(other).?;

return self_userdata.userdata == other_userdata.userdata;
},
.Bound,
.Closure,
.Function,
Expand All @@ -460,7 +466,6 @@ pub const Obj = struct {
.Map,
.Enum,
.Native,
.UserData,
.Fiber,
.ForeignStruct,
=> {
Expand Down
2 changes: 2 additions & 0 deletions tests/023-std.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test "parseInt/Float" {
assert(toFloat(23) == 23.0, message: "Could cast int to float");
assert(toUd(23) is ud, message: "Could cast int to ud");
assert(toUd(23.0) is ud, message: "Could cast float to ud");

assert(parseUd("42") == toUd(42), message: "Could parse ud");
}

test "char" {
Expand Down

0 comments on commit 0248b60

Please sign in to comment.