From cbefbe3f68e263bca2a04453768cbec32960169c Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 16 Nov 2024 16:12:15 -0500 Subject: [PATCH] refactor: organize files --- build.zig | 3 +++ src/01_lexic/number.zig | 33 ++++++++++++++++++++++++++ src/01_lexic/root.zig | 52 ++++------------------------------------- src/01_lexic/token.zig | 18 ++++++++++++++ src/01_lexic/utils.zig | 3 +++ 5 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 src/01_lexic/number.zig create mode 100644 src/01_lexic/token.zig create mode 100644 src/01_lexic/utils.zig diff --git a/build.zig b/build.zig index 36e8a98..5539bb1 100644 --- a/build.zig +++ b/build.zig @@ -92,6 +92,9 @@ pub fn build(b: *std.Build) void { // Add more dependencies for unit testing const files = [_][]const u8{ "src/01_lexic/root.zig", + "src/01_lexic/number.zig", + "src/01_lexic/token.zig", + "src/01_lexic/utils.zig", }; for (files) |file| { const file_unit_test = b.addTest(.{ diff --git a/src/01_lexic/number.zig b/src/01_lexic/number.zig new file mode 100644 index 0000000..7885b6f --- /dev/null +++ b/src/01_lexic/number.zig @@ -0,0 +1,33 @@ +const std = @import("std"); +const token = @import("./token.zig"); +const utils = @import("./utils.zig"); + +const Token = token.Token; +const TokenType = token.TokenType; + +const is_decimal_digit = utils.is_decimal_digit; + +fn integer(input: []const u8, cap: usize, start: usize) !?Token { + const first_char = input[start]; + if (!is_decimal_digit(first_char)) { + return null; + } + + var last_pos = start + 1; + while (last_pos < cap and is_decimal_digit(input[last_pos])) { + last_pos += 1; + } + + return Token.init(input[start..last_pos], TokenType.Int, start); +} + +test "number lexer" { + const input = "322 "; + const result = try integer(input, input.len, 0); + + if (result) |r| { + try std.testing.expectEqualDeep("322", r.value); + } else { + try std.testing.expect(false); + } +} diff --git a/src/01_lexic/root.zig b/src/01_lexic/root.zig index c987c73..1c41159 100644 --- a/src/01_lexic/root.zig +++ b/src/01_lexic/root.zig @@ -1,24 +1,9 @@ const std = @import("std"); -const t = std.testing; +const number = @import("./number.zig"); +const token = @import("./token.zig"); -const TokenType = enum { - Int, - Float, -}; - -const Token = struct { - value: []const u8, - token_type: TokenType, - start_pos: usize, - - pub fn init(value: []const u8, token_type: TokenType, start: usize) Token { - return Token{ - .value = value, - .token_type = token_type, - .start_pos = start, - }; - } -}; +const TokenType = token.TokenType; +const Token = token.Token; pub fn tokenize(input: []const u8) !void { const input_len = input.len; @@ -27,32 +12,3 @@ pub fn tokenize(input: []const u8) !void { std.debug.print("tokenize :D {s}\n", .{input}); } - -fn number(input: []const u8, cap: usize, start: usize) !?Token { - const first_char = input[start]; - if (!is_decimal_digit(first_char)) { - return null; - } - - var last_pos = start + 1; - while (last_pos < cap and is_decimal_digit(input[last_pos])) { - last_pos += 1; - } - - return Token.init(input[start..last_pos], TokenType.Int, start); -} - -fn is_decimal_digit(c: u8) bool { - return '0' <= c and c <= '9'; -} - -test "number lexer" { - const input = "322 "; - const result = try number(input, input.len, 0); - - if (result) |r| { - try std.testing.expectEqualDeep("322", r.value); - } else { - try std.testing.expect(false); - } -} diff --git a/src/01_lexic/token.zig b/src/01_lexic/token.zig new file mode 100644 index 0000000..b154c05 --- /dev/null +++ b/src/01_lexic/token.zig @@ -0,0 +1,18 @@ +pub const TokenType = enum { + Int, + Float, +}; + +pub const Token = struct { + value: []const u8, + token_type: TokenType, + start_pos: usize, + + pub fn init(value: []const u8, token_type: TokenType, start: usize) Token { + return Token{ + .value = value, + .token_type = token_type, + .start_pos = start, + }; + } +}; diff --git a/src/01_lexic/utils.zig b/src/01_lexic/utils.zig new file mode 100644 index 0000000..a23eaa8 --- /dev/null +++ b/src/01_lexic/utils.zig @@ -0,0 +1,3 @@ +pub fn is_decimal_digit(c: u8) bool { + return '0' <= c and c <= '9'; +}