From 0bac8e3585b2f937009903adddc3e6a4c09e7532 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Thu, 17 Oct 2024 16:26:43 +0300 Subject: [PATCH] jule: add file annotation support for test files --- std/jule/importer/annotation.jule | 109 ++++++++++++++------------ std/jule/importer/directive_eval.jule | 10 +-- std/jule/importer/importer.jule | 2 +- std/jule/importer/var.jule | 12 +++ 4 files changed, 74 insertions(+), 59 deletions(-) diff --git a/std/jule/importer/annotation.jule b/std/jule/importer/annotation.jule index 2e0392c59..733a32526 100644 --- a/std/jule/importer/annotation.jule +++ b/std/jule/importer/annotation.jule @@ -46,64 +46,75 @@ fn checkArch(arg: str): (ok: bool, exist: bool) { ret } -// Reports whether file path passes file annotation by current system. -fn isPassFileAnnotation(mut p: str): bool { - p = filepath::Base(p) - n := len(p) - p = p[:n-len(filepath::Ext(p))] +impl Importer { + // Reports whether file path passes file annotation by current system. + fn isPassFileAnnotation(self, mut p: str): bool { + p = filepath::Base(p) + n := len(p) + p = p[:n-len(filepath::Ext(p))] - // a1 is the second annotation. - // Should be architecture annotation if exist annotation 2 (aka a2), - // can operating system or architecture annotation if not. - mut a1 := "" - // a2 is first filter. - // Should be operating system filter if exist and valid annotation. - mut a2 := "" + if strings::HasSuffix(p, "_test") { + if findVar(self.vars, "test") == -1 { + // file have _test suffix and test compilation is not enabled + // so this file should be ignored + ret false + } + p = p[:len(p)-len("_test")] + } - // Annotation 1 - mut i := strings::FindLastByte(p, '_') - if i == -1 { - // Check file name directly if not exist any _ character. - mut ok, mut exist := checkOs(p) - if exist { - ret ok + // a1 is the second annotation. + // Should be architecture annotation if exist annotation 2 (aka a2), + // can operating system or architecture annotation if not. + mut a1 := "" + // a2 is first filter. + // Should be operating system filter if exist and valid annotation. + mut a2 := "" + + // Annotation 1 + mut i := strings::FindLastByte(p, '_') + if i == -1 { + // Check file name directly if not exist any _ character. + mut ok, mut exist := checkOs(p) + if exist { + ret ok + } + ok, exist = checkArch(p) + ret !exist || ok } - ok, exist = checkArch(p) - ret !exist || ok - } - if i+1 >= n { - ret true - } - a1 = p[i+1:] + if i+1 >= n { + ret true + } + a1 = p[i+1:] - p = p[:i] + p = p[:i] - // Annotation 2 - i = strings::FindLastByte(p, '_') - if i != -1 { - a2 = p[i+1:] - } + // Annotation 2 + i = strings::FindLastByte(p, '_') + if i != -1 { + a2 = p[i+1:] + } - if a2 == "" { - mut ok, mut exist := checkOs(a1) - if exist { - ret ok + if a2 == "" { + mut ok, mut exist := checkOs(a1) + if exist { + ret ok + } + ok, exist = checkArch(a1) + ret !exist || ok } - ok, exist = checkArch(a1) - ret !exist || ok - } - mut ok, mut exist := checkArch(a1) - if exist { - if !ok { - ret false + mut ok, mut exist := checkArch(a1) + if exist { + if !ok { + ret false + } + ok, exist = checkOs(a2) + ret !exist || ok } - ok, exist = checkOs(a2) + + // a1 is not architecture, for this reason bad couple pattern. + // Accept as one pattern, so a1 can be platform. + ok, exist = checkOs(a1) ret !exist || ok } - - // a1 is not architecture, for this reason bad couple pattern. - // Accept as one pattern, so a1 can be platform. - ok, exist = checkOs(a1) - ret !exist || ok } \ No newline at end of file diff --git a/std/jule/importer/directive_eval.jule b/std/jule/importer/directive_eval.jule index a44249582..54729b23a 100644 --- a/std/jule/importer/directive_eval.jule +++ b/std/jule/importer/directive_eval.jule @@ -62,15 +62,7 @@ impl directiveEval { // Eval directive variable. fn evalDirectiveIdent(self, ident: str): bool { - if ident == "" { - ret false - } - for _, var in self.vars { - if var == ident { - ret true - } - } - ret false + ret findVar(self.vars, ident) >= 0 } // Eval directive expression part. diff --git a/std/jule/importer/importer.jule b/std/jule/importer/importer.jule index 6458ff8cf..7cf605296 100644 --- a/std/jule/importer/importer.jule +++ b/std/jule/importer/importer.jule @@ -118,7 +118,7 @@ impl sema::Importer for Importer { } // Skip this source file if file annotation is failed. - if !isPassFileAnnotation(dirent.Name) { + if !self.isPassFileAnnotation(dirent.Name) { continue } diff --git a/std/jule/importer/var.jule b/std/jule/importer/var.jule index 5b9a1052a..dbc376161 100644 --- a/std/jule/importer/var.jule +++ b/std/jule/importer/var.jule @@ -79,4 +79,16 @@ fn initVars(mut &vars: []str, &info: CompileInfo) { | CppStd.Cpp20: vars = append(vars, "cpp20") } +} + +fn findVar(vars: []str, ident: str): int { + if ident == "" { + ret -1 + } + for i, var in vars { + if var == ident { + ret i + } + } + ret -1 } \ No newline at end of file