From 2173abbd1a52f4af808c3356b63f554f3d2c5f76 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 3 Sep 2026 19:50:47 +0200 Subject: implement bytes --- src/bytes.zig | 43 +++++++++++++++ src/number.zig | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/root.zig | 2 + src/type/root.zig | 2 +- 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 src/bytes.zig create mode 100644 src/number.zig diff --git a/src/bytes.zig b/src/bytes.zig new file mode 100644 index 0000000..f1eb258 --- /dev/null +++ b/src/bytes.zig @@ -0,0 +1,43 @@ +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Bytes = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn fromString(string: []const u8) !*Self { + return @ptrCast(c.PyBytes_FromStringAndSize(@ptrCast(string), string.len) orelse return error.Exception); + } + + pub fn fromObject(object: *zpy.Object) !*Self { + return @ptrCast(c.PyBytes_FromObject(@ptrCast(object)) orelse return error.Exception); + } + + pub fn size(self: *const Self) usize { + return @intCast(c.PyBytes_Size(@ptrCast(@constCast(self)))); + } + + pub fn toString(self: *Self) []const u8 { + var buffer: [*]const u8 = undefined; + var length: c.Py_ssize_t = undefined; + + if (c.PyBytes_AsStringAndSize(@ptrCast(self), &buffer, &length) == -1) { + return error.Exception; + } + + return buffer[0..length]; + } + + pub fn concat(self: *Self, new: *Self) !void { + c.PyBytes_Concat(@ptrCast(&self), @ptrCast(new)); + } + + pub fn concatAndDelete(self: *Self, new: *Self) !void { + c.PyBytes_ConcatAndDel(@ptrCast(&self), @ptrCast(new)); + } + + pub fn join(self: *Self, object: *zpy.Object) !*zpy.Object { + return @ptrCast(c.PyBytes_Join(@ptrCast(self), @ptrCast(object)) orelse return error.Exception); + } +}; diff --git a/src/number.zig b/src/number.zig new file mode 100644 index 0000000..253dda3 --- /dev/null +++ b/src/number.zig @@ -0,0 +1,156 @@ +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Number = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn add(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Add(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn sub(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Subtract(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn mul(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Multiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn matrixMultiply(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_MatrixMultiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn floorDiv(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_floorDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn div(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_TrueDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn remainder(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Remainder(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn divmod(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Divmod(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn power(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Power(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn negative(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Negative(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn positive(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Positive(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn absolute(self: *Self) !*Self { + return @ptrCast(c.PyNumber_Absolute(@ptrCast(self)) orelse return error.Exception); + } + + pub fn invert(self: *Self) !*Self { + return @ptrCast(c.PyNumber_Invert(@ptrCast(self)) orelse return error.Exception); + } + + pub fn lshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Lshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn rshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Rshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn @"and"(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_And(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn xor(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Xor(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn @"or"(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_Or(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceAdd(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceAdd(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceSub(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceSubtract(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceMul(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceMultiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceMatrixMultiply(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceMatrixMultiply(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceFloorDiv(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlacefloorDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceDiv(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceTrueDivide(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceRemainder(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceRemainder(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceDivmod(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceDivmod(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlacePower(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlacePower(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceLshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceLshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceRshift(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceRshift(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceAnd(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceAnd(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceXor(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceXor(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn inPlaceOr(self: *Self, other: *Self) !*Self { + return @ptrCast(c.PyNumber_InPlaceOr(@ptrCast(self), @ptrCast(other)) orelse return error.Exception); + } + + pub fn toInt(self: *Self) !*zpy.Int { + return @ptrCast(c.PyNumber_Long(@ptrCast(self)) orelse return error.Exception); + } + + pub fn toFloat(self: *Self) !*zpy.Float { + return @ptrCast(c.PyNumber_Float(@ptrCast(self)) orelse return error.Exception); + } + + pub fn toBase(self: *Self, n: usize) !*zpy.Str { + return @ptrCast(c.PyNumber_ToBase(@ptrCast(self), @intCast(n)) orelse return error.Exception); + } + + pub fn as(self: *Self, T: type) T { + return @intCast(c.PyNumber_AsSize_t(@ptrCast(self), null)); + } + + pub fn asObject(self: *Self) !*zpy.Object { + return @ptrCast(self); + } +}; diff --git a/src/root.zig b/src/root.zig index 0f4d9a2..67f7f9e 100644 --- a/src/root.zig +++ b/src/root.zig @@ -10,9 +10,11 @@ pub const Str = @import("str.zig").Str; pub const Tuple = @import("tuple.zig").Tuple; pub const Int = @import("int.zig").Int; pub const Float = @import("float.zig").Float; +pub const Number = @import("number.zig").Number; pub const List = @import("list.zig").List; pub const Sequence = @import("sequence/root.zig").Sequence; pub const StructSequence = @import("struct-sequence.zig").StructSequence; +pub const Bytes = @import("bytes.zig").Bytes; pub const Interpreter = @import("Interpreter.zig"); diff --git a/src/type/root.zig b/src/type/root.zig index 128f3d9..9b7725e 100644 --- a/src/type/root.zig +++ b/src/type/root.zig @@ -19,7 +19,7 @@ pub const Type = extern struct { var c_desc: c.PyStructSequence_Desc = .{ .name = @ptrCast(description.name), .doc = @ptrCast(description.doc), - .fields = @ptrCast(@constCast(fields)), + .fields = @ptrCast(@constCast(fields ++ .{ std.mem.zeroes(Description) })), .n_in_sequence = @intCast(fields.len), }; -- cgit v1.2.3-70-g09d2