diff options
Diffstat (limited to 'src/parser.zig')
-rw-r--r-- | src/parser.zig | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/parser.zig b/src/parser.zig index e8fcd81..5d3a957 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -11,6 +11,7 @@ const NodeType = enum { STATEMENT, VARIABLE_STATEMENT, PRINT_STATEMENT, + RETURN_STATEMENT, EXPRESSION, }; @@ -29,6 +30,9 @@ pub const Node = union(NodeType) { PRINT_STATEMENT: struct { expression: *Node, }, + RETURN_STATEMENT: struct { + expression: *Node, + }, EXPRESSION: union(enum) { NUMBER: struct { value: i64, @@ -84,6 +88,7 @@ pub const Parser = struct { const statement = switch (token) { .PRINT => try self.parse_print_statement(), + .RETURN => try self.parse_return_statement(), else => try self.parse_variable_statement(), }; @@ -138,6 +143,20 @@ pub const Parser = struct { }); } + // ReturnStatement :== RETURN Expression + fn parse_return_statement(self: *Parser) ParserError!*Node { + errdefer std.debug.print("Error parsing return statement\n", .{}); + _ = try self.accept_token(tokenizer.TokenType.RETURN); + + const expression = try self.parse_expression(); + + return self.create_node(.{ + .RETURN_STATEMENT = .{ + .expression = @constCast(expression), + }, + }); + } + // Expression :== NUMBER | IDENTIFIER | Expression + Expression fn parse_expression(self: *Parser) ParserError!*Node { errdefer std.debug.print("Error parsing expression\n", .{}); |