aboutsummaryrefslogtreecommitdiff
path: root/examples/hello-zig
diff options
context:
space:
mode:
authorDavid Czihak <git@dcz.at>2026-05-07 14:33:19 +0200
committerDavid Czihak <git@dcz.at>2026-05-07 14:33:19 +0200
commitddf2de739068b5ff0866ccb1d067f3cb53a4fc55 (patch)
tree1a77efe9d73a6172be3c37d29b321eadd4efe379 /examples/hello-zig
Initial commitv0.1.7
Diffstat (limited to 'examples/hello-zig')
-rw-r--r--examples/hello-zig/.editorconfig6
-rw-r--r--examples/hello-zig/.gitignore2
-rw-r--r--examples/hello-zig/.nova/Configuration.json8
-rw-r--r--examples/hello-zig/.nova/Tasks/Zig Debug.json12
-rw-r--r--examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json7
-rw-r--r--examples/hello-zig/.nova/Tasks/Zig Package.json8
-rw-r--r--examples/hello-zig/build.zig47
-rw-r--r--examples/hello-zig/build.zig.zon12
-rw-r--r--examples/hello-zig/src/main.zig32
-rw-r--r--examples/hello-zig/src/root.zig83
10 files changed, 217 insertions, 0 deletions
diff --git a/examples/hello-zig/.editorconfig b/examples/hello-zig/.editorconfig
new file mode 100644
index 0000000..d5f8c65
--- /dev/null
+++ b/examples/hello-zig/.editorconfig
@@ -0,0 +1,6 @@
1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6insert_final_newline = true
diff --git a/examples/hello-zig/.gitignore b/examples/hello-zig/.gitignore
new file mode 100644
index 0000000..3389c86
--- /dev/null
+++ b/examples/hello-zig/.gitignore
@@ -0,0 +1,2 @@
1.zig-cache/
2zig-out/
diff --git a/examples/hello-zig/.nova/Configuration.json b/examples/hello-zig/.nova/Configuration.json
new file mode 100644
index 0000000..47e341a
--- /dev/null
+++ b/examples/hello-zig/.nova/Configuration.json
@@ -0,0 +1,8 @@
1{
2 "at.dcz.nova-zig.toolchain.lldb-dap-path" : "\/Library\/Developer\/CommandLineTools\/usr\/bin\/lldb-dap",
3 "at.dcz.nova-zig.toolchain.zig-path" : "\/opt\/homebrew\/bin\/zig",
4 "at.dcz.nova-zig.zls.build-on-save" : true,
5 "at.dcz.nova-zig.zls.enabled" : true,
6 "zls.enable_build_on_save" : true,
7 "zls.zig_exe_path" : "\/opt\/homebrew\/bin\/zig"
8}
diff --git a/examples/hello-zig/.nova/Tasks/Zig Debug.json b/examples/hello-zig/.nova/Tasks/Zig Debug.json
new file mode 100644
index 0000000..53b7f13
--- /dev/null
+++ b/examples/hello-zig/.nova/Tasks/Zig Debug.json
@@ -0,0 +1,12 @@
1{
2 "extension" : {
3 "identifier" : "at.dcz.nova-zig",
4 "name" : "Zig"
5 },
6 "extensionTemplate" : "zigDebug",
7 "extensionValues" : {
8 "console" : "internalConsole",
9 "programPath" : "zig-out\/bin\/hello-zig"
10 },
11 "openLogOnRun" : "start"
12}
diff --git a/examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json b/examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json
new file mode 100644
index 0000000..d1fa04d
--- /dev/null
+++ b/examples/hello-zig/.nova/Tasks/Zig Package (macOS Terminal).json
@@ -0,0 +1,7 @@
1{
2 "extension" : {
3 "identifier" : "at.dcz.nova-zig",
4 "name" : "Zig"
5 },
6 "extensionTemplate" : "zigBuildRunTerminal"
7}
diff --git a/examples/hello-zig/.nova/Tasks/Zig Package.json b/examples/hello-zig/.nova/Tasks/Zig Package.json
new file mode 100644
index 0000000..10830e6
--- /dev/null
+++ b/examples/hello-zig/.nova/Tasks/Zig Package.json
@@ -0,0 +1,8 @@
1{
2 "buildBeforeRunning" : true,
3 "extension" : {
4 "identifier" : "at.dcz.nova-zig",
5 "name" : "Zig"
6 },
7 "extensionTemplate" : "zigBuildRun"
8}
diff --git a/examples/hello-zig/build.zig b/examples/hello-zig/build.zig
new file mode 100644
index 0000000..5c56fb0
--- /dev/null
+++ b/examples/hello-zig/build.zig
@@ -0,0 +1,47 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const target = b.standardTargetOptions(.{});
5 const optimize = b.standardOptimizeOption(.{});
6
7 const lib_mod = b.addModule("hello_zig", .{
8 .root_source_file = b.path("src/root.zig"),
9 .target = target,
10 });
11
12 const exe = b.addExecutable(.{
13 .name = "hello-zig",
14 .root_module = b.createModule(.{
15 .root_source_file = b.path("src/main.zig"),
16 .target = target,
17 .optimize = optimize,
18 .imports = &.{
19 .{ .name = "hello_zig", .module = lib_mod },
20 },
21 }),
22 });
23
24 b.installArtifact(exe);
25
26 const run_step = b.step("run", "Run the sample app");
27 const run_cmd = b.addRunArtifact(exe);
28 run_step.dependOn(&run_cmd.step);
29
30 if (b.args) |args| {
31 run_cmd.addArgs(args);
32 }
33
34 const lib_tests = b.addTest(.{
35 .root_module = lib_mod,
36 });
37 const exe_tests = b.addTest(.{
38 .root_module = exe.root_module,
39 });
40
41 const run_lib_tests = b.addRunArtifact(lib_tests);
42 const run_exe_tests = b.addRunArtifact(exe_tests);
43
44 const test_step = b.step("test", "Run the sample tests");
45 test_step.dependOn(&run_lib_tests.step);
46 test_step.dependOn(&run_exe_tests.step);
47}
diff --git a/examples/hello-zig/build.zig.zon b/examples/hello-zig/build.zig.zon
new file mode 100644
index 0000000..09f7da2
--- /dev/null
+++ b/examples/hello-zig/build.zig.zon
@@ -0,0 +1,12 @@
1.{
2 .name = .hello_zig,
3 .version = "0.1.0",
4 .fingerprint = 0x1c1a468675f426fe, // Changing this has security and trust implications.
5 .minimum_zig_version = "0.15.2",
6 .dependencies = .{},
7 .paths = .{
8 "build.zig",
9 "build.zig.zon",
10 "src",
11 },
12}
diff --git a/examples/hello-zig/src/main.zig b/examples/hello-zig/src/main.zig
new file mode 100644
index 0000000..74eb590
--- /dev/null
+++ b/examples/hello-zig/src/main.zig
@@ -0,0 +1,32 @@
1const std = @import("std");
2const hello_zig = @import("hello_zig");
3
4pub fn main() !void {
5 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
6 defer arena.deinit();
7
8 const allocator = arena.allocator();
9 const args = try std.process.argsAlloc(allocator);
10 const name = if (args.len > 1) args[1] else "Nova";
11
12 var greeter = hello_zig.Greeter{
13 .name = name,
14 .punctuation = '!',
15 };
16
17 const stdout = std.fs.File.stdout().deprecatedWriter();
18 try greeter.write(stdout, .verbose);
19
20 const sample_values = [_]i32{ -3, -2, 0, 1, 2, 3, 4 };
21 var squares = try hello_zig.collectEvenSquares(allocator, &sample_values);
22 defer squares.deinit(allocator);
23
24 std.debug.print("even squares: {any}\n", .{squares.items});
25}
26
27test "main module can compute a summary" {
28 const greeting = try hello_zig.describeNumber(std.testing.allocator, 42);
29 defer std.testing.allocator.free(greeting);
30
31 try std.testing.expect(std.mem.startsWith(u8, greeting, "positive"));
32}
diff --git a/examples/hello-zig/src/root.zig b/examples/hello-zig/src/root.zig
new file mode 100644
index 0000000..f6600ac
--- /dev/null
+++ b/examples/hello-zig/src/root.zig
@@ -0,0 +1,83 @@
1const std = @import("std");
2
3pub const OutputStyle = enum {
4 compact,
5 verbose,
6};
7
8pub const NumberKind = union(enum) {
9 negative: i64,
10 zero,
11 positive: u64,
12};
13
14pub const Greeter = struct {
15 name: []const u8,
16 punctuation: u8 = '!',
17
18 pub fn write(self: Greeter, writer: anytype, style: OutputStyle) !void {
19 switch (style) {
20 .compact => try writer.print("hello, {s}{c}\n", .{ self.name, self.punctuation }),
21 .verbose => {
22 try writer.print(
23 \\hello, {s}{c}
24 \\this sample is here to exercise Zig syntax support in Nova.
25 \\
26 , .{ self.name, self.punctuation });
27 },
28 }
29 }
30};
31
32pub fn classifyNumber(value: i64) NumberKind {
33 if (value == 0) return .zero;
34 if (value < 0) return .{ .negative = value };
35 return .{ .positive = @intCast(value) };
36}
37
38pub fn describeNumber(allocator: std.mem.Allocator, value: i64) ![]u8 {
39 return switch (classifyNumber(value)) {
40 .zero => std.fmt.allocPrint(allocator, "zero", .{}),
41 .negative => |negative| std.fmt.allocPrint(allocator, "negative({d})", .{negative}),
42 .positive => |positive| std.fmt.allocPrint(allocator, "positive({d})", .{positive}),
43 };
44}
45
46pub fn collectEvenSquares(
47 allocator: std.mem.Allocator,
48 values: []const i32,
49) !std.ArrayList(i32) {
50 var result: std.ArrayList(i32) = .empty;
51 errdefer result.deinit(allocator);
52
53 for (values) |value| {
54 if (@mod(value, 2) != 0) continue;
55 try result.append(allocator, value * value);
56 }
57
58 return result;
59}
60
61test "classifyNumber covers the main branches" {
62 try std.testing.expectEqual(NumberKind{ .negative = -5 }, classifyNumber(-5));
63 try std.testing.expectEqual(NumberKind.zero, classifyNumber(0));
64 try std.testing.expectEqual(NumberKind{ .positive = 9 }, classifyNumber(9));
65}
66
67test "collectEvenSquares keeps only even numbers" {
68 const values = [_]i32{ -4, -3, 0, 1, 2, 7 };
69 var result = try collectEvenSquares(std.testing.allocator, &values);
70 defer result.deinit(std.testing.allocator);
71
72 try std.testing.expectEqualSlices(i32, &.{ 16, 0, 4 }, result.items);
73}
74
75test "Greeter.write renders verbose output" {
76 var list: std.ArrayList(u8) = .empty;
77 defer list.deinit(std.testing.allocator);
78
79 const writer = list.writer(std.testing.allocator);
80 try (Greeter{ .name = "Zig" }).write(writer, .verbose);
81
82 try std.testing.expect(std.mem.indexOf(u8, list.items, "exercise Zig syntax support") != null);
83}