feat: structure project as a zig library
This commit is contained in:
parent
cf75229fb0
commit
2e231b8ffb
52
build.zig
52
build.zig
|
@ -15,55 +15,33 @@ pub fn build(b: *std.Build) void {
|
||||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const lib = b.addStaticLibrary(.{
|
||||||
.name = "hamt",
|
.name = "hamt",
|
||||||
// In this case the main source file is merely a path, however, in more
|
// In this case the main source file is merely a path, however, in more
|
||||||
// complicated build scripts, this could be a generated file.
|
// 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,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
// This declares intent for the executable to be installed into the
|
// This declares intent for the library to be installed into the standard
|
||||||
// standard location when the user invokes the "install" step (the default
|
// location when the user invokes the "install" step (the default step when
|
||||||
// step when running `zig build`).
|
// running `zig build`).
|
||||||
b.installArtifact(exe);
|
b.installArtifact(lib);
|
||||||
|
|
||||||
// This *creates* a RunStep in the build graph, to be executed when another
|
// Creates a step for unit testing. This only builds the test executable
|
||||||
// step is evaluated that depends on it. The next line below will establish
|
// but does not run it.
|
||||||
// such a dependency.
|
const main_tests = b.addTest(.{
|
||||||
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(.{
|
|
||||||
.root_source_file = .{ .path = "src/tests.zig" },
|
.root_source_file = .{ .path = "src/tests.zig" },
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.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
|
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||||
// the `zig build --help` menu, providing a way for the user to request
|
// and can be selected like this: `zig build test`
|
||||||
// running the unit tests.
|
// This will evaluate the `test` step rather than the default, which is "install".
|
||||||
const test_step = b.step("test", "Run unit tests");
|
const test_step = b.step("test", "Run library tests");
|
||||||
test_step.dependOn(&run_unit_tests.step);
|
test_step.dependOn(&run_main_tests.step);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
pub const HashArrayMappedTrie = @import("trie.zig").HashArrayMappedTrie;
|
53
src/main.zig
53
src/main.zig
|
@ -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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -1,4 +1,5 @@
|
||||||
comptime {
|
comptime {
|
||||||
|
_ = @import("lib.zig");
|
||||||
_ = @import("trie.zig");
|
_ = @import("trie.zig");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue