From b7efd94147f32db07f9f11e13d537749a0cb5acb Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 21 Feb 2024 12:31:31 -0500 Subject: [PATCH] lang: Pass through the fs and be consistent in usage This simplifies the API by passing through the filesystem so that function signatures don't need to be as complicated, and furthermore use that consistently throughout. --- lang/ast/structs.go | 18 +++++++++--------- lang/gapi/gapi.go | 12 ++++++------ lang/inputs/inputs.go | 10 +++++++--- lang/interpret_test.go | 24 ++++++++++++------------ lang/lang.go | 6 +++--- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index 90510916..85e29944 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -3474,7 +3474,7 @@ func (obj *StmtProg) importSystemScope(name string) (*interfaces.Scope, error) { // importScopeWithInputs returns a local or remote scope from an inputs string. // The inputs string is the common frontend for a lot of our parsing decisions. func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, parentVertex *pgraph.SelfVertex) (*interfaces.Scope, error) { - output, err := inputs.ParseInput(s, obj.data.Fs) + input, err := inputs.ParseInput(s, obj.data.Fs) if err != nil { return nil, errwrap.Wrapf(err, "could not activate an input parser") } @@ -3484,12 +3484,12 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa // run recursion detection by checking for duplicates in the seen files // TODO: do the paths need to be cleaned for "../", etc before compare? //for _, name := range obj.data.Files { // existing seen files - // if util.StrInList(name, output.Files) { + // if util.StrInList(name, input.Files) { // return nil, fmt.Errorf("recursive import of: `%s`", name) // } //} - reader := bytes.NewReader(output.Main) + reader := bytes.NewReader(input.Main) // nested logger logf := func(format string, v ...interface{}) { @@ -3498,11 +3498,11 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa // build new list of files files := []string{} - files = append(files, output.Files...) + files = append(files, input.Files...) files = append(files, obj.data.Files...) // store a reference to the parent metadata - metadata := output.Metadata + metadata := input.Metadata metadata.Metadata = obj.data.Metadata // now run the lexer/parser to do the import @@ -3518,9 +3518,9 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa // init and validate the structure of the AST data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: obj.data.Fs, - FsURI: obj.data.FsURI, - Base: output.Base, // new base dir (absolute path) + Fs: input.FS, // formerly: obj.data.Fs, + FsURI: input.FS.URI(), // formerly: obj.data.FsURI, + Base: input.Base, // new base dir (absolute path) Files: files, Imports: parentVertex, // the parent vertex that imported me Metadata: metadata, @@ -3593,7 +3593,7 @@ func (obj *StmtProg) importScopeWithInputs(s string, scope *interfaces.Scope, pa obj.importProgs = append(obj.importProgs, prog) // collecting these here is more elegant (and possibly more efficient!) - obj.importFiles = append(obj.importFiles, output.Files...) // save for CollectFiles + obj.importFiles = append(obj.importFiles, input.Files...) // save for CollectFiles return prog.scope, nil } diff --git a/lang/gapi/gapi.go b/lang/gapi/gapi.go index a85774a1..fb952a99 100644 --- a/lang/gapi/gapi.go +++ b/lang/gapi/gapi.go @@ -263,9 +263,9 @@ func (obj *GAPI) Cli(cliInfo *gapi.CliInfo) (*gapi.Deploy, error) { // init and validate the structure of the AST data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: localFs, // the local fs! - FsURI: localFs.URI(), // TODO: is this right? - Base: output.Base, // base dir (absolute path) that this is rooted in + Fs: output.FS, // formerly: localFs // the local fs! + FsURI: output.FS.URI(), // formerly: localFs.URI() // TODO: is this right? + Base: output.Base, // base dir (absolute path) that this is rooted in Files: output.Files, Imports: importVertex, Metadata: output.Metadata, @@ -770,9 +770,9 @@ func (obj *GAPI) Get(getInfo *gapi.GetInfo) error { // init and validate the structure of the AST data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: localFs, // the local fs! - FsURI: localFs.URI(), // TODO: is this right? - Base: output.Base, // base dir (absolute path) that this is rooted in + Fs: output.FS, // formerly: localFs // the local fs! + FsURI: output.FS.URI(), // formerly: localFs.URI() // TODO: is this right? + Base: output.Base, // base dir (absolute path) that this is rooted in Files: output.Files, Imports: importVertex, Metadata: output.Metadata, diff --git a/lang/inputs/inputs.go b/lang/inputs/inputs.go index 6717956f..def251f4 100644 --- a/lang/inputs/inputs.go +++ b/lang/inputs/inputs.go @@ -66,9 +66,10 @@ var ( // ParsedInput is the output struct which contains all the information we need. type ParsedInput struct { //activated bool // if struct is not nil we're activated - Base string // base path (abs path with trailing slash) - Main []byte // contents of main entry mcl code - Files []string // files and dirs to copy to fs (abs paths) + FS engine.Fs // reference to the engine.Fs used to call the parse + Base string // base path (abs path with trailing slash) + Main []byte // contents of main entry mcl code + Files []string // files and dirs to copy to fs (abs paths) Metadata *interfaces.Metadata Workers []func(engine.Fs) error // copy files here that aren't listed! } @@ -214,6 +215,7 @@ func inputMetadata(s string, fs engine.Fs) (*ParsedInput, error) { return nil, errwrap.Wrapf(err, "could not build metadata") } return &ParsedInput{ + FS: fs, Base: basePath, Main: b, Files: files, @@ -261,6 +263,7 @@ func inputMcl(s string, fs engine.Fs) (*ParsedInput, error) { }, } return &ParsedInput{ + FS: fs, Base: dirify(filepath.Dir(s)), // base path with trailing slash Main: b, Files: []string{ @@ -363,6 +366,7 @@ func inputCode(s string, fs engine.Fs) (*ParsedInput, error) { } return &ParsedInput{ + FS: fs, Base: dirify(wd), Main: b, Files: []string{}, // they're already copied in diff --git a/lang/interpret_test.go b/lang/interpret_test.go index 790b6b1a..e8cdc4d8 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -374,10 +374,10 @@ func TestAstFunc1(t *testing.T) { data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: fs, - FsURI: fs.URI(), // TODO: is this right? - Base: output.Base, // base dir (absolute path) the metadata file is in - Files: output.Files, // no really needed here afaict + Fs: output.FS, // formerly: fs + FsURI: output.FS.URI(), // formerly: fs.URI() // TODO: is this right? + Base: output.Base, // base dir (absolute path) the metadata file is in + Files: output.Files, // no really needed here afaict Imports: importVertex, Metadata: output.Metadata, Modules: "/" + interfaces.ModuleDirectory, // not really needed here afaict @@ -904,10 +904,10 @@ func TestAstFunc2(t *testing.T) { data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: fs, - FsURI: "memmapfs:///", // we're in standalone mode - Base: output.Base, // base dir (absolute path) the metadata file is in - Files: output.Files, // no really needed here afaict + Fs: output.FS, // formerly: fs + FsURI: output.FS.URI(), + Base: output.Base, // base dir (absolute path) the metadata file is in + Files: output.Files, // no really needed here afaict Imports: importVertex, Metadata: output.Metadata, Modules: "/" + interfaces.ModuleDirectory, // not really needed here afaict @@ -1706,10 +1706,10 @@ func TestAstFunc3(t *testing.T) { data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: fs, - FsURI: "memmapfs:///", // we're in standalone mode - Base: output.Base, // base dir (absolute path) the metadata file is in - Files: output.Files, // no really needed here afaict + Fs: output.FS, // formerly: fs + FsURI: output.FS.URI(), + Base: output.Base, // base dir (absolute path) the metadata file is in + Files: output.Files, // no really needed here afaict Imports: importVertex, Metadata: output.Metadata, Modules: "/" + interfaces.ModuleDirectory, // not really needed here afaict diff --git a/lang/lang.go b/lang/lang.go index da4c5184..e99c271a 100644 --- a/lang/lang.go +++ b/lang/lang.go @@ -139,9 +139,9 @@ func (obj *Lang) Init() error { // init and validate the structure of the AST data := &interfaces.Data{ // TODO: add missing fields here if/when needed - Fs: obj.Fs, - FsURI: obj.FsURI, - Base: output.Base, // base dir (absolute path) the metadata file is in + Fs: output.FS, // formerly: obj.Fs + FsURI: output.FS.URI(), // formerly: obj.FsURI + Base: output.Base, // base dir (absolute path) the metadata file is in Files: output.Files, Imports: importVertex, Metadata: output.Metadata,