diff --git a/src/main.zig b/src/main.zig index 7eb9252..75c6db6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -7,47 +7,36 @@ pub fn main() !void { var input = Signal{ .digital = 1, .analog = 0.5 }; - var not1 = try Not.init(allocator); - defer not1.deinit(allocator); + var circuit = Circuit.init(allocator); + defer circuit.deinit(); + + var not1 = try circuit.addComponent(Not); not1.invert_output = false; not1.component.inputs.items[0] = &input; // manually set the input here - var battery1 = try Battery.init(allocator); - defer battery1.deinit(allocator); - var battery2 = try Battery.init(allocator); - defer battery2.deinit(allocator); - var battery3 = try Battery.init(allocator); - defer battery3.deinit(allocator); + var battery1 = try circuit.addComponent(Battery); + var battery2 = try circuit.addComponent(Battery); + var battery3 = try circuit.addComponent(Battery); battery1.value = -0.5; battery2.value = 0.5; battery3.value = -1.0; - var or1 = try Or.init(allocator); - defer or1.deinit(allocator); - var or2 = try Or.init(allocator); - defer or2.deinit(allocator); + var or1 = try circuit.addComponent(Or); + var or2 = try circuit.addComponent(Or); or1.arithmetic_mode = true; or2.arithmetic_mode = true; try or1.component.setNumInputs(allocator, 3); try or2.component.setNumInputs(allocator, 3); - var or3 = try Or.init(allocator); - defer or3.deinit(allocator); - var or4 = try Or.init(allocator); - defer or4.deinit(allocator); - var or5 = try Or.init(allocator); - defer or5.deinit(allocator); + var or3 = try circuit.addComponent(Or); + var or4 = try circuit.addComponent(Or); + var or5 = try circuit.addComponent(Or); - var or6 = try Or.init(allocator); - defer or6.deinit(allocator); - var or7 = try Or.init(allocator); - defer or7.deinit(allocator); - var or8 = try Or.init(allocator); - defer or8.deinit(allocator); - var and1 = try And.init(allocator); - defer and1.deinit(allocator); - var and2 = try And.init(allocator); - defer and2.deinit(allocator); + var or6 = try circuit.addComponent(Or); + var or7 = try circuit.addComponent(Or); + var or8 = try circuit.addComponent(Or); + var and1 = try circuit.addComponent(And); + var and2 = try circuit.addComponent(And); or6.arithmetic_mode = true; or7.arithmetic_mode = true; or8.arithmetic_mode = true; @@ -104,6 +93,36 @@ pub fn main() !void { std.debug.print("{}\n{}\n{}\n{}\n{}\n\n", .{ or6.component.outputs.items[0], and1.component.outputs.items[0], or7.component.outputs.items[0], and2.component.outputs.items[0], or8.component.outputs.items[0] }); } +pub const Circuit = struct { + allocator: std.mem.Allocator, + components: Components, + source_components: SourceComponents, + + pub fn init(allocator: std.mem.Allocator) Circuit { + return .{ + .allocator = allocator, + .components = Components.empty, + .source_components = SourceComponents.empty, + }; + } + + pub fn deinit(self: *Circuit) void { + self.source_components.deinit(self.allocator); + for (0..self.components.items.len) |i| self.components.items[i].deinit(self.allocator); + self.components.deinit(self.allocator); + } + + pub fn addComponent(self: *Circuit, comptime T: type) !T { + var c = try T.init(self.allocator); + errdefer c.deinit(self.allocator); + try self.components.append(self.allocator, c.component); + return c; + } + + const Components = std.ArrayListUnmanaged(Component); + const SourceComponents = std.ArrayListUnmanaged(*Component); +}; + var null_signal = Signal{}; pub const Component = struct {