From 6e79d572960eec56dbe623c1a4fa824cc4ff4fd1 Mon Sep 17 00:00:00 2001 From: Jeeves Date: Tue, 29 Apr 2025 12:09:03 -0600 Subject: [PATCH] clean up implementation code --- src/main.zig | 102 ++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/main.zig b/src/main.zig index 8786d29..8163614 100644 --- a/src/main.zig +++ b/src/main.zig @@ -147,7 +147,7 @@ pub const Circuit = struct { if (self.update_order == null) try self.calculateUpdateOrder(); for (self.update_order.?) |component| { std.log.debug("updating {}@{d}", .{ component, self.componentIndex(component).? }); - try component.process(); + try component.update(); } } @@ -241,7 +241,7 @@ pub const Component = struct { inputs: Inputs, outputs: Outputs, - processFn: *const fn (*Component) AllocatorError!void, + updateFn: *const fn (*Component) AllocatorError!void, deinitFn: *const fn (*Component, std.mem.Allocator) void, pub const Input = struct { @@ -272,7 +272,7 @@ pub const Component = struct { name: []const u8, inputs_len: usize, outputs_len: usize, - processFn: *const fn (*Component) AllocatorError!void, + updateFn: *const fn (*Component) AllocatorError!void, deinitFn: *const fn (*Component, std.mem.Allocator) void, ) !void { var inputs = Inputs.empty; @@ -288,7 +288,7 @@ pub const Component = struct { self.name = name; self.inputs = inputs; self.outputs = outputs; - self.processFn = processFn; + self.updateFn = updateFn; self.deinitFn = deinitFn; } @@ -300,8 +300,8 @@ pub const Component = struct { self.deinitFn(self, allocator); } - pub inline fn process(self: *Component) AllocatorError!void { - try self.processFn(self); + pub inline fn update(self: *Component) AllocatorError!void { + try self.updateFn(self); } // TODO allow inserting the new elements at an arbitrary index @@ -384,7 +384,7 @@ pub const Microchip = struct { errdefer allocator.destroy(self); self.circuit = Circuit.init(allocator); errdefer self.circuit.deinit(); - try Component.init(&self.component, allocator, "Microchip", 0, 0, &process, &deinit); + try Component.init(&self.component, allocator, "Microchip", 0, 0, &update, &deinit); return self; } @@ -394,7 +394,7 @@ pub const Microchip = struct { allocator.destroy(self); } - pub fn process(component: *Component) AllocatorError!void { + pub fn update(component: *Component) AllocatorError!void { const self: *Microchip = @fieldParentPtr("component", component); try self.circuit.tick(); } @@ -413,7 +413,7 @@ pub const Battery = struct { pub fn init(allocator: std.mem.Allocator) !*Battery { var self = try allocator.create(Battery); errdefer allocator.destroy(self); - try Component.init(&self.component, allocator, "Battery", 0, 1, &process, &deinit); + try Component.init(&self.component, allocator, "Battery", 0, 1, &update, &deinit); return self; } @@ -422,10 +422,11 @@ pub const Battery = struct { allocator.destroy(self); } - pub fn process(component: *Component) AllocatorError!void { + pub fn update(component: *Component) AllocatorError!void { const self: *Battery = @fieldParentPtr("component", component); - component.outputs.items[0].signal.digital = @intFromFloat(std.math.sign(self.value)); - component.outputs.items[0].signal.analog = self.value; + const output = &component.outputs.items[0].signal; + output.digital = @intFromFloat(std.math.sign(self.value)); + output.analog = self.value; } }; @@ -438,7 +439,7 @@ pub const Not = struct { var self = try allocator.create(Not); errdefer allocator.destroy(self); self.* = .{ .component = undefined }; - try Component.init(&self.component, allocator, "NOT", 1, 1, &process, &deinit); + try Component.init(&self.component, allocator, "NOT", 1, 1, &update, &deinit); return self; } @@ -447,17 +448,18 @@ pub const Not = struct { allocator.destroy(self); } - pub fn process(component: *Component) AllocatorError!void { + pub fn update(component: *Component) AllocatorError!void { const self: *Not = @fieldParentPtr("component", component); + const output = &component.outputs.items[0].signal; const input = if (component.inputs.items[0].signal) |s| s else &Signal{}; if (self.invert_output) { - component.outputs.items[0].signal.digital = 1 - @as(i2, @intCast(@abs(input.digital))); - component.outputs.items[0].signal.analog = 1.0 - @abs(input.analog); + output.digital = 1 - @as(i2, @intCast(@abs(input.digital))); + output.analog = 1.0 - @abs(input.analog); } else { - component.outputs.items[0].signal.digital = input.digital; - component.outputs.items[0].signal.analog = input.analog; + output.digital = input.digital; + output.analog = input.analog; } - component.outputs.items[0].signal.analog = std.math.clamp(component.outputs.items[0].signal.analog, -1.0, 1.0); + output.analog = std.math.clamp(output.analog, -1.0, 1.0); } }; @@ -472,7 +474,7 @@ pub const And = struct { var self = try allocator.create(And); errdefer allocator.destroy(self); self.* = .{ .component = undefined }; - try Component.init(&self.component, allocator, "AND", 2, 1, &process, &deinit); + try Component.init(&self.component, allocator, "AND", 2, 1, &update, &deinit); return self; } @@ -482,31 +484,32 @@ pub const And = struct { } // TODO check implementation - pub fn process(component: *Component) AllocatorError!void { + pub fn update(component: *Component) AllocatorError!void { const self: *And = @fieldParentPtr("component", component); + const output = &component.outputs.items[0].signal; const input0 = if (component.inputs.items[0].signal) |s| s else &Signal{}; if (self.arithmetic_mode) { - component.outputs.items[0].signal.digital = input0.digital; - component.outputs.items[0].signal.analog = input0.analog; + output.digital = input0.digital; + output.analog = input0.analog; for (component.inputs.items[1..]) |opt_input| { const input = if (opt_input.signal) |s| s else &Signal{}; - component.outputs.items[0].signal.digital = 0; // TODO - component.outputs.items[0].signal.analog *= input.analog; + output.digital = 0; // TODO + output.analog *= input.analog; } } else { - component.outputs.items[0].signal.digital = input0.digital; - component.outputs.items[0].signal.analog = input0.analog; + output.digital = input0.digital; + output.analog = input0.analog; for (component.inputs.items[1..]) |opt_input| { const input = if (opt_input.signal) |s| s else &Signal{}; - component.outputs.items[0].signal.digital = 0; // TODO - component.outputs.items[0].signal.analog = switch (std.math.order(@abs(component.outputs.items[0].signal.analog), @abs(input.analog))) { - .lt => component.outputs.items[0].signal.analog, - .eq => @min(component.outputs.items[0].signal.analog, input.analog), // TODO what does this *actually* do? + output.digital = 0; // TODO + output.analog = switch (std.math.order(@abs(output.analog), @abs(input.analog))) { + .lt => output.analog, + .eq => @min(output.analog, input.analog), // TODO what does this *actually* do? .gt => input.analog, }; } } - component.outputs.items[0].signal.analog = std.math.clamp(component.outputs.items[0].signal.analog, -1.0, 1.0); + output.analog = std.math.clamp(output.analog, -1.0, 1.0); } }; @@ -519,12 +522,12 @@ test "min" { and1.component.inputs.items[0].signal = &a; and1.component.inputs.items[1].signal = &b; - try and1.component.process(); + try and1.component.update(); try std.testing.expectEqual(0.0, and1.component.outputs.items[0].signal.analog); a.analog = -0.5; b.analog = -0.2; - try and1.component.process(); + try and1.component.update(); try std.testing.expectEqual(-0.2, and1.component.outputs.items[0].signal.analog); } @@ -539,7 +542,7 @@ pub const Or = struct { var self = try allocator.create(Or); errdefer allocator.destroy(self); self.* = .{ .component = undefined }; - try Component.init(&self.component, allocator, "OR", 2, 1, &process, &deinit); + try Component.init(&self.component, allocator, "OR", 2, 1, &update, &deinit); return self; } @@ -549,31 +552,32 @@ pub const Or = struct { } // TODO check implementation - pub fn process(component: *Component) AllocatorError!void { + pub fn update(component: *Component) AllocatorError!void { const self: *Or = @fieldParentPtr("component", component); + const output = &component.outputs.items[0].signal; const input0 = if (component.inputs.items[0].signal) |s| s else &Signal{}; if (self.arithmetic_mode) { - component.outputs.items[0].signal.digital = input0.digital; - component.outputs.items[0].signal.analog = input0.analog; + output.digital = input0.digital; + output.analog = input0.analog; for (component.inputs.items[1..]) |opt_input| { const input = if (opt_input.signal) |s| s else &Signal{}; - component.outputs.items[0].signal.digital = 0; // TODO - component.outputs.items[0].signal.analog += input.analog; + output.digital = 0; // TODO + output.analog += input.analog; } } else { - component.outputs.items[0].signal.digital = input0.digital; - component.outputs.items[0].signal.analog = input0.analog; + output.digital = input0.digital; + output.analog = input0.analog; for (component.inputs.items[1..]) |opt_input| { const input = if (opt_input.signal) |s| s else &Signal{}; - component.outputs.items[0].signal.digital = 0; // TODO - component.outputs.items[0].signal.analog = switch (std.math.order(@abs(component.outputs.items[0].signal.analog), @abs(input.analog))) { + output.digital = 0; // TODO + output.analog = switch (std.math.order(@abs(output.analog), @abs(input.analog))) { .lt => input.analog, - .eq => @max(component.outputs.items[0].signal.analog, input.analog), // TODO what does this *actually* do? - .gt => component.outputs.items[0].signal.analog, + .eq => @max(output.analog, input.analog), // TODO what does this *actually* do? + .gt => output.analog, }; } } - component.outputs.items[0].signal.analog = std.math.clamp(component.outputs.items[0].signal.analog, -1.0, 1.0); + output.analog = std.math.clamp(output.analog, -1.0, 1.0); } }; @@ -586,12 +590,12 @@ test "max" { or1.component.inputs.items[0].signal = &a; or1.component.inputs.items[1].signal = &b; - try or1.component.process(); + try or1.component.update(); try std.testing.expectEqual(1.0, or1.component.outputs.items[0].signal.analog); a.analog = -0.5; b.analog = -0.2; - try or1.component.process(); + try or1.component.update(); try std.testing.expectEqual(-0.5, or1.component.outputs.items[0].signal.analog); }