From 4b56a9bb6c35522edf8efb7f8b8e45f59d39ffe4 Mon Sep 17 00:00:00 2001 From: IonutParau Date: Thu, 22 May 2025 17:03:03 +0200 Subject: [PATCH] nule the Zig version --- src/engine.zig | 6 -- src/engine/component.zig | 74 -------------- src/engine/computer.zig | 212 --------------------------------------- src/engine/universe.zig | 18 ---- src/main.zig | 11 -- 5 files changed, 321 deletions(-) delete mode 100644 src/engine.zig delete mode 100644 src/engine/component.zig delete mode 100644 src/engine/computer.zig delete mode 100644 src/engine/universe.zig delete mode 100644 src/main.zig diff --git a/src/engine.zig b/src/engine.zig deleted file mode 100644 index 9863511..0000000 --- a/src/engine.zig +++ /dev/null @@ -1,6 +0,0 @@ -const std = @import("std"); -const testing = std.testing; - -pub const Computer = @import("engine/computer.zig"); -pub const Component = @import("engine/component.zig"); -pub const Universe = @import("engine/universe.zig"); diff --git a/src/engine/component.zig b/src/engine/component.zig deleted file mode 100644 index a3e7f2e..0000000 --- a/src/engine/component.zig +++ /dev/null @@ -1,74 +0,0 @@ -address: []const u8, -slot: isize, -allocator: Allocator, -userdata: *anyopaque, -vtable: *const VTable, -computer: *const Computer, - -const std = @import("std"); -const Allocator = std.mem.Allocator; -const Component = @This(); -const Computer = @import("computer.zig"); - -pub const Method = struct { - callback: Callback, - userdata: *anyopaque, - doc: []const u8, - direct: bool, - - pub const Callback = *const fn(componentUser: *anyopaque, callbackUser: *anyopaque, computer: *const Computer) callconv(.C) c_int; -}; - -// not an extern struct because of how C API will work -pub const VTable = struct { - componentType: []const u8, - methods: std.StringHashMap(Method), - resetBudget: ?*const fn(userdata: *anyopaque) callconv(.C) void, - passive: ?*const fn(userdata: *anyopaque) callconv(.C) void, - teardown: ?*const fn(userdata: *anyopaque) callconv(.C) void, - - pub fn init(ctype: []const u8, allocator: Allocator) VTable { - return VTable { - .componentType = ctype, - .methods = std.StringHashMap(Method).init(allocator), - .resetBudget = null, - .teardown = null, - }; - } -}; - -pub fn init(allocator: Allocator, address: []const u8, slot: isize, vtable: *const VTable, userdata: *anyopaque) !Component { - const ourAddr = try allocator.dupe(u8, address); - errdefer allocator.free(ourAddr); - - return Component { - .address = ourAddr, - .slot = slot, - .allocator = allocator, - .userdata = userdata, - .vtable = vtable, - }; -} - -pub fn resetBudget(self: *const Component) void { - self.vtable.resetBudget(self.userdata); -} - -pub fn passive(self: *const Component) void { - self.vtable.passive(self.userdata); -} - -pub fn invoke(self: *const Component, method: []const u8) c_int { - const res = self.vtable.methods.get(method); - if(res) |f| { - return f.callback(self.userdata, f.userdata, self.computer); - } - - // no such method - return 0; -} - -pub fn deinit(self: Component) void { - self.vtable.teardown(self.userdata); - self.allocator.free(self.address); -} diff --git a/src/engine/computer.zig b/src/engine/computer.zig deleted file mode 100644 index fefc14a..0000000 --- a/src/engine/computer.zig +++ /dev/null @@ -1,212 +0,0 @@ -address: []const u8, -time: f64, -allocator: Allocator, -components: std.StringHashMap(Component), -userdata: *anyopaque, -universe: *Universe, -stack: std.ArrayList(Value), -userError: Error, -isUserErrorAllocated: bool, -architectureData: *anyopaque, -architecture: Universe.Architecture, -architectures: std.ArrayList(Universe.Architecture), -state: State, - -pub fn init(address: []const u8, allocator: Allocator, userdata: *anyopaque, architecture: Universe.Architecture, universe: *Universe) Computer { - const ourAddr = try allocator.dupe(u8, address); - errdefer allocator.free(ourAddr); - - var c = Computer { - .address = ourAddr, - .time = 0, - .allocator = allocator, - .components = std.StringHashMap(Component).init(allocator), - .userdata = userdata, - .universe = universe, - .args = std.ArrayList(Value).init(allocator), - .ret = std.ArrayList(Value).init(allocator), - .userError = Error {.none = 0}, - .isUserErrorAllocated = false, - .architectureData = undefined, - .architecture = architecture, - .architectures = std.ArrayList(Universe.Architecture).init(allocator), - .state = State.running, - }; - c.architectureData = architecture.setup(architecture.udata, &c); - return c; -} - -const std = @import("std"); -const Allocator = std.mem.Allocator; -const Computer = @This(); -const Component = @import("component.zig"); -const Universe = @import("universe.zig"); - -pub const Error = union(enum) { - none, - raw: [*c]const u8, - allocated: [*c]const u8, -}; - -pub const State = enum { - running, - closing, - rebooting, - blackout, - overworked, -}; - -pub const Value = union(enum) { - nil, - integer: i64, - number: f64, - cstring: [*c]const u8, - string: []const u8, - - pub fn toInteger(self: Value) i64 { - return switch(self) { - .integer => |i| i, - .number => |f| @intFromFloat(f), - else => 0, - }; - } - - pub fn toNumber(self: Value) f64 { - return switch(self) { - .integer => |i| @floatFromInt(i), - .number => |f| f, - else => 0, - }; - } - - pub fn toString(self: Value) ?[]const u8 { - return switch(self) { - .string => |s| s, - .cstring => |c| std.mem.span(c), - else => null, - }; - } - - pub fn toCString(self: Value) ?[*c]const u8 { - // normal strings are NOT CAST because it could be a safety violation - return switch(self) { - .cstring => |c| c, - else => null, - }; - } - - pub fn initNil() Value { - return Value {.nil = .{}}; - } - - pub fn initInteger(i: i64) Value { - return Value {.integer = i}; - } - - pub fn initNumber(f: f64) Value { - return Value {.number = f}; - } - - pub fn initCString(cstr: [*c]const u8, allocator: Allocator) !Value { - const span = std.mem.span(cstr); - const mem = try allocator.dupeZ(u8, span); - return Value {.cstring = mem}; - } - - pub fn initString(str: []const u8, allocator: Allocator) !Value { - const mem = try allocator.dupe(u8, str); - return Value {.string = mem}; - } - - pub fn deinit(self: Value, allocator: Allocator) void { - switch(self) { - .string => |s| allocator.free(s), - .cstring => |c| allocator.free(c), - else => {}, - } - } -}; - -// Error handling - -pub fn clearError(self: *Computer) void { - switch(self.userError) { - .allocated => |c| { - self.allocator.free(c); - }, - else => {}, - } - - self.userError = Error {.none = .{}}; -} - -pub fn setCError(self: *Computer, err: [*c]const u8) void { - self.clearError(); - self.userError = Error {.raw = err}; -} - -pub fn setError(self: *Computer, err: [*c]const u8) void { - self.clearError(); - const maybeBuf = self.allocator.dupeZ(u8, std.mem.span(err)); - if(maybeBuf) |buf| { - self.userError = Error {.allocated = buf}; - } else |e| { - _ = e; - self.setCError("out of memory"); - } -} - -pub fn getError(self: *const Computer) ?[*c]const u8 { - return switch(self.userError) { - .none => null, - .raw => |c| c, - .allocated => |c| c, - }; -} - -// Component functions - -pub fn invoke(self: *Computer, address: []const u8, method: []const u8) void { - self.setError(null); - if(self.components.get(address)) |c| { - c.invoke(method); - return; - } - self.setError("no such component"); -} - -// end of epilogue, just deletes everything -pub fn resetCall(self: *Computer) void { - for(self.stack.items) |element| { - element.deinit(self.allocator); - } - - // retain capacity for speed - self.stack.clearRetainingCapacity(); -} - -pub fn deinit(self: Computer) void { - // just to be safe - defer self.components.deinit(); - defer self.stack.deinit(); - defer self.architectures.deinit(); - - // absolutely destroy everything - // burn it all to the ground - // leave no evidence behind - self.resetCall(); - - self.architecture.demolish(self.architecture.udata, self.architectureData); - - self.clearError(); - - var compIter = self.components.iterator(); - while(compIter.next()) |entry| { - entry.value_ptr.deinit(); - } -} - -pub fn process(self: *Computer) void { - self.clearError(); - self.architecture.tick(self.architecture.udata, self.architectureData, self); -} diff --git a/src/engine/universe.zig b/src/engine/universe.zig deleted file mode 100644 index 7b5b332..0000000 --- a/src/engine/universe.zig +++ /dev/null @@ -1,18 +0,0 @@ -allocator: Allocator, -components: std.StringHashMap(Computer), -host: Host, - -const std = @import("std"); -const Allocator = std.mem.Allocator; -const Computer = @import("computer.zig"); - -pub const Architecture = struct { - name: [*c]const u8, - udata: *anyopaque, - setup: *const fn(udata: *anyopaque, computer: *Computer) callconv(.C) *anyopaque, - tick: *const fn(udata: *anyopaque, context: *anyopaque, computer: *Computer) callconv(.C) void, - demolish: *const fn(udata: *anyopaque, context: *anyopaque) callconv(.C) void, -}; - -pub const Host = struct { -}; diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index 9c24ef2..0000000 --- a/src/main.zig +++ /dev/null @@ -1,11 +0,0 @@ -const std = @import("std"); - -pub fn main() !void { - const stdout_file = std.io.getStdOut().writer(); - var bw = std.io.bufferedWriter(stdout_file); - const stdout = bw.writer(); - - try stdout.print("Emulator is not even close to working.\n", .{}); - - try bw.flush(); // don't forget to flush! -}