From f0876bd5acc78c39bf4209169faf95b822e73b29 Mon Sep 17 00:00:00 2001 From: William Bell Date: Wed, 29 May 2024 23:15:49 +0100 Subject: [PATCH] fix being able to run a script in a parent folder from a child folder --- src/import.go | 7 ++++++- src/main.go | 2 +- src/parseImport.go | 4 ++-- tests/circular_import.ar | 3 ++- tests/circular_test.ar | 5 ++--- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/import.go b/src/import.go index c74e07f..6b9e325 100644 --- a/src/import.go +++ b/src/import.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "fmt" "os" "path/filepath" ) @@ -97,7 +98,7 @@ func __runTranslatedImport(translatedImport translatedImport, global ArObject, m return local, ArErr{} } -func translateImport(realpath string, origin string) (translatedImport, ArErr) { +func translateImport(realpath string, origin string, topLevelOnly bool) (translatedImport, ArErr) { extention := filepath.Ext(realpath) path := realpath if extention == "" { @@ -125,9 +126,13 @@ func translateImport(realpath string, origin string) (translatedImport, ArErr) { filepath.Join(currentPath, realpath, "init.ar"), filepath.Join(currentPath, modules_folder, path), filepath.Join(currentPath, modules_folder, realpath, "init.ar")) + if topLevelOnly { + break + } oldPath = currentPath currentPath = filepath.Dir(currentPath) } + fmt.Println(pathsToTest) } var p string var found bool diff --git a/src/main.go b/src/main.go index 0a79f53..900ddcd 100644 --- a/src/main.go +++ b/src/main.go @@ -56,7 +56,7 @@ func main() { if e != nil { panic(e) } - translated, err := translateImport(Args[0], ex) + translated, err := translateImport(Args[0], ex, true) if err.EXISTS { panicErr(err) os.Exit(1) diff --git a/src/parseImport.go b/src/parseImport.go index f4f6f84..d9df689 100644 --- a/src/parseImport.go +++ b/src/parseImport.go @@ -80,7 +80,7 @@ func parseGenericImport(code UNPARSEcode, index int, codeline []UNPARSEcode) (Ar } if str, ok := toImport.(string); ok { - resp, err := translateImport(str, filepath.Dir(filepath.ToSlash(code.path))) + resp, err := translateImport(str, filepath.Dir(filepath.ToSlash(code.path)), false) if !err.EXISTS { importOBJ.translated = resp importOBJ.pretranslated = true @@ -102,7 +102,7 @@ func runImport(importOBJ ArImport, stack stack, stacklevel int) (any, ArErr) { return nil, ArErr{"Type Error", "import requires a string, got type '" + typeof(val) + "'", importOBJ.Line, importOBJ.Path, importOBJ.Code, true} } parent := filepath.Dir(filepath.ToSlash(importOBJ.Path)) - translated, err = translateImport(val.(string), parent) + translated, err = translateImport(val.(string), parent, false) if err.EXISTS { if err.line == 0 { err.line = importOBJ.Line diff --git a/tests/circular_import.ar b/tests/circular_import.ar index 71b749a..69caff6 100644 --- a/tests/circular_import.ar +++ b/tests/circular_import.ar @@ -1 +1,2 @@ -import "circular_test" \ No newline at end of file +import "circular_test" +term.log("epic wow") \ No newline at end of file diff --git a/tests/circular_test.ar b/tests/circular_test.ar index ae79a71..9f21f9c 100644 --- a/tests/circular_test.ar +++ b/tests/circular_test.ar @@ -1,3 +1,2 @@ - -term.log("hello") -import "circular_import" \ No newline at end of file +import "circular_import" +term.log("hello") \ No newline at end of file