zig: adds missing build script and utility
authorJosh Tobin <tobinrj@tcd.ie>
Sat, 14 Dec 2019 04:49:33 +0000 (23:49 -0500)
committerJosh Tobin <tobinrj@tcd.ie>
Sat, 14 Dec 2019 04:52:28 +0000 (23:52 -0500)
zig/build.zig [new file with mode: 0644]
zig/logging_alloc.zig [new file with mode: 0644]

diff --git a/zig/build.zig b/zig/build.zig
new file mode 100644 (file)
index 0000000..4154e1c
--- /dev/null
@@ -0,0 +1,35 @@
+const LibExeObjStep = @import("std").build.LibExeObjStep;
+const Builder = @import("std").build.Builder;
+const builtin = @import("builtin");
+
+const warn = @import("std").debug.warn;
+
+pub fn build(b: *Builder) void {
+    const mode = b.standardReleaseOptions();
+
+    const exes = [_] *LibExeObjStep {
+        b.addExecutable("step0_repl",       "step0_repl.zig"),
+        b.addExecutable("step1_read_print", "step1_read_print.zig"),
+        b.addExecutable("step2_eval",       "step2_eval.zig"),
+        b.addExecutable("step3_env",        "step3_env.zig"),
+        b.addExecutable("step4_if_fn_do",   "step4_if_fn_do.zig"),
+        b.addExecutable("step5_tco",        "step5_tco.zig"),
+        b.addExecutable("step6_file",       "step6_file.zig"),
+        b.addExecutable("step7_quote",      "step7_quote.zig"),
+        b.addExecutable("step8_macros",     "step8_macros.zig"),
+        b.addExecutable("step9_try",        "step9_try.zig"),
+        b.addExecutable("stepA_mal",        "stepA_mal.zig"),
+    };
+
+    for(exes) |exe| {
+        exe.setBuildMode(mode);
+        exe.linkSystemLibrary("c");
+        exe.linkSystemLibrary("pcre");
+        exe.linkSystemLibrary("readline");
+        const run_cmd = exe.run();
+        const step = b.step(exe.name, exe.name);
+        step.dependOn(&run_cmd.step);
+        b.default_step.dependOn(&exe.step);
+        b.installArtifact(exe);
+    }
+}
diff --git a/zig/logging_alloc.zig b/zig/logging_alloc.zig
new file mode 100644 (file)
index 0000000..7917421
--- /dev/null
@@ -0,0 +1,35 @@
+const std = @import("std");
+const Allocator = std.mem.Allocator;
+const warn = @import("std").debug.warn;
+
+pub const LoggingAllocator = struct {
+    allocator: Allocator,
+    parent_allocator: *Allocator,
+
+    const Self = @This();
+
+    pub fn init(parent_allocator: *Allocator) Self {
+        return Self {
+            .allocator = Allocator{
+                .reallocFn = realloc,
+                .shrinkFn = shrink,
+            },
+            .parent_allocator = parent_allocator,
+        };
+    }
+
+    fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
+        warn("mem new {} {} {}\n", old_mem.len, new_size, @intCast(i64,new_size) - @intCast(i64, old_mem.len));
+        const self = @fieldParentPtr(Self, "allocator", allocator);
+        const result = self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
+        return result;
+    }
+
+    fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
+        warn("mem del {} {} {}\n", old_mem.len, new_size, @intCast(i64,new_size) - @intCast(i64,old_mem.len));
+        warn("deleted: {}\n", old_mem);
+        const self = @fieldParentPtr(Self, "allocator", allocator);
+        const result = self.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
+        return result;
+    }
+};