diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-07 22:58:12 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-07 22:58:12 +0100 |
commit | 219a40824456e5fad12a24f25dd645ac3117a298 (patch) | |
tree | ad0672d3cab7e8c491d1b49e35ca0887190781d4 | |
parent | Add REPL (diff) | |
download | interpreter-219a40824456e5fad12a24f25dd645ac3117a298.tar.gz interpreter-219a40824456e5fad12a24f25dd645ac3117a298.tar.bz2 interpreter-219a40824456e5fad12a24f25dd645ac3117a298.zip |
Parser: Small improvements
-rw-r--r-- | src/main.zig | 1 | ||||
-rw-r--r-- | src/parser.zig | 30 |
2 files changed, 17 insertions, 14 deletions
diff --git a/src/main.zig b/src/main.zig index c65ef4b..86ba202 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,6 +50,7 @@ fn process_buf(buf: []u8, allocator: std.mem.Allocator) !void { } const source_parser = try parser.Parser.init(token_list.items, allocator); + errdefer source_parser.deinit(null); const ast = try source_parser.parse(); defer source_parser.deinit(@constCast(ast)); std.debug.print("AST: {any}\n", .{ast}); diff --git a/src/parser.zig b/src/parser.zig index 8499732..5b6915f 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -50,22 +50,24 @@ pub const Parser = struct { return parser; } - pub fn deinit(self: *Parser, ast: *Node) void { - std.debug.assert(ast.* == .PROGRAM); - for (ast.PROGRAM.statements) |statement| { - switch (statement.*) { - .VARIABLE_STATEMENT => unreachable, - .PRINT_STATEMENT => |x| { - self.allocator.destroy(x.expression); - }, - .NUMBER => unreachable, - .IDENTIFIER => unreachable, - else => unreachable, + pub fn deinit(self: *Parser, ast: ?*Node) void { + if (ast != null) { + // std.debug.assert(ast.?.* == .PROGRAM); + for (ast.?.PROGRAM.statements) |statement| { + switch (statement.*) { + .VARIABLE_STATEMENT => @panic("NOT IMPLEMENTED"), + .PRINT_STATEMENT => |x| { + self.allocator.destroy(x.expression); + }, + .NUMBER => @panic("NOT IMPLEMENTED"), + .IDENTIFIER => @panic("NOT IMPLEMENTED"), + else => unreachable, + } + self.allocator.destroy(statement); } - self.allocator.destroy(statement); + self.allocator.free(ast.?.PROGRAM.statements); + self.allocator.destroy(ast.?); } - self.allocator.free(ast.PROGRAM.statements); - self.allocator.destroy(ast); self.allocator.destroy(self); } |