Skip to content
This repository was archived by the owner on Jan 1, 2026. It is now read-only.

Commit 50962c9

Browse files
authored
Merge pull request #13 from forthix/RESRCH-2-ForthicZig-tokenizer
Add forthic-zig tokenizer
2 parents aadef46 + 5088e78 commit 50962c9

8 files changed

Lines changed: 541 additions & 0 deletions

File tree

forthic-zig/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
zig-cache/
2+
zig-out/
3+
/release/
4+
/debug/
5+
/build/
6+
/build-*/
7+
/docgen_tmp/

forthic-zig/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
run:
2+
zig build run
3+
4+
test:
5+
zig test src/forthic/tokenizer.zig
6+
zig test src/forthic/token.zig

forthic-zig/build.zig

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
const lib = b.addStaticLibrary(.{
19+
.name = "forthic-zig",
20+
// In this case the main source file is merely a path, however, in more
21+
// complicated build scripts, this could be a generated file.
22+
.root_source_file = b.path("src/root.zig"),
23+
.target = target,
24+
.optimize = optimize,
25+
});
26+
27+
// This declares intent for the library to be installed into the standard
28+
// location when the user invokes the "install" step (the default step when
29+
// running `zig build`).
30+
b.installArtifact(lib);
31+
32+
const exe = b.addExecutable(.{
33+
.name = "forthic-zig",
34+
.root_source_file = b.path("src/main.zig"),
35+
.target = target,
36+
.optimize = optimize,
37+
});
38+
39+
// This declares intent for the executable to be installed into the
40+
// standard location when the user invokes the "install" step (the default
41+
// step when running `zig build`).
42+
b.installArtifact(exe);
43+
44+
// This *creates* a Run step in the build graph, to be executed when another
45+
// step is evaluated that depends on it. The next line below will establish
46+
// such a dependency.
47+
const run_cmd = b.addRunArtifact(exe);
48+
49+
// By making the run step depend on the install step, it will be run from the
50+
// installation directory rather than directly from within the cache directory.
51+
// This is not necessary, however, if the application depends on other installed
52+
// files, this ensures they will be present and in the expected location.
53+
run_cmd.step.dependOn(b.getInstallStep());
54+
55+
// This allows the user to pass arguments to the application in the build
56+
// command itself, like this: `zig build run -- arg1 arg2 etc`
57+
if (b.args) |args| {
58+
run_cmd.addArgs(args);
59+
}
60+
61+
// This creates a build step. It will be visible in the `zig build --help` menu,
62+
// and can be selected like this: `zig build run`
63+
// This will evaluate the `run` step rather than the default, which is "install".
64+
const run_step = b.step("run", "Run the app");
65+
run_step.dependOn(&run_cmd.step);
66+
67+
// Creates a step for unit testing. This only builds the test executable
68+
// but does not run it.
69+
const lib_unit_tests = b.addTest(.{
70+
.root_source_file = b.path("src/root.zig"),
71+
.target = target,
72+
.optimize = optimize,
73+
});
74+
75+
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
76+
77+
const exe_unit_tests = b.addTest(.{
78+
.root_source_file = b.path("src/main.zig"),
79+
.target = target,
80+
.optimize = optimize,
81+
});
82+
83+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
84+
85+
// Similar to creating the run step earlier, this exposes a `test` step to
86+
// the `zig build --help` menu, providing a way for the user to request
87+
// running the unit tests.
88+
const test_step = b.step("test", "Run unit tests");
89+
test_step.dependOn(&run_lib_unit_tests.step);
90+
test_step.dependOn(&run_exe_unit_tests.step);
91+
}

forthic-zig/build.zig.zon

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.{
2+
.name = "forthic-zig",
3+
// This is a [Semantic Version](https://semver.org/).
4+
// In a future version of Zig it will be used for package deduplication.
5+
.version = "0.0.0",
6+
7+
// This field is optional.
8+
// This is currently advisory only; Zig does not yet do anything
9+
// with this value.
10+
//.minimum_zig_version = "0.11.0",
11+
12+
// This field is optional.
13+
// Each dependency must either provide a `url` and `hash`, or a `path`.
14+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
15+
// Once all dependencies are fetched, `zig build` no longer requires
16+
// internet connectivity.
17+
.dependencies = .{
18+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
19+
//.example = .{
20+
// // When updating this field to a new URL, be sure to delete the corresponding
21+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
22+
// // the new URL.
23+
// .url = "https://example.com/foo.tar.gz",
24+
//
25+
// // This is computed from the file contents of the directory of files that is
26+
// // obtained after fetching `url` and applying the inclusion rules given by
27+
// // `paths`.
28+
// //
29+
// // This field is the source of truth; packages do not come from a `url`; they
30+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
31+
// // obtain a package matching this `hash`.
32+
// //
33+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
34+
// .hash = "...",
35+
//
36+
// // When this is provided, the package is found in a directory relative to the
37+
// // build root. In this case the package's hash is irrelevant and therefore not
38+
// // computed. This field and `url` are mutually exclusive.
39+
// .path = "foo",
40+
41+
// // When this is set to `true`, a package is declared to be lazily
42+
// // fetched. This makes the dependency only get fetched if it is
43+
// // actually used.
44+
// .lazy = false,
45+
//},
46+
},
47+
48+
// Specifies the set of files and directories that are included in this package.
49+
// Only files and directories listed here are included in the `hash` that
50+
// is computed for this package.
51+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
52+
// the build root itself.
53+
// A directory listed here means that all files within, recursively, are included.
54+
.paths = .{
55+
// This makes *all* files, recursively, included in this package. It is generally
56+
// better to explicitly list the files and directories instead, to insure that
57+
// fetching from tarballs, file system paths, and version control all result
58+
// in the same contents hash.
59+
"",
60+
// For example...
61+
//"build.zig",
62+
//"build.zig.zon",
63+
//"src",
64+
//"LICENSE",
65+
//"README.md",
66+
},
67+
}

forthic-zig/src/forthic/token.zig

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const std = @import("std");
2+
pub const TokenType = enum { tok_string, tok_comment, tok_start_array, tok_end_array, tok_start_module, tok_end_module, tok_start_definition, tok_end_definition, tok_start_memo, tok_word, tok_eos };
3+
4+
pub const Token = struct {
5+
token_type: TokenType,
6+
token_string: []u8,
7+
allocator: std.mem.Allocator,
8+
9+
pub fn deinit(self: *Token) void {
10+
if (self.token_string.len > 0) {
11+
self.allocator.free(self.token_string);
12+
}
13+
}
14+
};
15+
16+
pub fn createToken(token_type: TokenType, token_string: []const u8, allocator: std.mem.Allocator) error{OutOfMemory}!Token {
17+
var buf = try allocator.alloc(u8, token_string.len);
18+
for (token_string, 0..) |c, i| {
19+
buf[i] = c;
20+
}
21+
22+
return Token{
23+
.token_type = token_type,
24+
.token_string = buf,
25+
.allocator = allocator,
26+
};
27+
}
28+
29+
test "createToken" {
30+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
31+
const allocator = gpa.allocator();
32+
defer {
33+
const deinit_status = gpa.deinit();
34+
//fail test; can't try in defer as defer is executed after we return
35+
if (deinit_status == .leak) std.testing.expect(false) catch @panic("TEST FAIL");
36+
}
37+
var token = createToken(TokenType.tok_string, "hello", allocator) catch |err| {
38+
std.debug.print("Error: {}\n", .{err});
39+
return;
40+
};
41+
defer token.deinit();
42+
43+
std.testing.expect(token.token_type == TokenType.tok_string) catch @panic("TEST FAIL");
44+
std.testing.expect(std.mem.eql(u8, token.token_string, "hello")) catch @panic("TEST FAIL");
45+
}

0 commit comments

Comments
 (0)