refactor: address interning review feedback
This commit is contained in:
+20
-19
@@ -20,7 +20,7 @@ Builtin :: struct {
|
||||
}
|
||||
|
||||
@hide:file
|
||||
builtins [8]Builtin :: [
|
||||
builtins [_]Builtin :: [
|
||||
{ name = "int", type = .int },
|
||||
{ name = "float", type = .float },
|
||||
{ name = "bool", type = .bool },
|
||||
@@ -31,6 +31,17 @@ builtins [8]Builtin :: [
|
||||
{ name = "usize", type = .usize },
|
||||
]
|
||||
|
||||
@hide:file
|
||||
intern_builtins proc(
|
||||
$N usize,
|
||||
pool @mut strpool.StringPool,
|
||||
entries @[N]Builtin,
|
||||
) [N]mut strpool.StringId ! (mem.AllocError | strpool.InternError) {
|
||||
ids [N]mut strpool.StringId := undefined
|
||||
for (entries) |builtin, index| ids[index] = try strpool.intern(pool, builtin.name)
|
||||
return ids
|
||||
}
|
||||
|
||||
BindingKind :: enum { none, declaration, builtin, invalid }
|
||||
|
||||
BindingData :: union {
|
||||
@@ -56,7 +67,7 @@ Resolution :: struct { storage Storage }
|
||||
|
||||
@hide:file
|
||||
Scope :: struct {
|
||||
names hashmap.HashMap(strpool.StringId, Binding, name_hash, names_equal)
|
||||
names hashmap.AutoHashMap(strpool.StringId, Binding)
|
||||
parent ?@Scope
|
||||
}
|
||||
|
||||
@@ -65,7 +76,7 @@ State :: struct {
|
||||
tree @ast.Tree
|
||||
diagnostics @mut diagnostics.Store
|
||||
bindings []mut Binding
|
||||
builtin_ids [8]mut strpool.StringId = [0, 0, 0, 0, 0, 0, 0, 0]
|
||||
builtin_ids []strpool.StringId
|
||||
allocator mem.Allocator
|
||||
}
|
||||
|
||||
@@ -81,19 +92,18 @@ resolve proc(
|
||||
bindings :: try mem.alloc(Binding, allocator, ast.node_count(tree))
|
||||
errdefer mem.free(allocator, bindings)
|
||||
mem.set(bindings, Binding{})
|
||||
builtin_ids :: try intern_builtins(pool, &builtins)
|
||||
|
||||
state := State{
|
||||
tree = tree,
|
||||
diagnostics = diagnostic_store,
|
||||
bindings = bindings,
|
||||
builtin_ids = builtin_ids[..],
|
||||
allocator = allocator,
|
||||
}
|
||||
|
||||
for (builtins) |builtin, index| {
|
||||
state.builtin_ids[index] = try strpool.intern(pool, builtin.name)
|
||||
}
|
||||
package_scope := Scope{
|
||||
names = hashmap.init(strpool.StringId, Binding, name_hash, names_equal, allocator),
|
||||
names = hashmap.init(allocator),
|
||||
parent = null,
|
||||
}
|
||||
defer hashmap.deinit(&package_scope.names)
|
||||
@@ -196,7 +206,7 @@ resolve_function proc(
|
||||
}
|
||||
|
||||
scope := Scope{
|
||||
names = hashmap.init(strpool.StringId, Binding, name_hash, names_equal, state.allocator),
|
||||
names = hashmap.init(state.allocator),
|
||||
parent = package_scope,
|
||||
}
|
||||
defer hashmap.deinit(&scope.names)
|
||||
@@ -210,7 +220,7 @@ resolve_function proc(
|
||||
@hide:file
|
||||
resolve_arm proc(state @mut State, parent @Scope, id ast.NodeId) void ! (mem.AllocError | diagnostics.CapacityError) {
|
||||
scope := Scope{
|
||||
names = hashmap.init(strpool.StringId, Binding, name_hash, names_equal, state.allocator),
|
||||
names = hashmap.init(state.allocator),
|
||||
parent = parent,
|
||||
}
|
||||
defer hashmap.deinit(&scope.names)
|
||||
@@ -255,7 +265,7 @@ resolve_block proc(state @mut State, scope @mut Scope, id ast.NodeId) void ! (me
|
||||
loop :: ast.for_stmt(tree, statement)
|
||||
try resolve_expr(state, scope, loop.iterable)
|
||||
body_scope := Scope{
|
||||
names = hashmap.init(strpool.StringId, Binding, name_hash, names_equal, state.allocator),
|
||||
names = hashmap.init(state.allocator),
|
||||
parent = scope,
|
||||
}
|
||||
defer hashmap.deinit(&body_scope.names)
|
||||
@@ -352,12 +362,3 @@ resolve_expr proc(
|
||||
}
|
||||
}
|
||||
|
||||
@hide:file
|
||||
name_hash proc(name strpool.StringId) usize {
|
||||
return usize(name)
|
||||
}
|
||||
|
||||
@hide:file
|
||||
names_equal proc(left, right strpool.StringId) bool {
|
||||
return left == right
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user