diff options
Diffstat (limited to 'src/lexer.zig')
-rw-r--r-- | src/lexer.zig | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/lexer.zig b/src/lexer.zig index 67beec9..8586765 100644 --- a/src/lexer.zig +++ b/src/lexer.zig @@ -1,3 +1,5 @@ +const std = @import("std"); + pub const Token = struct { loc: Location, type: Type, @@ -20,6 +22,16 @@ pub const Location = struct { if (a.end > b.start) unreachable; return .{ .start = @min(a.start, b.start), .end = @max(a.end, b.end) }; } + + /// Assumes that the location comes directly from an `integer_literal` token. + pub fn getInt(self: Location, file_source: []const u8) u64 { + var value: u64 = 0; + for (file_source[self.start..self.end]) |c| { + std.debug.assert('0' <= c and c <= '9'); + value = value * 10 + (c - '0'); + } + return value; + } }; source: []const u8, @@ -41,10 +53,22 @@ pub fn next(self: *Self) ?Token { } fn integerLiteral(self: *Self) Token { - while (digitValue(self.peek()) != null) { + var value: ?u64 = 0; + while (digitValue(self.peek())) |v| { + var nxt: ?u64 = null; + if (value) |val| + if (std.math.mul(u64, val, 10) catch null) |p| + if (std.math.add(u64, p, v) catch null) |s| { + nxt = s; + }; + + value = nxt; _ = self.eat(); } - return self.create(.{ .integer_literal = {} }); + return if (value != null) + self.create(.{ .integer_literal = {} }) + else + self.create(.{ .invalid = {} }); } fn create(self: *Self, tajp: Token.Type) Token { |