From 5b940c9e445cb9c8e1f1a69082c1e942a9fe3d07 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 16 Nov 2024 06:42:26 -0500 Subject: [PATCH] feat: minimal, single digit, int lexer --- src/01_lexic/root.zig | 53 +++++++++++++++++++++++++++++++++++++++++-- src/main.zig | 20 ++++++---------- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/01_lexic/root.zig b/src/01_lexic/root.zig index 1c2c752..53e4cd7 100644 --- a/src/01_lexic/root.zig +++ b/src/01_lexic/root.zig @@ -1,3 +1,52 @@ -pub fn stub() i32 { - return 322; +const std = @import("std"); +const t = std.testing; + +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, + }; + } +}; + +pub fn tokenize(input: []const u8) !void { + const next_token = try number(input, 0); + _ = next_token; + + std.debug.print("tokenize :D {s}\n", .{input}); +} + +fn number(input: []const u8, start: usize) !?Token { + const first_char = input[start]; + if (!is_digit(first_char)) { + return null; + } + + return Token.init(input[start .. start + 1], TokenType.Int, start); +} + +fn is_digit(c: u8) bool { + return '0' <= c and c <= '9'; +} + +test "number lexer" { + const input = "3"; + const result = try number(input, 0); + + if (result) |r| { + try std.testing.expectEqual("3", r.value); + } else { + try std.testing.expect(false); + } } diff --git a/src/main.zig b/src/main.zig index e876e82..2319ffe 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,9 +5,6 @@ const thp_version: []const u8 = "0.0.0"; pub fn main() !void { try repl(); - - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); } fn repl() !void { @@ -15,18 +12,20 @@ fn repl() !void { var bw = std.io.bufferedWriter(stdout_file); const stdout = bw.writer(); try stdout.print("The THP REPL, v{s}\n", .{thp_version}); - try stdout.print("Enter expressions to evaluate. Enter CTRL-D to exit.\n\n", .{}); + try stdout.print("Enter expressions to evaluate. Enter CTRL-D to exit.\n", .{}); try bw.flush(); const stdin = std.io.getStdIn().reader(); - try stdout.print("thp => ", .{}); + try stdout.print("\nthp => ", .{}); try bw.flush(); - const user_input = try stdin.readUntilDelimiterAlloc(std.heap.page_allocator, '\n', 8192); - defer std.heap.page_allocator.free(user_input); + const bare_line = try stdin.readUntilDelimiterAlloc(std.heap.page_allocator, '\n', 8192); + defer std.heap.page_allocator.free(bare_line); + const line = std.mem.trim(u8, bare_line, "\r"); + + try lexic.tokenize(line); - try stdout.print("got: `{s}`\n", .{user_input}); try bw.flush(); } @@ -36,8 +35,3 @@ test "simple test" { try list.append(42); try std.testing.expectEqual(@as(i32, 42), list.pop()); } - -test "new test" { - const res = lexic.stub(); - try std.testing.expectEqual(@as(i32, 322), res); -}