diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-08 21:32:26 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-08 21:32:26 +0100 |
commit | 0361b8b891a04cd84189d81a0f1b5f8e512bae7b (patch) | |
tree | 7a8b144b44e9968930bf9ae40f751ec541805541 | |
parent | Parser: Prepare for variable statement parsing (diff) | |
download | interpreter-0361b8b891a04cd84189d81a0f1b5f8e512bae7b.tar.gz interpreter-0361b8b891a04cd84189d81a0f1b5f8e512bae7b.tar.bz2 interpreter-0361b8b891a04cd84189d81a0f1b5f8e512bae7b.zip |
Parser: Use arena allocator
-rw-r--r-- | src/main.zig | 6 | ||||
-rw-r--r-- | src/parser.zig | 25 |
2 files changed, 3 insertions, 28 deletions
diff --git a/src/main.zig b/src/main.zig index 86ba202..e6de7f9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -49,10 +49,10 @@ fn process_buf(buf: []u8, allocator: std.mem.Allocator) !void { std.debug.print("{any}\n", .{token}); } - const source_parser = try parser.Parser.init(token_list.items, allocator); - errdefer source_parser.deinit(null); + var arena = std.heap.ArenaAllocator.init(allocator); + const source_parser = try parser.Parser.init(token_list.items, arena.allocator()); + defer arena.deinit(); 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 7fb6372..e855cbd 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -49,31 +49,6 @@ pub const Parser = struct { return parser; } - //TODO: We maybe should use an arena alocator or smth - pub fn deinit(self: *Parser, ast: ?*Node) void { - if (ast != null) { - std.debug.assert(ast.?.* == .PROGRAM); - for (ast.?.PROGRAM.statements) |statement| { - switch (statement.*) { - .STATEMENT => |x| { - switch (x.statement.*) { - .PRINT_STATEMENT => |p| { - self.allocator.destroy(p.expression); - }, - else => @panic("NOT IMPLEMENTED"), - } - self.allocator.destroy(x.statement); - }, - else => @panic("NOT IMPLEMENTED"), - } - self.allocator.destroy(statement); - } - self.allocator.free(ast.?.PROGRAM.statements); - self.allocator.destroy(ast.?); - } - self.allocator.destroy(self); - } - pub fn parse(self: *Parser) !*Node { return self.parse_program(); } |