diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Float.zig | 22 | ||||
| -rw-r--r-- | src/Int.zig | 41 | ||||
| -rw-r--r-- | src/List.zig | 49 | ||||
| -rw-r--r-- | src/Tuple.zig | 4 | ||||
| -rw-r--r-- | src/main.zig | 5 | ||||
| -rw-r--r-- | src/root.zig | 9 |
6 files changed, 122 insertions, 8 deletions
diff --git a/src/Float.zig b/src/Float.zig new file mode 100644 index 0000000..5877d12 --- /dev/null +++ b/src/Float.zig @@ -0,0 +1,22 @@ +const c = @import("c"); + +const zpy = @import("root.zig"); +const conversion = zpy.conversion; + +const Self = @This(); + +object: zpy.Object, + +pub fn from(value: f64) !Self { + return .{ + .object = .fromPtr(try conversion.ptr(c.PyFloat_FromDouble(value))), + }; +} + +pub fn asDouble(self: *const Self) f64 { + return c.PyFloat_asDouble(self.object.ptr); +} + +pub fn deinit(self: *Self) void { + self.object.decref(); +} diff --git a/src/Int.zig b/src/Int.zig new file mode 100644 index 0000000..bd13e99 --- /dev/null +++ b/src/Int.zig @@ -0,0 +1,41 @@ +const std = @import("std"); +const c = @import("c"); + +const zpy = @import("root.zig"); +const conversion = zpy.conversion; + +const Self = @This(); + +object: zpy.Object, + +pub fn from(value: anytype) !Self { + const T = @TypeOf(value); + const result = switch (@typeInfo(T)) { + .int => |i| switch (i.signedness) { + .signed => c.PyLong_FromLong(value), + .unsigned => c.PyLong_FromUnsignedLong(value), + }, + .comptime_int => c.PyLong_FromLong(value), + .float => c.PyLong_FromDouble(value), + else => @compileError("value must be numberic."), + }; + + return .{ + .object = .fromPtr(try conversion.ptr(result)), + }; +} + +pub fn as(self: *const Self, T: type) T { + return switch(@typeInfo(T)) { + .int => |i| switch (i) { + .signed => c.PyLong_AsLong(self.object.ptr), + .unsigned => c.PyLong_AsUnsignedLong(self.object.ptr), + }, + .float => c.PyLong_AsDouble(self.object.ptr), + else => @compileError("value must be numberic."), + }; +} + +pub fn deinit(self: *Self) void { + self.object.decref(); +} diff --git a/src/List.zig b/src/List.zig new file mode 100644 index 0000000..6c9f65c --- /dev/null +++ b/src/List.zig @@ -0,0 +1,49 @@ +const c = @import("c"); + +const zpy = @import("root.zig"); +const conversion = zpy.conversion; + +const Self = @This(); + +object: zpy.Object, + +pub fn new(items: []const zpy.Object) !Self { + var list: Self = .{ + .object = .fromPtr(try conversion.ptr(c.PyList_New(items.len))), + }; + errdefer list.deinit(); + + for (items, 0..) |item, index| { + try list.insert(index, item); + } +} + +pub fn size(self: *const Self) usize { + return @as(usize, c.PyList_Size(self.object.ptr)); +} + +pub fn get(self: *const Self, index: usize) !zpy.Object { + return .fromC(try conversion.ptr(c.PyList_GetItem(self.object.ptr, index))); +} + +pub fn set(self: *Self, index: usize, value: zpy.Object) !void { + return conversion.success( + c.PyList_SetItem(self.object.ptr, index, value.ptr), + ); +} + +pub fn insert(self: *Self, index: usize, value: zpy.Object) !void { + return .conversion.success( + c.PyList_Insert(self.object.ptr, index, value.ptr) + ); +} + +pub fn append(self: *Self, value: zpy.Object) !void { + return .conversion.success( + c.PyList_Append(self.object.ptr, value.ptr), + ); +} + +pub fn deinit(self: *Self) void { + self.object.decref(); +} diff --git a/src/Tuple.zig b/src/Tuple.zig index ceb1711..af1ec80 100644 --- a/src/Tuple.zig +++ b/src/Tuple.zig @@ -9,7 +9,7 @@ object: zpy.Object, pub fn new(length: usize) !Self { return .{ - .object = .fromPtr(try conversion.Ptr(c.PyTuple_New(@intCast(length)))), + .object = .fromPtr(try conversion.ptr(c.PyTuple_New(@intCast(length)))), }; } @@ -29,7 +29,7 @@ pub fn size(self: *const Self) usize { } pub fn get(self: *Self, index: usize) zpy.Error!zpy.Object { - return .fromPtr(conversion.Ptr(c.PyTuple_GetItem(self.object.ptr, @intCast(index))) catch return error.IndexError); + return .fromPtr(conversion.ptr(c.PyTuple_GetItem(self.object.ptr, @intCast(index))) catch return error.IndexError); } pub fn set(self: *Self, index: usize, value: zpy.Object) zpy.Error!void { diff --git a/src/main.zig b/src/main.zig index 0cd2ec5..cfd7704 100644 --- a/src/main.zig +++ b/src/main.zig @@ -12,11 +12,10 @@ pub fn main() !void { var test_fn = py.globals.get(.{ .string = "hello" }) orelse unreachable; defer test_fn.decref(); var result = try test_fn.call(try .pack(&.{ - zpy.Str.fromString("hello").object, - zpy.Str.fromString("world").object, + (try zpy.Int.from(42)).object, + (try zpy.Int.from(20)).object, }), null); defer result.decref(); - std.debug.print("globals = {f}\n", .{py.globals.object}); std.debug.print("hello() = {f}\n", .{result}); } diff --git a/src/root.zig b/src/root.zig index 471267f..ff7e017 100644 --- a/src/root.zig +++ b/src/root.zig @@ -6,6 +6,9 @@ pub const Dict = @import("Dict.zig"); pub const Str = @import("Str.zig"); pub const Tuple = @import("Tuple.zig"); pub const Fn = @import("function.zig").Fn; +pub const Int = @import("Int.zig"); +pub const Float = @import("Float.zig"); +pub const List = @import("List.zig"); pub const Error = error { Exception, @@ -18,9 +21,9 @@ pub const conversion = struct { return *info.pointer.child; } - pub fn Ptr(value: anytype) Error!PtrFromCPtr(@TypeOf(value)) { - if (value) |ptr| { - return ptr; + pub fn ptr(value: anytype) Error!PtrFromCPtr(@TypeOf(value)) { + if (value) |p| { + return p; } return error.Exception; |