aboutsummaryrefslogtreecommitdiff
path: root/src/parse.zig
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-06-02 15:01:58 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-06-02 15:16:20 +0200
commit939ee7606fbfe3afa6b6a008fb617120a6dd8114 (patch)
tree2bd55979cd3f4531ef83090b99ba296ec7db82c8 /src/parse.zig
parentef18ba588a4b1531c8b42cbfc996ef6f60de1d97 (diff)
downloadhuginn-939ee7606fbfe3afa6b6a008fb617120a6dd8114.tar.gz
make Lexer peekable without a wrapper
Diffstat (limited to 'src/parse.zig')
-rw-r--r--src/parse.zig20
1 files changed, 8 insertions, 12 deletions
diff --git a/src/parse.zig b/src/parse.zig
index d5d3e9b..901ce0d 100644
--- a/src/parse.zig
+++ b/src/parse.zig
@@ -4,8 +4,6 @@ const Allocator = std.mem.Allocator;
const root = @import("root");
const Lexer = root.Lexer;
const Token = root.Lexer.Token;
-const Peekable = root.Peekable;
-const peekable = root.peekable;
pub const Expr = struct {
loc: Lexer.Location,
@@ -57,20 +55,18 @@ pub const Expr = struct {
}
};
-pub fn expression(allocator: Allocator, lexer: *Peekable(Lexer)) error{OutOfMemory}!*Expr {
+pub fn expression(allocator: Allocator, lexer: *Lexer) error{OutOfMemory}!*Expr {
return addExpr(allocator, lexer);
}
-pub fn addExpr(allocator: Allocator, lexer: *Peekable(Lexer)) !*Expr {
+pub fn addExpr(allocator: Allocator, lexer: *Lexer) !*Expr {
var lhs = try callExpr(allocator, lexer);
while (true) {
- const token: Lexer.Token = if (lexer.peek()) |t| t else break;
- const op: Expr.Type.BinOp.Op = switch (token.type) {
+ const op: Expr.Type.BinOp.Op = switch (lexer.peek().type) {
.plus => .plus,
.minus => .minus,
else => break,
};
-
_ = lexer.next();
const rhs = try callExpr(allocator, lexer);
@@ -82,10 +78,10 @@ pub fn addExpr(allocator: Allocator, lexer: *Peekable(Lexer)) !*Expr {
return lhs;
}
-pub fn callExpr(allocator: Allocator, lexer: *Peekable(Lexer)) !*Expr {
+pub fn callExpr(allocator: Allocator, lexer: *Lexer) !*Expr {
var proc = try primaryExpr(allocator, lexer);
while (true) {
- switch (lexer.peek().?.type) {
+ switch (lexer.peek().type) {
.left_paren, .integer_literal => {},
else => break,
}
@@ -98,13 +94,13 @@ pub fn callExpr(allocator: Allocator, lexer: *Peekable(Lexer)) !*Expr {
return proc;
}
-pub fn primaryExpr(allocator: Allocator, lexer: *Peekable(Lexer)) !*Expr {
- const token = lexer.next().?;
+pub fn primaryExpr(allocator: Allocator, lexer: *Lexer) !*Expr {
+ const token = lexer.next();
// std.debug.print("term {}\n", .{token});
return allocate(allocator, switch (token.type) {
.left_paren => {
const res = expression(allocator, lexer);
- const right_paren = lexer.next().?;
+ const right_paren = lexer.next();
if (right_paren.type != .right_paren)
return allocate(allocator, .{
.loc = right_paren.loc,