Skip to content

Commit d039105

Browse files
authored
refactor(sema): move ReferenceStack to separate file (#336)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Extracted reference-management logic into a dedicated module and updated the semantic builder to use that external component. This reorganizes internal structure, clarifies responsibilities, and reduces coupling between components. No user-facing behavior or functionality has changed. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 3299e0a commit d039105

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/Semantic/ReferenceStack.zig

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const ReferenceStack = @This();
2+
3+
frames: std.ArrayListUnmanaged(ReferenceIdList) = .{},
4+
5+
const ReferenceIdList = std.ArrayListUnmanaged(Reference.Id);
6+
7+
/// current frame
8+
pub fn curr(self: *ReferenceStack) *ReferenceIdList {
9+
assert(self.len() > 0);
10+
return &self.frames.items[self.len() - 1];
11+
}
12+
13+
/// parent frame. `null` when currently in root scope.
14+
pub fn parent(self: *ReferenceStack) ?*ReferenceIdList {
15+
return if (self.len() <= 1) null else &self.frames.items[self.len() - 2];
16+
}
17+
18+
/// current number of frames
19+
pub inline fn len(self: ReferenceStack) usize {
20+
return self.frames.items.len;
21+
}
22+
23+
pub fn enter(self: *ReferenceStack, alloc: Allocator) Allocator.Error!void {
24+
try self.frames.append(alloc, .{});
25+
}
26+
27+
/// Add an unresolved reference to the current frame
28+
pub fn append(self: *ReferenceStack, alloc: Allocator, ref: Reference.Id) Allocator.Error!void {
29+
try self.curr().append(alloc, ref);
30+
}
31+
32+
pub fn deinit(self: *ReferenceStack, alloc: Allocator) void {
33+
for (0..self.frames.items.len) |i| {
34+
self.frames.items[i].deinit(alloc);
35+
}
36+
self.frames.deinit(alloc);
37+
}
38+
39+
const std = @import("std");
40+
const assert = std.debug.assert;
41+
const Allocator = std.mem.Allocator;
42+
43+
const Semantic = @import("../Semantic.zig");
44+
const Reference = Semantic.Reference;

src/Semantic/SemanticBuilder.zig

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,55 +1608,6 @@ fn resolveReferencesInCurrentScope(self: *SemanticBuilder) Allocator.Error!void
16081608
}
16091609
}
16101610

1611-
const ReferenceStack = struct {
1612-
frames: std.ArrayListUnmanaged(ReferenceIdList) = .{},
1613-
1614-
const ReferenceIdList = std.ArrayListUnmanaged(Reference.Id);
1615-
1616-
fn init(alloc: Allocator) Allocator.Error!ReferenceStack {
1617-
var self: ReferenceStack = .{};
1618-
try self.frames.ensureTotalCapacity(alloc, 16);
1619-
1620-
return self;
1621-
}
1622-
1623-
/// current frame
1624-
pub fn curr(self: *ReferenceStack) *ReferenceIdList {
1625-
assert(self.len() > 0);
1626-
return &self.frames.items[self.len() - 1];
1627-
}
1628-
1629-
/// parent frame. `null` when currently in root scope.
1630-
pub fn parent(self: *ReferenceStack) ?*ReferenceIdList {
1631-
return if (self.len() <= 1) null else &self.frames.items[self.len() - 2];
1632-
}
1633-
1634-
/// current number of frames
1635-
inline fn len(self: ReferenceStack) usize {
1636-
return self.frames.items.len;
1637-
}
1638-
1639-
fn enter(self: *ReferenceStack, alloc: Allocator) Allocator.Error!void {
1640-
try self.frames.append(alloc, .{});
1641-
}
1642-
fn exit(self: *ReferenceStack, alloc: Allocator) void {
1643-
var frame = self.frames.pop();
1644-
frame.deinit(alloc);
1645-
}
1646-
1647-
/// Add an unresolved reference to the current frame
1648-
fn append(self: *ReferenceStack, alloc: Allocator, ref: Reference.Id) Allocator.Error!void {
1649-
try self.curr().append(alloc, ref);
1650-
}
1651-
1652-
fn deinit(self: *ReferenceStack, alloc: Allocator) void {
1653-
for (0..self.frames.items.len) |i| {
1654-
self.frames.items[i].deinit(alloc);
1655-
}
1656-
self.frames.deinit(alloc);
1657-
}
1658-
};
1659-
16601611
// =========================================================================
16611612
// ================================ MODULES ================================
16621613
// =========================================================================
@@ -1904,6 +1855,7 @@ const Scope = Semantic.Scope;
19041855
const Symbol = Semantic.Symbol;
19051856
const NodeLinks = Semantic.NodeLinks;
19061857
const Reference = Semantic.Reference;
1858+
const ReferenceStack = @import("ReferenceStack.zig");
19071859
const ModuleRecord = Semantic.ModuleRecord;
19081860

19091861
const std = @import("std");

0 commit comments

Comments
 (0)