From 8dccf94ad5ffde4100a28fece3ea69e85dc6878f Mon Sep 17 00:00:00 2001 From: Araozu Date: Sun, 17 Nov 2024 07:56:29 -0500 Subject: [PATCH] feat: lex floating point numbers with scientific notation --- src/01_lexic/number.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/01_lexic/number.zig b/src/01_lexic/number.zig index cf6152f..b79a018 100644 --- a/src/01_lexic/number.zig +++ b/src/01_lexic/number.zig @@ -146,6 +146,12 @@ fn floating_point(input: []const u8, cap: usize, token_start: usize, period_pos: current_pos += 1; } + // check if the current character is a `e`, + // if so lex a scientific number + if (current_pos < cap and input[current_pos] == 'e') { + return scientific(input, cap, token_start, current_pos); + } + // return the matched fp number return .{ Token.init(input[token_start..current_pos], TokenType.Float, token_start), @@ -448,3 +454,15 @@ test "should fail on incomplete scientific number" { try std.testing.expect(false); } + +test "should lex floating scientific number" { + const input = "0.58e+3"; + const result = try lex(input, input.len, 0); + + if (result) |tuple| { + const r = tuple[0]; + try std.testing.expectEqualDeep("0.58e+3", r.value); + } else { + try std.testing.expect(false); + } +}