lang: ast: Add a util function for node map copying

This commit is contained in:
James Shubin
2024-01-22 13:04:23 -05:00
parent 16dfb7b5f5
commit f8077d9fc4
2 changed files with 10 additions and 9 deletions

View File

@@ -3125,15 +3125,7 @@ func (obj *StmtProg) Ordering(produces map[string]interfaces.Node) (*pgraph.Grap
} }
} }
// TODO: move to a util package? newProduces := CopyNodeMapping(produces) // don't modify the input map!
cp := func(in map[string]interfaces.Node) map[string]interfaces.Node {
out := make(map[string]interfaces.Node)
for k, v := range in {
out[k] = v // copy the map, not the Node's
}
return out
}
newProduces := cp(produces) // don't modify the input map!
// Overwrite anything in this scope with the shadowed parent variable! // Overwrite anything in this scope with the shadowed parent variable!
for key, val := range prod { for key, val := range prod {

View File

@@ -265,3 +265,12 @@ func CollectFiles(ast interfaces.Stmt) ([]string, error) {
} }
return fileList, nil return fileList, nil
} }
// CopyNodeMapping copies the map of string to node and is used in Ordering.
func CopyNodeMapping(in map[string]interfaces.Node) map[string]interfaces.Node {
out := make(map[string]interfaces.Node)
for k, v := range in {
out[k] = v // copy the map, not the Node's
}
return out
}