refactor: use modules in build.zig
This commit is contained in:
parent
1bd463998c
commit
0ff3e94620
61
build.zig
61
build.zig
@ -15,20 +15,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const lib = b.addStaticLibrary(.{
|
|
||||||
.name = "thp",
|
|
||||||
// In this case the main source file is merely a path, however, in more
|
|
||||||
// complicated build scripts, this could be a generated file.
|
|
||||||
.root_source_file = b.path("src/root.zig"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
|
||||||
|
|
||||||
// This declares intent for the library to be installed into the standard
|
|
||||||
// location when the user invokes the "install" step (the default step when
|
|
||||||
// running `zig build`).
|
|
||||||
b.installArtifact(lib);
|
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "thp",
|
.name = "thp",
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_source_file = b.path("src/main.zig"),
|
||||||
@ -36,6 +22,26 @@ pub fn build(b: *std.Build) void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// Lexic module
|
||||||
|
//
|
||||||
|
const lexic_module = b.addModule("lexic", .{
|
||||||
|
.root_source_file = b.path("src/01_lexic/root.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
exe.root_module.addImport("lexic", lexic_module);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Syntax module
|
||||||
|
//
|
||||||
|
const syntax_module = b.addModule("syntax", .{
|
||||||
|
.root_source_file = b.path("src/02_syntax/root.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
exe.root_module.addImport("syntax", syntax_module);
|
||||||
|
|
||||||
// This declares intent for the executable to be installed into the
|
// This declares intent for the executable to be installed into the
|
||||||
// standard location when the user invokes the "install" step (the default
|
// standard location when the user invokes the "install" step (the default
|
||||||
// step when running `zig build`).
|
// step when running `zig build`).
|
||||||
@ -66,19 +72,13 @@ pub fn build(b: *std.Build) void {
|
|||||||
|
|
||||||
// Creates a step for unit testing. This only builds the test executable
|
// Creates a step for unit testing. This only builds the test executable
|
||||||
// but does not run it.
|
// but does not run it.
|
||||||
const lib_unit_tests = b.addTest(.{
|
|
||||||
.root_source_file = b.path("src/root.zig"),
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
|
||||||
|
|
||||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
|
||||||
|
|
||||||
const exe_unit_tests = b.addTest(.{
|
const exe_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
exe_unit_tests.root_module.addImport("lexic", lexic_module);
|
||||||
|
exe_unit_tests.root_module.addImport("syntax", syntax_module);
|
||||||
|
|
||||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
|
|
||||||
@ -86,22 +86,12 @@ pub fn build(b: *std.Build) void {
|
|||||||
// the `zig build --help` menu, providing a way for the user to request
|
// the `zig build --help` menu, providing a way for the user to request
|
||||||
// running the unit tests.
|
// running the unit tests.
|
||||||
const test_step = b.step("test", "Run ALL unit tests");
|
const test_step = b.step("test", "Run ALL unit tests");
|
||||||
test_step.dependOn(&run_lib_unit_tests.step);
|
|
||||||
test_step.dependOn(&run_exe_unit_tests.step);
|
test_step.dependOn(&run_exe_unit_tests.step);
|
||||||
|
|
||||||
// Add more dependencies for unit testing
|
// Add dependencies for unit testing
|
||||||
const files = [_][]const u8{
|
const files = [_][]const u8{
|
||||||
"src/01_lexic/root.zig",
|
"src/01_lexic/root.zig",
|
||||||
"src/01_lexic/number.zig",
|
"src/02_syntax/root.zig",
|
||||||
"src/01_lexic/identifier.zig",
|
|
||||||
"src/01_lexic/datatype.zig",
|
|
||||||
"src/01_lexic/operator.zig",
|
|
||||||
"src/01_lexic/comment.zig",
|
|
||||||
"src/01_lexic/string.zig",
|
|
||||||
"src/01_lexic/token.zig",
|
|
||||||
"src/01_lexic/utils.zig",
|
|
||||||
"src/01_lexic/grouping.zig",
|
|
||||||
"src/01_lexic/punctiation.zig",
|
|
||||||
};
|
};
|
||||||
for (files) |file| {
|
for (files) |file| {
|
||||||
const file_unit_test = b.addTest(.{
|
const file_unit_test = b.addTest(.{
|
||||||
@ -109,6 +99,9 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
file_unit_test.root_module.addImport("lexic", lexic_module);
|
||||||
|
file_unit_test.root_module.addImport("syntax", syntax_module);
|
||||||
|
|
||||||
var test_artifact = b.addRunArtifact(file_unit_test);
|
var test_artifact = b.addRunArtifact(file_unit_test);
|
||||||
test_step.dependOn(&test_artifact.step);
|
test_step.dependOn(&test_artifact.step);
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const number = @import("./number.zig");
|
const number = @import("number.zig");
|
||||||
const identifier = @import("./identifier.zig");
|
const identifier = @import("identifier.zig");
|
||||||
const datatype = @import("./datatype.zig");
|
const datatype = @import("datatype.zig");
|
||||||
const token = @import("./token.zig");
|
const token = @import("token.zig");
|
||||||
const operator = @import("./operator.zig");
|
const operator = @import("operator.zig");
|
||||||
const comment = @import("./comment.zig");
|
const comment = @import("comment.zig");
|
||||||
const string = @import("./string.zig");
|
const string = @import("string.zig");
|
||||||
const grouping = @import("./grouping.zig");
|
const grouping = @import("grouping.zig");
|
||||||
const punctuation = @import("./punctiation.zig");
|
const punctuation = @import("punctiation.zig");
|
||||||
|
|
||||||
const TokenType = token.TokenType;
|
const TokenType = token.TokenType;
|
||||||
const Token = token.Token;
|
const Token = token.Token;
|
||||||
|
|
||||||
// Creates an array list of tokens. The caller is responsible of
|
/// Creates an array list of tokens. The caller is responsible of
|
||||||
// calling `deinit` to free the array list
|
/// calling `deinit` to free the array list
|
||||||
pub fn tokenize(input: []const u8, alloc: std.mem.Allocator) !std.ArrayList(Token) {
|
pub fn tokenize(input: []const u8, alloc: std.mem.Allocator) !std.ArrayList(Token) {
|
||||||
const input_len = input.len;
|
const input_len = input.len;
|
||||||
var current_pos: usize = 0;
|
var current_pos: usize = 0;
|
||||||
@ -118,6 +118,10 @@ pub fn ignore_whitespace(input: []const u8, start: usize) usize {
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
std.testing.refAllDecls(@This());
|
||||||
|
}
|
||||||
|
|
||||||
test "should insert 1 item" {
|
test "should insert 1 item" {
|
||||||
const input = "322";
|
const input = "322";
|
||||||
const arrl = try tokenize(input, std.testing.allocator);
|
const arrl = try tokenize(input, std.testing.allocator);
|
||||||
|
5
src/02_syntax/root.zig
Normal file
5
src/02_syntax/root.zig
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
test {
|
||||||
|
std.testing.refAllDecls(@This());
|
||||||
|
}
|
@ -1,7 +1,8 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lexic = @import("./01_lexic/root.zig");
|
const lexic = @import("lexic");
|
||||||
|
const syntax = @import("syntax");
|
||||||
|
|
||||||
const thp_version: []const u8 = "0.0.0";
|
const thp_version: []const u8 = "0.0.1";
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
try repl();
|
try repl();
|
||||||
|
Loading…
Reference in New Issue
Block a user