Skip to content

Commit 6806c87

Browse files
committed
Proper module structure
1 parent aed470a commit 6806c87

File tree

5 files changed

+17
-29
lines changed

5 files changed

+17
-29
lines changed

.justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ run:
2929
# Test the project
3030
test:
3131
@echo "Testing..."
32-
@zig build test
32+
@zig build test --summary all
3333

3434
# Remove build artifacts and non-essential files
3535
clean:

build.zig

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,12 @@ pub fn build(b: *std.Build) void {
4242
// Modules can depend on one another using the `std.Build.Module.addImport` function.
4343
// This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
4444
// file path. In this case, we set up `exe_mod` to import `lib_mod`.
45-
exe_mod.addImport("micrograd_lib", lib_mod);
46-
47-
// Now, we will create a static library based on the module we created above.
48-
// This creates a `std.Build.Step.Compile`, which is the build step responsible
49-
// for actually invoking the compiler.
50-
const lib = b.addLibrary(.{
51-
.linkage = .static,
52-
.name = "micrograd",
53-
.root_module = lib_mod,
54-
});
55-
56-
// This declares intent for the library to be installed into the standard
57-
// location when the user invokes the "install" step (the default step when
58-
// running `zig build`).
59-
b.installArtifact(lib);
45+
exe_mod.addImport("micrograd", lib_mod);
6046

6147
// This creates another `std.Build.Step.Compile`, but this one builds an executable
6248
// rather than a static library.
6349
const exe = b.addExecutable(.{
64-
.name = "micrograd",
50+
.name = "train",
6551
.root_module = exe_mod,
6652
});
6753

src/engine.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
//! This file provides
1+
//! This file provides engine functionality for micrograd
22

33
const std = @import("std");
4-
const arch = @import("../arch/arch.zig");
54

65
/// Operations for the engine
76
pub const Operation = enum(u8) {
@@ -28,6 +27,11 @@ pub fn Value(comptime T: type) type {
2827
};
2928
}
3029

30+
/// Convert the Value to a string
31+
pub fn toString(self: @This()) []const u8 {
32+
return std.fmt.allocPrint(std.heap.page_allocator, "Value(value={any})", .{self.value}) catch unreachable;
33+
}
34+
3135
/// Add two values
3236
pub fn add(self: @This(), other: @This()) @This() {
3337
return @This(){

src/main.zig

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
//! you are building an executable. If you are making a library, the convention
33
//! is to delete this file and start with root.zig instead.
44

5-
const engine = @import("engine.zig");
5+
const std = @import("std");
6+
/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
7+
const lib = @import("micrograd");
68

79
pub fn main() !void {
8-
const value = engine.Value(f32).init(3.14);
10+
const value = lib.engine.Value(f32).init(3.14);
911

10-
std.debug.print("Value: {}\n", .{value});
12+
std.debug.print("{s}\n", .{value.toString()});
1113

12-
const value2 = engine.Value(i32).init(0x01);
13-
std.debug.print("Value2: {}\n", .{value2});
14+
const value2 = lib.engine.Value(i32).init(0x01);
15+
std.debug.print("{s}\n", .{value2.toString()});
1416

1517
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
1618
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
@@ -48,8 +50,3 @@ test "fuzz example" {
4850
};
4951
try std.testing.fuzz(Context{}, Context.testOne, .{});
5052
}
51-
52-
const std = @import("std");
53-
54-
/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details.
55-
const lib = @import("micrograd_lib");

src/root.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! start with main.zig instead.
44
const std = @import("std");
55
const testing = std.testing;
6+
pub const engine = @import("engine.zig");
67

78
pub export fn add(a: i32, b: i32) i32 {
89
return a + b;

0 commit comments

Comments
 (0)