Select the types of activity you want to include in your feed.
Add skeleton code structure
Scaffold the basic modules for the program, and clean up unnecessary zig-init template code. Pretty sketchy, but it's late and I don't have much time tonight so it will have to do.
···11const std = @import("std");
2233-// Although this function looks imperative, it does not perform the build
44-// directly and instead it mutates the build graph (`b`) that will be then
55-// executed by an external runner. The functions in `std.Build` implement a DSL
66-// for defining build steps and express dependencies between them, allowing the
77-// build runner to parallelize the build automatically (and the cache system to
88-// know when a step doesn't need to be re-run).
93pub fn build(b: *std.Build) void {
1010- // Standard target options allow the person running `zig build` to choose
1111- // what target to build for. Here we do not override the defaults, which
1212- // means any target is allowed, and the default is native. Other options
1313- // for restricting supported target set are available.
144 const target = b.standardTargetOptions(.{});
1515- // Standard optimization options allow the person running `zig build` to select
1616- // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
1717- // set a preferred release mode, allowing the user to decide how to optimize.
185 const optimize = b.standardOptimizeOption(.{});
1919- // It's also possible to define more custom flags to toggle optional features
2020- // of this build script using `b.option()`. All defined flags (including
2121- // target and optimize options) will be listed when running `zig build --help`
2222- // in this directory.
2362424- // This creates a module, which represents a collection of source files alongside
2525- // some compilation options, such as optimization mode and linked system libraries.
2626- // Zig modules are the preferred way of making Zig code available to consumers.
2727- // addModule defines a module that we intend to make available for importing
2828- // to our consumers. We must give it a name because a Zig package can expose
2929- // multiple modules and consumers will need to be able to specify which
3030- // module they want to access.
317 const mod = b.addModule("rat_lisp", .{
3232- // The root source file is the "entry point" of this module. Users of
3333- // this module will only be able to access public declarations contained
3434- // in this file, which means that if you have declarations that you
3535- // intend to expose to consumers that were defined in other files part
3636- // of this module, you will have to make sure to re-export them from
3737- // the root file.
388 .root_source_file = b.path("src/root.zig"),
3939- // Later on we'll use this module as the root module of a test executable
4040- // which requires us to specify a target.
419 .target = target,
4210 });
43114444- // Here we define an executable. An executable needs to have a root module
4545- // which needs to expose a `main` function. While we could add a main function
4646- // to the module defined above, it's sometimes preferable to split business
4747- // logic and the CLI into two separate modules.
4848- //
4949- // If your goal is to create a Zig library for others to use, consider if
5050- // it might benefit from also exposing a CLI tool. A parser library for a
5151- // data serialization format could also bundle a CLI syntax checker, for example.
5252- //
5353- // If instead your goal is to create an executable, consider if users might
5454- // be interested in also being able to embed the core functionality of your
5555- // program in their own executable in order to avoid the overhead involved in
5656- // subprocessing your CLI tool.
5757- //
5858- // If neither case applies to you, feel free to delete the declaration you
5959- // don't need and to put everything under a single module.
6012 const exe = b.addExecutable(.{
6113 .name = "rat_lisp",
6214 .root_module = b.createModule(.{
6363- // b.createModule defines a new module just like b.addModule but,
6464- // unlike b.addModule, it does not expose the module to consumers of
6565- // this package, which is why in this case we don't have to give it a name.
6615 .root_source_file = b.path("src/main.zig"),
6767- // Target and optimization levels must be explicitly wired in when
6868- // defining an executable or library (in the root module), and you
6969- // can also hardcode a specific target for an executable or library
7070- // definition if desireable (e.g. firmware for embedded devices).
7116 .target = target,
7217 .optimize = optimize,
7373- // List of modules available for import in source files part of the
7474- // root module.
7518 .imports = &.{
7676- // Here "rat_lisp" is the name you will use in your source code to
7777- // import this module (e.g. `@import("rat_lisp")`). The name is
7878- // repeated because you are allowed to rename your imports, which
7979- // can be extremely useful in case of collisions (which can happen
8080- // importing modules from different packages).
8119 .{ .name = "rat_lisp", .module = mod },
8220 },
8321 }),
8422 });
85238686- // This declares intent for the executable to be installed into the
8787- // install prefix when running `zig build` (i.e. when executing the default
8888- // step). By default the install prefix is `zig-out/` but can be overridden
8989- // by passing `--prefix` or `-p`.
9024 b.installArtifact(exe);
91259292- // This creates a top level step. Top level steps have a name and can be
9393- // invoked by name when running `zig build` (e.g. `zig build run`).
9494- // This will evaluate the `run` step rather than the default step.
9595- // For a top level step to actually do something, it must depend on other
9696- // steps (e.g. a Run step, as we will see in a moment).
9726 const run_step = b.step("run", "Run the app");
98279999- // This creates a RunArtifact step in the build graph. A RunArtifact step
100100- // invokes an executable compiled by Zig. Steps will only be executed by the
101101- // runner if invoked directly by the user (in the case of top level steps)
102102- // or if another step depends on it, so it's up to you to define when and
103103- // how this Run step will be executed. In our case we want to run it when
104104- // the user runs `zig build run`, so we create a dependency link.
10528 const run_cmd = b.addRunArtifact(exe);
10629 run_step.dependOn(&run_cmd.step);
10730108108- // By making the run step depend on the default step, it will be run from the
109109- // installation directory rather than directly from within the cache directory.
11031 run_cmd.step.dependOn(b.getInstallStep());
11132112112- // This allows the user to pass arguments to the application in the build
113113- // command itself, like this: `zig build run -- arg1 arg2 etc`
11433 if (b.args) |args| {
11534 run_cmd.addArgs(args);
11635 }
11736118118- // Creates an executable that will run `test` blocks from the provided module.
119119- // Here `mod` needs to define a target, which is why earlier we made sure to
120120- // set the releative field.
12137 const mod_tests = b.addTest(.{
12238 .root_module = mod,
12339 });
12440125125- // A run step that will run the test executable.
12641 const run_mod_tests = b.addRunArtifact(mod_tests);
12742128128- // Creates an executable that will run `test` blocks from the executable's
129129- // root module. Note that test executables only test one module at a time,
130130- // hence why we have to create two separate ones.
13143 const exe_tests = b.addTest(.{
13244 .root_module = exe.root_module,
13345 });
13446135135- // A run step that will run the second test executable.
13647 const run_exe_tests = b.addRunArtifact(exe_tests);
13748138138- // A top level step for running all tests. dependOn can be called multiple
139139- // times and since the two run steps do not depend on one another, this will
140140- // make the two of them run in parallel.
14149 const test_step = b.step("test", "Run tests");
14250 test_step.dependOn(&run_mod_tests.step);
14351 test_step.dependOn(&run_exe_tests.step);
144144-145145- // Just like flags, top level steps are also listed in the `--help` menu.
146146- //
147147- // The Zig build system is entirely implemented in userland, which means
148148- // that it cannot hook into private compiler APIs. All compilation work
149149- // orchestrated by the build system will result in other Zig compiler
150150- // subcommands being invoked with the right flags defined. You can observe
151151- // these invocations when one fails (or you pass a flag to increase
152152- // verbosity) to validate assumptions and diagnose problems.
153153- //
154154- // Lastly, the Zig build system is relatively simple and self-contained,
155155- // and reading its source code will allow you to master it.
15652}
+1-70
build.zig.zon
···11.{
22- // This is the default name used by packages depending on this one. For
33- // example, when a user runs `zig fetch --save <url>`, this field is used
44- // as the key in the `dependencies` table. Although the user can choose a
55- // different name, most users will stick with this provided value.
66- //
77- // It is redundant to include "zig" in this name because it is already
88- // within the Zig package namespace.
92 .name = .rat_lisp,
1010- // This is a [Semantic Version](https://semver.org/).
1111- // In a future version of Zig it will be used for package deduplication.
123 .version = "0.0.0",
1313- // Together with name, this represents a globally unique package
1414- // identifier. This field is generated by the Zig toolchain when the
1515- // package is first created, and then *never changes*. This allows
1616- // unambiguous detection of one package being an updated version of
1717- // another.
1818- //
1919- // When forking a Zig project, this id should be regenerated (delete the
2020- // field and run `zig build`) if the upstream project is still maintained.
2121- // Otherwise, the fork is *hostile*, attempting to take control over the
2222- // original project's identity. Thus it is recommended to leave the comment
2323- // on the following line intact, so that it shows up in code reviews that
2424- // modify the field.
254 .fingerprint = 0xee87fa05ccae38f0, // Changing this has security and trust implications.
2626- // Tracks the earliest Zig version that the package considers to be a
2727- // supported use case.
285 .minimum_zig_version = "0.15.2",
2929- // This field is optional.
3030- // Each dependency must either provide a `url` and `hash`, or a `path`.
3131- // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
3232- // Once all dependencies are fetched, `zig build` no longer requires
3333- // internet connectivity.
3434- .dependencies = .{
3535- // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
3636- //.example = .{
3737- // // When updating this field to a new URL, be sure to delete the corresponding
3838- // // `hash`, otherwise you are communicating that you expect to find the old hash at
3939- // // the new URL. If the contents of a URL change this will result in a hash mismatch
4040- // // which will prevent zig from using it.
4141- // .url = "https://example.com/foo.tar.gz",
4242- //
4343- // // This is computed from the file contents of the directory of files that is
4444- // // obtained after fetching `url` and applying the inclusion rules given by
4545- // // `paths`.
4646- // //
4747- // // This field is the source of truth; packages do not come from a `url`; they
4848- // // come from a `hash`. `url` is just one of many possible mirrors for how to
4949- // // obtain a package matching this `hash`.
5050- // //
5151- // // Uses the [multihash](https://multiformats.io/multihash/) format.
5252- // .hash = "...",
5353- //
5454- // // When this is provided, the package is found in a directory relative to the
5555- // // build root. In this case the package's hash is irrelevant and therefore not
5656- // // computed. This field and `url` are mutually exclusive.
5757- // .path = "foo",
5858- //
5959- // // When this is set to `true`, a package is declared to be lazily
6060- // // fetched. This makes the dependency only get fetched if it is
6161- // // actually used.
6262- // .lazy = false,
6363- //},
6464- },
6565- // Specifies the set of files and directories that are included in this package.
6666- // Only files and directories listed here are included in the `hash` that
6767- // is computed for this package. Only files listed here will remain on disk
6868- // when using the zig package manager. As a rule of thumb, one should list
6969- // files required for compilation plus any license(s).
7070- // Paths are relative to the build root. Use the empty string (`""`) to refer to
7171- // the build root itself.
7272- // A directory listed here means that all files within, recursively, are included.
66+ .dependencies = .{},
737 .paths = .{
748 "build.zig",
759 "build.zig.zon",
7610 "src",
7777- // For example...
7878- //"LICENSE",
7979- //"README.md",
8011 },
8112}
···11+//! TODO: Lexing functionality. Intentionally separate from parsing.
22+const std = @import("std");
33+44+// Unresolved questions: OOP stateful nonsense or can we just have A Function?
55+66+/// TODO: Process a code string into a flat array of tokens.
77+pub fn lex(allocator: std.mem.Allocator, code: []const u8) !std.ArrayList(Lexeme) {
88+ _ = allocator;
99+ _ = code;
1010+ return error.Todo;
1111+}
1212+1313+pub const Lexeme = struct {
1414+ text: []const u8,
1515+ kind: Kind,
1616+1717+ const Kind = enum {
1818+ l_paren,
1919+ r_paren,
2020+ identifier,
2121+ number,
2222+ string,
2323+ inert,
2424+ ignore,
2525+ };
2626+};
+10-23
src/main.zig
···11const std = @import("std");
22-const rat_lisp = @import("rat_lisp");
22+const rl = @import("rat_lisp");
3344pub fn main() !void {
55- // Prints to stderr, ignoring potential errors.
66- std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
77- try rat_lisp.bufferedPrint();
88-}
99-1010-test "simple test" {
1111- const gpa = std.testing.allocator;
1212- var list: std.ArrayList(i32) = .empty;
1313- defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
1414- try list.append(gpa, 42);
1515- try std.testing.expectEqual(@as(i32, 42), list.pop());
1616-}
1717-1818-test "fuzz example" {
1919- const Context = struct {
2020- fn testOne(context: @This(), input: []const u8) anyerror!void {
2121- _ = context;
2222- // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
2323- try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
2424- }
2525- };
2626- try std.testing.fuzz(Context{}, Context.testOne, .{});
55+ var gpa: std.heap.DebugAllocator(.{}) = .init;
66+ const allocator = gpa.allocator();
77+ // TODO: somehow turn this into a proper REPL
88+ // TODO: the prompt for the repl should be `:>` ofc
99+ const code = @embedFile("example.rats");
1010+ _ = try rl.read(allocator, code);
1111+ // TODO: eval
1212+ // TODO: print
1313+ // TODO: loop!
2714}
+30
src/parse.zig
···11+//! TODO: Parsing functionality. Intentionally separate from lexing.
22+const std = @import("std");
33+44+const lex = @import("lex.zig");
55+66+pub fn parse(allocator: std.mem.Allocator, lexemes: std.ArrayList(lex.Lexeme)) !Ast {
77+ _ = allocator;
88+ _ = lexemes;
99+ return error.Todo;
1010+}
1111+1212+// TODO: AST
1313+// the AST will be a flat array(list?) of nodes, indexed by a handle type. nodes that contain other
1414+// nodes will merely contain these handles.
1515+1616+pub const Ast = struct {
1717+ nodes: std.ArrayList(Node),
1818+1919+ pub const Node = union(enum) {
2020+ pair: struct { car: Handle, cdr: Handle },
2121+ boolean: bool,
2222+ number: f64,
2323+ symbol: []const u8, // TODO: interning!
2424+ string: []const u8, // TODO: interning?
2525+ inert,
2626+ ignore,
2727+2828+ pub const Handle = enum(u32) { _ };
2929+ };
3030+};
+9-18
src/root.zig
···11-//! By convention, root.zig is the root source file when making a library.
11+//! TODO: Root library module.
22const std = @import("std");
3344-pub fn bufferedPrint() !void {
55- // Stdout is for the actual output of your application, for example if you
66- // are implementing gzip, then only the compressed bytes should be sent to
77- // stdout, not any debugging messages.
88- var stdout_buffer: [1024]u8 = undefined;
99- var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
1010- const stdout = &stdout_writer.interface;
44+const lex = @import("lex.zig");
55+const parse = @import("parse.zig");
66+const eval = @import("eval.zig");
1171212- try stdout.print("Run `zig build test` to run the tests.\n", .{});
88+// TODO: io/reader function(s)
1391414- try stdout.flush(); // Don't forget to flush!
1515-}
1616-1717-pub fn add(a: i32, b: i32) i32 {
1818- return a + b;
1919-}
2020-2121-test "basic add functionality" {
2222- try std.testing.expect(add(3, 7) == 10);
1010+/// TODO: Lex and parse a string of source code and return the AST.
1111+pub fn read(allocator: std.mem.Allocator, code: []const u8) !parse.Ast {
1212+ const lexemes = try lex.lex(allocator, code);
1313+ return parse.parse(allocator, lexemes);
2314}