refactor: organize files
This commit is contained in:
parent
70dce84f05
commit
cbefbe3f68
@ -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(.{
|
||||
|
33
src/01_lexic/number.zig
Normal file
33
src/01_lexic/number.zig
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
18
src/01_lexic/token.zig
Normal file
18
src/01_lexic/token.zig
Normal file
@ -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,
|
||||
};
|
||||
}
|
||||
};
|
3
src/01_lexic/utils.zig
Normal file
3
src/01_lexic/utils.zig
Normal file
@ -0,0 +1,3 @@
|
||||
pub fn is_decimal_digit(c: u8) bool {
|
||||
return '0' <= c and c <= '9';
|
||||
}
|
Loading…
Reference in New Issue
Block a user