From 9d8beb85d716a4df11f43299e311335ef6efebf6 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 22 Feb 2024 17:59:49 -0500 Subject: [PATCH] lang: ast: Refactor import scope for sanity This makes it easier to integrate future additions to the import code. --- lang/ast/structs.go | 46 +++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index 08a10f47..e2e95d8f 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -3186,24 +3186,8 @@ func (obj *StmtProg) Ordering(produces map[string]interfaces.Node) (*pgraph.Grap return graph, newCons, nil } -// importScope is a helper function called from SetScope. If it can't find a -// particular scope, then it can also run the downloader if it is available. -func (obj *StmtProg) importScope(info *interfaces.ImportData, scope *interfaces.Scope) (*interfaces.Scope, error) { - if obj.data.Debug { - obj.data.Logf("import: %s", info.Name) - } - // the abs file path that we started actively running SetScope on is: - // obj.data.Base + obj.data.Metadata.Main - // but recursive imports mean this is not always the active file... - - if info.IsSystem { // system imports are the exact name, eg "fmt" - systemScope, err := obj.importSystemScope(info.Name) - if err != nil { - return nil, errwrap.Wrapf(err, "system import of `%s` failed", info.Name) - } - return systemScope, nil - } - +// nextVertex is a helper function that builds a vertex for recursion detection. +func (obj *StmtProg) nextVertex(info *interfaces.ImportData) (*pgraph.SelfVertex, error) { // graph-based recursion detection // TODO: is this sufficiently unique, but not incorrectly unique? // TODO: do we need to clean uvid for consistency so the compare works? @@ -3238,6 +3222,32 @@ func (obj *StmtProg) importScope(info *interfaces.ImportData, scope *interfaces. return nil, errwrap.Wrapf(err, "recursive import of: `%s`", info.Name) } + return nextVertex, nil +} + +// importScope is a helper function called from SetScope. If it can't find a +// particular scope, then it can also run the downloader if it is available. +func (obj *StmtProg) importScope(info *interfaces.ImportData, scope *interfaces.Scope) (*interfaces.Scope, error) { + if obj.data.Debug { + obj.data.Logf("import: %s", info.Name) + } + // the abs file path that we started actively running SetScope on is: + // obj.data.Base + obj.data.Metadata.Main + // but recursive imports mean this is not always the active file... + + if info.IsSystem { // system imports are the exact name, eg "fmt" + systemScope, err := obj.importSystemScope(info.Name) + if err != nil { + return nil, errwrap.Wrapf(err, "system import of `%s` failed", info.Name) + } + return systemScope, nil + } + + nextVertex, err := obj.nextVertex(info) // everyone below us uses this! + if err != nil { + return nil, err + } + if info.IsLocal { // append the relative addition of where the running code is, on // to the base path that the metadata file (data) is relative to