commit 7461681defecd82d22b351663ba98c759f1abdef
parent 6ac2b3226fa8fb6d6cdf2ae3d59cdfe6d5b2b79c
Author: Szymon Mikulicz <szymon.mikulicz@aptiv.com>
Date: Sat, 20 Apr 2024 14:28:53 +0200
Remove comptime trie (broken), use zon for linenoise
Diffstat:
5 files changed, 46 insertions(+), 73 deletions(-)
diff --git a/zlox/build.zig b/zlox/build.zig
@@ -52,8 +52,15 @@ pub fn build(b: *std.Build) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
+ const linenoise = b.dependency("linenoise", .{
+ .target = target,
+ .optimize = optimize,
+ });
+
+ exe.addCSourceFile(.{ .file = linenoise.path("linenoise.c") });
+ exe.addIncludePath(linenoise.path(""));
+
exe.linkLibC();
- exe.linkSystemLibrary("linenoise");
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
diff --git a/zlox/build.zig.zon b/zlox/build.zig.zon
@@ -0,0 +1,36 @@
+.{
+ .name = "zlox",
+ // This is a [Semantic Version](https://semver.org/).
+ // In a future version of Zig it will be used for package deduplication.
+ .version = "0.0.0",
+
+ // This field is optional.
+ // This is currently advisory only; Zig does not yet do anything
+ // with this value.
+ //.minimum_zig_version = "0.11.0",
+
+ // This field is optional.
+ // Each dependency must either provide a `url` and `hash`, or a `path`.
+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
+ // Once all dependencies are fetched, `zig build` no longer requires
+ // internet connectivity.
+ .dependencies = .{
+ .linenoise = .{
+ .url = "https://github.com/antirez/linenoise/archive/refs/heads/master.tar.gz",
+ .hash = "1220f1e9b8ae57a1ed3c1110f6ccfbcbef26598a99ef225d68e14a863a35355f7abe",
+ },
+ },
+ .paths = .{
+ // This makes *all* files, recursively, included in this package. It is generally
+ // better to explicitly list the files and directories instead, to insure that
+ // fetching from tarballs, file system paths, and version control all result
+ // in the same contents hash.
+ "",
+ // For example...
+ //"build.zig",
+ //"build.zig.zon",
+ //"src",
+ //"LICENSE",
+ //"README.md",
+ },
+}
diff --git a/zlox/src/obj.zig b/zlox/src/obj.zig
@@ -152,7 +152,7 @@ pub const Obj = packed struct {
}
};
- pub const Type = enum {
+ pub const Type = enum(u8) {
String,
pub fn get(comptime self: @This()) type {
diff --git a/zlox/src/scanner.zig b/zlox/src/scanner.zig
@@ -1,5 +1,4 @@
const std = @import("std");
-const trie = @import("trie.zig");
pub const TokenType = enum {
LEFT_PAREN,
@@ -70,7 +69,7 @@ pub const Token = struct {
};
pub const Scanner = struct {
- const identifiers = trie.LowercaseTrieTable(TokenType, .{
+ const identifiers = std.ComptimeStringMap(TokenType, .{
.{ "and", TokenType.AND },
.{ "class", TokenType.CLASS },
.{ "else", TokenType.ELSE },
diff --git a/zlox/src/trie.zig b/zlox/src/trie.zig
@@ -1,69 +0,0 @@
-const std = @import("std");
-
-pub fn TrieTable(comptime Key: type, comptime Value: type, size: comptime_int, get_idx: fn (Key) usize, comptime list: anytype) type {
- const TrieLeaf = struct {
- value: ?Value,
- data: [size]?*@This(),
- };
-
- const max_len = comptime blk: {
- var ret = 0;
- for (list) |el| {
- ret += el.@"0".len;
- }
- break :blk ret;
- };
-
- const precomputed = comptime blk: {
- var allocated = [_]TrieLeaf{TrieLeaf{ .value = null, .data = [_]?*TrieLeaf{null} ** size }} ** max_len;
- var allocated_i = 0;
- var tip = TrieLeaf{ .value = null, .data = [_]?*TrieLeaf{null} ** size };
-
- for (list) |el| {
- var leaf = &tip;
- for (el.@"0") |key| {
- const idx = get_idx(key);
- std.debug.assert(idx >= 0 and idx < size);
- if (leaf.data[idx]) |val| {
- leaf = val;
- } else {
- var new = &allocated[allocated_i];
- allocated_i += 1;
- new.value = null;
- new.data = [_]?*TrieLeaf{null} ** size;
- leaf.data[idx] = new;
- leaf = new;
- }
- }
- leaf.value = el.@"1";
- }
- break :blk .{ .allocated = allocated[0..allocated_i], .tip = tip };
- };
-
- return struct {
- const allocated = precomputed.allocated;
- const tip = precomputed.tip;
-
- pub fn get(word: []const Key) ?Value {
- var this = &tip;
- for (word) |key| {
- const idx = get_idx(key);
- if (idx < 0 or idx >= size) return null;
- if (this.data[idx]) |val| {
- this = val;
- } else {
- return null;
- }
- }
- return this.value;
- }
- };
-}
-
-pub fn LowercaseTrieTable(comptime Value: type, comptime list: anytype) type {
- return TrieTable(u8, Value, 26, struct {
- pub fn idx(c: u8) usize {
- return c - 'a';
- }
- }.idx, list);
-}