Skip to content

Commit

Permalink
zig: build with 0.13.0, merge eval_ast, fix remaining issues
Browse files Browse the repository at this point in the history
Update build system, syntax and library calls for zig 0.13.0.

Rewrite the build system so that the steps can build separately.  Drop
intermediate symbolic links (unneeded complexity, confusing
timestamps).

Build with debugging options, this is a toy project full of memory
leaks.

Declare the allocators as global variables instead of passing and/or
storing always the same reference everywhere for no benefit.

Make apply_function a global variable instead of adding a reference to
EVAL in each function.

Pass arguments as a slice instead of using a different type for each
argument count.

There is no point in renaming default errors.

Remove a lot of reference counting and some indirection levels.
This fixes the current segmentation faults.

Create each object as soon as possible and use errdefer so that it is
deallocated if an exception occurs when computing its elemements.

Use a global variable to convey a MAL object alongside a thrown error.

Remove the unused logging_alloc module (but add a debug_alloc boolean
in types.zig).
  • Loading branch information
asarhaddon committed Sep 18, 2024
1 parent fbee4ec commit 335430d
Show file tree
Hide file tree
Showing 27 changed files with 3,692 additions and 4,272 deletions.
19 changes: 6 additions & 13 deletions impls/zig/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:bionic
FROM ubuntu:24.04
MAINTAINER Joel Martin <[email protected]>

##########################################################
Expand All @@ -9,25 +9,18 @@ MAINTAINER Joel Martin <[email protected]>
RUN apt-get -y update

# Required for running tests
RUN apt-get -y install make python

# Some typical implementation and test requirements
RUN apt-get -y install curl libreadline-dev libedit-dev libpcre3-dev
RUN apt-get -y install make python3
RUN ln -fs /usr/bin/python3 /usr/local/bin/python

RUN mkdir -p /mal
WORKDIR /mal

##########################################################
# Specific implementation requirements
##########################################################
RUN apt-get -y install ca-certificates curl gcc libc6-dev libpcre3-dev libreadline-dev xz-utils

RUN apt-get -y install gcc gdc ldc gpg wget

RUN wget https://ziglang.org/download/0.5.0/zig-linux-x86_64-0.5.0.tar.xz && \
echo `pwd` && \
tar -xf zig-linux-x86_64-0.5.0.tar.xz && \
cp -r zig-linux-x86_64-0.5.0 /usr/local/bin && \
ln -sf /usr/local/bin/zig-linux-x86_64-0.5.0/zig /usr/local/bin/zig && \
chmod +x /usr/local/bin/zig
RUN curl https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz | tar -xJC/mal
RUN ln -fst/usr/local/bin /mal/zig-linux-x86_64-0.13.0/zig

ENV HOME /mal
15 changes: 7 additions & 8 deletions impls/zig/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ STEPS = step0_repl step1_read_print step2_eval step3_env step4_if_fn_do step5_tc

all: $(STEPS)

dist: mal
zig_opts += --release=safe
zig_opts += -Doptimize=Debug
$(STEPS):
zig build $(zig_opts) -Dname=$@ -Droot_source_file=$@.zig


%: %.zig
zig build -Drelease-fast=true
ln -sf zig-cache/bin/$* .

.PHONY: clean
.PHONY: all $(STEPS) clean

clean:
rm -f $(STEPS)
rm -fr .zig-cache/ zig-out/
rm -f *~
24 changes: 24 additions & 0 deletions impls/zig/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
debug_alloc in types.zig may help with reference counting.


TODO Simplify the printer with the new reader functions in the zig
library.


NOTE Before implementing any optimization or optional fix that would
increase the complexity, please take into account that someone has to
maintain the code, and the zig language evolves quickly.

Some memory leaks are probably already present, especially when an
error interrupts the normal execution flow.

Examples of things that are deliberately not implemented...
* TCO for try*
* preallocate integers between 0 and 100 at startup
* use ArrayList.ensureTotalCapacityPrecise/HashMap.ensureTotalCapacity
after most calls to new_list/vector/map.
* store symbols in a global hash map,
* implement lists/vectors as slices/cons cells/whatever
* deallocate cyclic structures not detected by reference counting like
(let* (f (fn* () nil)))
(def! a (atom 2)) (def! v [a]) (reset! a v)
51 changes: 21 additions & 30 deletions impls/zig/build.zig
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
const LibExeObjStep = @import("std").build.LibExeObjStep;
const Builder = @import("std").build.Builder;
const builtin = @import("builtin");

const warn = @import("std").debug.warn;
const Builder = @import("std").Build;

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"),
};
// Two options select the built step.

const name = b.option([]const u8, "name", "step name (without .zig)")
orelse "stepA_mal";

const root_source_file = b.path(
b.option([]const u8, "root_source_file", "step name (with .zig)")
orelse "stepA_mal.zig");

const exe = b.addExecutable(.{
.name = name,
.root_source_file = root_source_file,
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});

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);
}
exe.linkSystemLibrary("c");
exe.linkSystemLibrary("pcre");
exe.linkSystemLibrary("readline");
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
}
Loading

0 comments on commit 335430d

Please sign in to comment.