diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-27 21:04:37 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-27 21:04:37 +0200 |
| commit | 28f47b8399c4f3c4a64921fb16e38e44e8ce749e (patch) | |
| tree | 53cde744130f2adf9c293fafde3c0a9c6a990d4b /src/Interpreter.zig | |
first commit
Diffstat (limited to 'src/Interpreter.zig')
| -rw-r--r-- | src/Interpreter.zig | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Interpreter.zig b/src/Interpreter.zig new file mode 100644 index 0000000..6862706 --- /dev/null +++ b/src/Interpreter.zig @@ -0,0 +1,39 @@ +const c = @import("c"); + +const zpy = @import("root.zig"); + +const Self = @This(); + +globals: zpy.Dict, + +pub fn init() !Self { + c.Py_Initialize(); + + const self: Self = .{ + .globals = try .new(), + }; + + return self; +} + +pub const RunOptions = struct { + locals: ?*zpy.Dict = null, +}; + +pub fn run(self: *Self, str: [*c]const u8, options: RunOptions) zpy.Error!zpy.Object { + if (c.PyRun_String( + str, + c.Py_file_input, + self.globals.object.ptr, + if (options.locals) |loc| loc.object.ptr else null, + )) |object| { + return .fromPtr(object); + } + + return error.Exception; +} + +pub fn deinit(self: *Self) void { + c.Py_Finalize(); + self.* = undefined; +} |