summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.zig4
-rw-r--r--src/parser.zig30
-rw-r--r--src/symtable.zig19
3 files changed, 37 insertions, 16 deletions
diff --git a/src/main.zig b/src/main.zig
index d748461..319801a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -58,7 +58,9 @@ pub fn main() !void {
try pop.populateSymtable(&treeNode);
// Codegen
- var generator = gen.Generator.init(allocator, tree);
+ var arena = std.heap.ArenaAllocator.init(allocator);
+ defer arena.deinit();
+ var generator = gen.Generator.init(arena.allocator(), tree);
defer generator.deinit();
const code = try generator.generate();
try outWriter.writeAll(code);
diff --git a/src/parser.zig b/src/parser.zig
index 8cd539f..08f098c 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -113,16 +113,30 @@ pub const Parser = struct {
};
}
+ fn dinitHelper(self: *Parser, node: NodeStmt) !void {
+ switch (node.kind) {
+ .block => |blk| {
+ const children = try node.children(self.allocator);
+ defer self.allocator.free(children);
+ for (children) |child| try self.dinitHelper(child.Stmt);
+ self.allocator.free(blk);
+ node.symtable.deinit();
+ },
+ .function => |fun| {
+ const children = try node.children(self.allocator);
+ defer self.allocator.free(children);
+ for (children) |child| try self.dinitHelper(child.Stmt);
+ self.allocator.destroy(fun.block);
+ },
+ else => {},
+ }
+ }
+
pub fn deinit(self: *Parser) void {
for (self.nodes.items) |node| {
- switch (node.kind) {
- .block => |blk| self.allocator.free(blk),
- .function => |fun| {
- self.allocator.free(fun.block.kind.block);
- self.allocator.destroy(fun.block);
- },
- else => {},
- }
+ self.dinitHelper(node) catch |err| {
+ if (err == error.OutOfMemory) {}
+ };
}
self.nodes.deinit();
}
diff --git a/src/symtable.zig b/src/symtable.zig
index f37ee12..cc041c5 100644
--- a/src/symtable.zig
+++ b/src/symtable.zig
@@ -196,6 +196,7 @@ pub const Populator = struct {
std.debug.print("Populated Function {s}\n", .{fun.ident.ident});
if (!try table.insert(fun.ident.ident, symbol)) return error.FailedToInsert;
const children = try stmt.children(self.allocator);
+ defer self.allocator.free(children);
for (children) |child| try self.populateSymtable(&child);
},
@@ -221,17 +222,21 @@ pub const Populator = struct {
const output = try self.allocator.create(SymbType);
output.* = if (retType) |typ| table.getType(typ).? else SymbType.Void;
+ const typ = SymbType{
+ .Function = .{
+ .output = output,
+ .input = input,
+ },
+ };
+ const id = self.reserveId();
+
+ _ = try table.insert("func_" ++ .{@as(u8, @truncate(id))}, typ.toSymb());
return Symbol{
.Value = SymbValue{
.mut = true,
- .id = self.reserveId(),
- .typ = SymbType{
- .Function = .{
- .input = input,
- .output = output,
- },
- },
+ .id = id,
+ .typ = typ,
},
};
}