From 956cd84ca545f1d93e2218cbbdbb39c81a46bec8 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Fri, 28 Aug 2026 17:44:38 +0200 Subject: implement other builtins --- src/Int.zig | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Int.zig (limited to 'src/Int.zig') 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(); +} -- cgit v1.2.3-70-g09d2