From e8d25d231c178c9c19e4d72ddce16890bba9b221 Mon Sep 17 00:00:00 2001 From: William Bell Date: Sat, 6 Apr 2024 15:48:00 +0100 Subject: [PATCH] get rid of circular imports --- src/import.go | 2 +- src/parseImport.go | 20 +++++--------------- src/translate.go | 4 ++-- tests/circular_test.ar | 2 ++ 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/import.go b/src/import.go index d47db18..1b8c938 100644 --- a/src/import.go +++ b/src/import.go @@ -85,11 +85,11 @@ func __runTranslatedImport(translatedImport translatedImport, global ArObject, m "main": main, }), }) + imported[translatedImport.p] = local _, runimeErr := run(translatedImport.translated, stack{global, localvars, local}) if runimeErr.EXISTS { return ArObject{}, runimeErr } - imported[translatedImport.p] = local return local, ArErr{} } diff --git a/src/parseImport.go b/src/parseImport.go index e7d8c59..f4f6f84 100644 --- a/src/parseImport.go +++ b/src/parseImport.go @@ -5,7 +5,7 @@ import ( "strings" ) -var genericImportCompiled = makeRegex(`import( )+(.|\n)+(( )+as( )+([a-zA-Z_]|(\p{L}\p{M}*))([a-zA-Z0-9_]|(\p{L}\p{M}*))*)?( *)`) +var genericImportCompiled = makeRegex(`( *)import( )+(.|\n)+(( )+as( )+([a-zA-Z_]|(\p{L}\p{M}*))([a-zA-Z0-9_]|(\p{L}\p{M}*))*)?( *)`) type ArImport struct { pretranslated bool @@ -80,20 +80,10 @@ func parseGenericImport(code UNPARSEcode, index int, codeline []UNPARSEcode) (Ar } if str, ok := toImport.(string); ok { - importOBJ.pretranslated = true - var err ArErr - importOBJ.translated, err = translateImport(str, filepath.Dir(filepath.ToSlash(code.path))) - if err.EXISTS { - if err.line == 0 { - err.line = importOBJ.Line - } - if err.path == "" { - err.path = importOBJ.Path - } - if err.code == "" { - err.code = importOBJ.Code - } - return importOBJ, false, err, i + resp, err := translateImport(str, filepath.Dir(filepath.ToSlash(code.path))) + if !err.EXISTS { + importOBJ.translated = resp + importOBJ.pretranslated = true } } diff --git a/src/translate.go b/src/translate.go index 5822752..2a78de1 100644 --- a/src/translate.go +++ b/src/translate.go @@ -31,7 +31,7 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i err ArErr = ArErr{"Syntax Error", "invalid syntax", code.line, code.path, code.realcode, true} i int = 1 ) - if isLine == 3 { + if isLine >= 3 { if isComment(code) { resp, worked, err, i = parseComment(code, index, codelines) if worked { @@ -251,7 +251,7 @@ func translate(codelines []UNPARSEcode) ([]any, ArErr) { if currentindent != 0 { return nil, ArErr{"Syntax Error", "invalid indent", codelines[i].line, codelines[i].path, codelines[i].realcode, true} } - val, _, err, step := translateVal(codelines[i], i, codelines, 3) + val, _, err, step := translateVal(codelines[i], i, codelines, 4) i += step if err.EXISTS { return nil, err diff --git a/tests/circular_test.ar b/tests/circular_test.ar index fac8c91..ae79a71 100644 --- a/tests/circular_test.ar +++ b/tests/circular_test.ar @@ -1 +1,3 @@ + +term.log("hello") import "circular_import" \ No newline at end of file