diff options
Diffstat (limited to 'examples/hello-zig/src')
| -rw-r--r-- | examples/hello-zig/src/main.zig | 32 | ||||
| -rw-r--r-- | examples/hello-zig/src/root.zig | 83 |
2 files changed, 115 insertions, 0 deletions
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const hello_zig = @import("hello_zig"); | ||
| 3 | |||
| 4 | pub 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 | |||
| 27 | test "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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | pub const OutputStyle = enum { | ||
| 4 | compact, | ||
| 5 | verbose, | ||
| 6 | }; | ||
| 7 | |||
| 8 | pub const NumberKind = union(enum) { | ||
| 9 | negative: i64, | ||
| 10 | zero, | ||
| 11 | positive: u64, | ||
| 12 | }; | ||
| 13 | |||
| 14 | pub 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 | |||
| 32 | pub 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 | |||
| 38 | pub 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 | |||
| 46 | pub 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 | |||
| 61 | test "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 | |||
| 67 | test "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 | |||
| 75 | test "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 | } | ||
