aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.zig
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-05-29 00:42:36 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-05-29 01:53:54 +0200
commitdb112f0d384ca991e1477913b8af7a2c8dc80088 (patch)
treed76972bfbff517243027a4d65f796bc94374420c /src/lexer.zig
parent7b85276fc98643b1138df6f322b8bd657339ea96 (diff)
downloadhuginn-db112f0d384ca991e1477913b8af7a2c8dc80088.tar.gz
compile some god damn additions
Diffstat (limited to 'src/lexer.zig')
-rw-r--r--src/lexer.zig49
1 files changed, 28 insertions, 21 deletions
diff --git a/src/lexer.zig b/src/lexer.zig
index 93ed6cc..67beec9 100644
--- a/src/lexer.zig
+++ b/src/lexer.zig
@@ -1,49 +1,56 @@
pub const Token = struct {
- start: usize,
- end: usize,
+ loc: Location,
type: Type,
pub const Type = union(enum) {
- LeftParen,
- RightParen,
- IntegerLiteral: usize,
- Plus,
- Invalid,
- Eof,
+ left_paren,
+ right_paren,
+ integer_literal,
+ plus,
+ invalid,
+ eof,
};
};
+pub const Location = struct {
+ start: usize,
+ end: usize,
+
+ pub fn combine(a: Location, b: Location) Location {
+ if (a.end > b.start) unreachable;
+ return .{ .start = @min(a.start, b.start), .end = @max(a.end, b.end) };
+ }
+};
+
source: []const u8,
last_end: usize = 0,
pos: usize = 0,
pub fn next(self: *Self) ?Token {
- return s: switch (self.eat() orelse return self.create(.Eof)) {
- '(' => self.create(.LeftParen),
- ')' => self.create(.RightParen),
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' => |d| self.integerLiteral(d),
- '+' => self.create(.Plus),
+ return s: switch (self.eat() orelse return self.create(.eof)) {
+ '(' => self.create(.left_paren),
+ ')' => self.create(.right_paren),
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' => self.integerLiteral(),
+ '+' => self.create(.plus),
' ' => {
self.last_end = self.pos;
- continue :s (self.eat() orelse return self.create(.Eof));
+ continue :s (self.eat() orelse return self.create(.eof));
},
- else => self.create(.Invalid),
+ else => self.create(.invalid),
};
}
-fn integerLiteral(self: *Self, first: u8) Token {
- var value: usize = @intCast(digitValue(first).?);
- while (digitValue(self.peek())) |d| {
+fn integerLiteral(self: *Self) Token {
+ while (digitValue(self.peek()) != null) {
_ = self.eat();
- value = value *| 10 +| d;
}
- return self.create(.{ .IntegerLiteral = value });
+ return self.create(.{ .integer_literal = {} });
}
fn create(self: *Self, tajp: Token.Type) Token {
const start = self.last_end;
self.last_end = self.pos;
- return .{ .start = start, .end = self.pos, .type = tajp };
+ return .{ .loc = .{ .start = start, .end = self.pos }, .type = tajp };
}
fn eat(self: *Self) ?u8 {