feat: structure project as a zig library

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-04-17 22:30:14 -05:00
parent cf75229fb0
commit 2e231b8ffb
4 changed files with 17 additions and 90 deletions

View File

@ -15,55 +15,33 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
const lib = b.addStaticLibrary(.{
.name = "hamt",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = .{ .path = "src/lib.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
// This *creates* a RunStep in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing.
const unit_tests = b.addTest(.{
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/tests.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
const run_main_tests = b.addRunArtifact(main_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}

1
src/lib.zig Normal file
View File

@ -0,0 +1 @@
pub const HashArrayMappedTrie = @import("trie.zig").HashArrayMappedTrie;

View File

@ -1,53 +0,0 @@
const std = @import("std");
const HashArrayMappedTrie = @import("trie.zig").HashArrayMappedTrie;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
const allocator = gpa.allocator();
var trie = HashArrayMappedTrie([]const u8, void, Context(u32)).init();
defer trie.deinit(allocator);
try trie.insert(allocator, "and", {});
try trie.insert(allocator, "class", {});
try trie.insert(allocator, "else", {});
try trie.insert(allocator, "false", {});
try trie.insert(allocator, "for", {});
try trie.insert(allocator, "fun", {});
try trie.insert(allocator, "if", {});
try trie.insert(allocator, "nil", {});
try trie.insert(allocator, "or", {});
try trie.insert(allocator, "print", {});
try trie.insert(allocator, "return", {});
try trie.insert(allocator, "super", {});
try trie.insert(allocator, "this", {});
try trie.insert(allocator, "true", {});
try trie.insert(allocator, "var", {});
try trie.insert(allocator, "while", {});
try trie.print();
}
pub fn Context(comptime HashCode: type) type {
const Log2Int = std.math.Log2Int;
return struct {
pub const Digest = HashCode;
pub inline fn hash(key: []const u8) Digest {
// the MSB will represent 'z'
const offset = @typeInfo(Digest).Int.bits - 26;
var result: Digest = 0;
for (key) |c| result |= @as(Digest, 1) << @intCast(Log2Int(Digest), offset + c - 'a');
return result;
}
pub inline fn eql(left: []const u8, right: []const u8) bool {
return std.mem.eql(u8, left, right);
}
};
}

View File

@ -1,4 +1,5 @@
comptime {
_ = @import("lib.zig");
_ = @import("trie.zig");
}