Skip to content

Commit

Permalink
jule: add file annotation support for test files
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Oct 17, 2024
1 parent 1dc7533 commit 0bac8e3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 59 deletions.
109 changes: 60 additions & 49 deletions std/jule/importer/annotation.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 1 addition & 9 deletions std/jule/importer/directive_eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion std/jule/importer/importer.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions std/jule/importer/var.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0bac8e3

Please sign in to comment.