Refactor unsafe pointer usage to use reflect.Type and pointers
Remove internal/danger package and replace unsafe pointer arithmetic with direct pointer manipulation. Update AST node references to use pointers instead of integer offsets. This improves code safety and maintainability. Co-authored-by: thomas.pelletier <thomas.pelletier@bedrockrobotics.com>
This commit is contained in:
+25
-5
@@ -29,8 +29,10 @@ func (r reference) Valid() bool {
|
||||
}
|
||||
|
||||
type builder struct {
|
||||
tree root
|
||||
lastIdx int
|
||||
tree root
|
||||
lastIdx int
|
||||
nextOffsets []int
|
||||
childOffsets []int
|
||||
}
|
||||
|
||||
func (b *builder) Tree() *root {
|
||||
@@ -43,29 +45,47 @@ func (b *builder) NodeAt(ref reference) *Node {
|
||||
|
||||
func (b *builder) Reset() {
|
||||
b.tree.nodes = b.tree.nodes[:0]
|
||||
b.nextOffsets = b.nextOffsets[:0]
|
||||
b.childOffsets = b.childOffsets[:0]
|
||||
b.lastIdx = 0
|
||||
}
|
||||
|
||||
func (b *builder) Push(n Node) reference {
|
||||
b.lastIdx = len(b.tree.nodes)
|
||||
b.tree.nodes = append(b.tree.nodes, n)
|
||||
b.nextOffsets = append(b.nextOffsets, 0)
|
||||
b.childOffsets = append(b.childOffsets, 0)
|
||||
return reference(b.lastIdx)
|
||||
}
|
||||
|
||||
func (b *builder) PushAndChain(n Node) reference {
|
||||
newIdx := len(b.tree.nodes)
|
||||
b.tree.nodes = append(b.tree.nodes, n)
|
||||
b.nextOffsets = append(b.nextOffsets, 0)
|
||||
b.childOffsets = append(b.childOffsets, 0)
|
||||
|
||||
if b.lastIdx >= 0 {
|
||||
b.tree.nodes[b.lastIdx].next = newIdx - b.lastIdx
|
||||
b.nextOffsets[b.lastIdx] = newIdx - b.lastIdx
|
||||
}
|
||||
b.lastIdx = newIdx
|
||||
return reference(b.lastIdx)
|
||||
}
|
||||
|
||||
func (b *builder) AttachChild(parent reference, child reference) {
|
||||
b.tree.nodes[parent].child = int(child) - int(parent)
|
||||
b.childOffsets[parent] = int(child) - int(parent)
|
||||
}
|
||||
|
||||
func (b *builder) Chain(from reference, to reference) {
|
||||
b.tree.nodes[from].next = int(to) - int(from)
|
||||
b.nextOffsets[from] = int(to) - int(from)
|
||||
}
|
||||
|
||||
func (b *builder) Link() {
|
||||
for i := range b.tree.nodes {
|
||||
if next := b.nextOffsets[i]; next != 0 {
|
||||
b.tree.nodes[i].next = &b.tree.nodes[i+next]
|
||||
}
|
||||
if child := b.childOffsets[i]; child != 0 {
|
||||
b.tree.nodes[i].child = &b.tree.nodes[i+child]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user