refactor: integrate compilation-owned identifier interning
This commit is contained in:
@@ -4,13 +4,11 @@ import "@std/hashmap"
|
||||
|
||||
InternError :: enum { out_of_space }
|
||||
|
||||
#! identifies a string only within its owning pool.
|
||||
StringId :: alias u32
|
||||
|
||||
no_str :: maxval!(StringId)
|
||||
|
||||
#! global pool owned by main.hon; initialize before scanning and release with deinit.
|
||||
strings StringPool := undefined
|
||||
|
||||
#! retains its allocator and owns interned copies; release all storage with deinit.
|
||||
StringPool :: struct {
|
||||
ids hashmap.StringHashMap(StringId) # string → id
|
||||
|
||||
@@ -22,3 +22,19 @@ handles_interning test {
|
||||
try testing.expect_equal(null, invalid_str)
|
||||
try testing.expect_equal("hello", hello_str?)
|
||||
}
|
||||
|
||||
keeps_independent_pool_ids_local test {
|
||||
first := init(mem.c_allocator)
|
||||
defer deinit(&first)
|
||||
second := init(mem.c_allocator)
|
||||
defer deinit(&second)
|
||||
hello :: try intern(&first, "hello")
|
||||
world :: try intern(&second, "world")
|
||||
try testing.expect_equal(hello, world)
|
||||
try testing.expect_equal("hello", get_str(&first, hello)?)
|
||||
try testing.expect_equal("world", get_str(&second, world)?)
|
||||
different :: try intern(&first, "world")
|
||||
try testing.expect(different != hello)
|
||||
try testing.expect_equal(hello, try intern(&first, "hello"))
|
||||
try testing.expect_equal(world, try intern(&second, "world"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user