fix being able to run a script in a parent folder from a child folder

This commit is contained in:
2024-05-29 23:15:49 +01:00
parent 37a3d56b5f
commit f0876bd5ac
5 changed files with 13 additions and 8 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -1 +1,2 @@
import "circular_test"
import "circular_test"
term.log("epic wow")

View File

@@ -1,3 +1,2 @@
term.log("hello")
import "circular_import"
import "circular_import"
term.log("hello")