diff options
Diffstat (limited to 'src/parse.zig')
-rw-r--r-- | src/parse.zig | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/parse.zig b/src/parse.zig index efb5c8e..26f32e9 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -151,7 +151,18 @@ pub const Expr = struct { const ParseError = error{ OutOfMemory, ExpectedRightParen, UnexpectedToken, InvalidAssignTarget }; -pub fn block(allocator: Allocator, lexer: *Lexer) !Block { +pub fn file(allocator: Allocator, lexer: *Lexer) !Block { + var stmts: std.ArrayList(Stmt) = .init(allocator); + while (lexer.peek().type != .eof) { + try stmts.append(try statement(allocator, lexer)); + } + return .{ + .loc = stmts.items[0].loc.combine(stmts.getLast().loc), + .stmts = try stmts.toOwnedSlice(), + }; +} + +fn block(allocator: Allocator, lexer: *Lexer) !Block { const left_curly = try mustEat(lexer, .left_curly); var stmts: std.ArrayList(Stmt) = .init(allocator); while (lexer.peek().type != .right_curly) { @@ -164,7 +175,7 @@ pub fn block(allocator: Allocator, lexer: *Lexer) !Block { }; } -pub fn statement(allocator: Allocator, lexer: *Lexer) ParseError!Stmt { +fn statement(allocator: Allocator, lexer: *Lexer) ParseError!Stmt { switch (lexer.peek().type) { .let => { const let = lexer.next(); @@ -217,11 +228,11 @@ pub fn statement(allocator: Allocator, lexer: *Lexer) ParseError!Stmt { } } -pub fn expression(allocator: Allocator, lexer: *Lexer) ParseError!*Expr { +fn expression(allocator: Allocator, lexer: *Lexer) ParseError!*Expr { return parseComparisons(allocator, lexer); } -pub fn parseComparisons(allocator: Allocator, lexer: *Lexer) ParseError!*Expr { +fn parseComparisons(allocator: Allocator, lexer: *Lexer) ParseError!*Expr { const lhs = try parseTerms(allocator, lexer); const op: Expr.Type.BinOp.Op = switch (lexer.peek().type) { @@ -240,7 +251,7 @@ pub fn parseComparisons(allocator: Allocator, lexer: *Lexer) ParseError!*Expr { }); } -pub fn parseTerms(allocator: Allocator, lexer: *Lexer) !*Expr { +fn parseTerms(allocator: Allocator, lexer: *Lexer) !*Expr { var lhs = try parseIf(allocator, lexer); while (true) { const op: Expr.Type.BinOp.Op = switch (lexer.peek().type) { @@ -259,7 +270,7 @@ pub fn parseTerms(allocator: Allocator, lexer: *Lexer) !*Expr { return lhs; } -pub fn parseIf(allocator: Allocator, lexer: *Lexer) !*Expr { +fn parseIf(allocator: Allocator, lexer: *Lexer) !*Expr { switch (lexer.peek().type) { .@"if" => { const @"if" = lexer.next(); @@ -282,7 +293,7 @@ pub fn parseIf(allocator: Allocator, lexer: *Lexer) !*Expr { } } -pub fn parseInvocations(allocator: Allocator, lexer: *Lexer) !*Expr { +fn parseInvocations(allocator: Allocator, lexer: *Lexer) !*Expr { var proc = try parsePrimaryExpr(allocator, lexer); while (true) { if (lexer.peek().type != .left_paren) break; @@ -297,7 +308,7 @@ pub fn parseInvocations(allocator: Allocator, lexer: *Lexer) !*Expr { return proc; } -pub fn parsePrimaryExpr(allocator: Allocator, lexer: *Lexer) !*Expr { +fn parsePrimaryExpr(allocator: Allocator, lexer: *Lexer) !*Expr { const token = lexer.next(); return allocate(Expr, allocator, switch (token.type) { .left_paren => { |