Hey everyone!
I am writing an interpreter from scratch in Zig. I implemented a REPL as a way to interact with it. As a learning exercise, I wanted to incorporate a the GNU’s readline library as a way to get experience working with a C library and it has the happy side effect of improving the REPL.
Since my OS is Linux Mint, I simply installed the readline library with apt install readline
and imported it into my interpreter with exe.linkSystemLibrary("readline")
. So far, so good!
I wanted to challenge myself further and vendor the library so my (hypothetical) users won’t have to install a system library in order to build and use the interpreter. After watching a stream from Tsoding, I found a smaller drop-in replacement called editline
. Here’s a link its repo.
I chose to vendor editline
instead of readline
because editline
is in a GitHub repo that’s trivial to fork and editline
has far fewer files and lines of code.
Edit: here’s the link to my forked repo.
Here’s my first stab at getting things started:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib_mod = b.createModule(.{
.root_source_file = null,
.target = target,
.optimize = optimize,
.link_libc = true,
});
lib_mod.addCSourceFiles(
.{
.files = &.{
"src/editline.c",
"src/complete.c",
"src/sysos9.c",
"src/sysunix.c",
},
},
);
lib_mod.addIncludePath(b.path("src"));
const lib = b.addLibrary(.{
.linkage = .static,
.name = "editline",
.root_module = lib_mod,
});
lib.installHeader(b.path("config.h"), "config.h");
lib.installHeader(b.path("src/editline.h"), "editline.h");
lib.installHeader(b.path("src/unix.h"), "unix.h");
lib.installHeader(b.path("src/os9.h"), "os9.h");
b.installArtifact(lib);
}
Naturally, I hit a few stags off the bat. This build script gives these errors:
install
└─ install editline
└─ zig build-lib editline Debug native 4 errors
/home/theshinx317/Coding/C/editline/src/editline.h:26:10: error: 'config.h' file not found
#include <config.h>
^~~~~~~~~~~
/home/theshinx317/Coding/C/editline/src/sysos9.c:22:10: note: in file included from /home/theshinx317/Coding/C/editline/src/sysos9.c:22:
#include "editline.h"
^
/home/theshinx317/Coding/C/editline/src/editline.h:26:10: error: 'config.h' file not found
#include <config.h>
^~~~~~~~~~~
/home/theshinx317/Coding/C/editline/src/sysunix.c:24:10: note: in file included from /home/theshinx317/Coding/C/editline/src/sysunix.c:24:
#include "editline.h"
^
/home/theshinx317/Coding/C/editline/src/editline.h:26:10: error: 'config.h' file not found
#include <config.h>
^~~~~~~~~~~
/home/theshinx317/Coding/C/editline/src/complete.c:24:10: note: in file included from /home/theshinx317/Coding/C/editline/src/complete.c:24:
#include "editline.h"
^
/home/theshinx317/Coding/C/editline/src/editline.h:26:10: error: 'config.h' file not found
#include <config.h>
^~~~~~~~~~~
/home/theshinx317/Coding/C/editline/src/editline.c:27:10: note: in file included from /home/theshinx317/Coding/C/editline/src/editline.c:27:
#include "editline.h"
^
error: warning(compilation): failed to delete '/home/theshinx317/Coding/C/editline/.zig-cache/tmp/2060cee100fc8a98-sysos9.o.d': FileNotFound
warning(compilation): failed to delete '/home/theshinx317/Coding/C/editline/.zig-cache/tmp/95524cbf70fffc00-sysunix.o.d': FileNotFound
warning(compilation): failed to delete '/home/theshinx317/Coding/C/editline/.zig-cache/tmp/f9d6e30e75fe34a6-complete.o.d': FileNotFound
warning(compilation): failed to delete '/home/theshinx317/Coding/C/editline/.zig-cache/tmp/d0691e5a9a830392-editline.o.d': FileNotFound