diff options
Diffstat (limited to 'src/parse.zig')
-rw-r--r-- | src/parse.zig | 20 |
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, |