refactor executeImportDirective

This commit is contained in:
Irmen de Jong 2021-10-13 23:14:27 +02:00
parent 3281d9a215
commit aaff484306
2 changed files with 26 additions and 29 deletions

View File

@ -78,37 +78,34 @@ class ModuleImporter(private val program: Program,
val existing = program.modules.singleOrNull { it.name == moduleName }
if (existing!=null)
return null // TODO: why return null instead of Module instance?
return existing
// try internal library first
val moduleResourceSrc = getModuleFromResource("$moduleName.p8", compilationTargetName)
var importedModule: Module? = null
moduleResourceSrc.fold(
success = {
println("importing '$moduleName' (from internal ${it.origin})")
importedModule=importModule(it)
},
failure = {
// try filesystem next
val moduleSrc = getModuleFromFile(moduleName, importingModule)
moduleSrc.fold(
success = {
println("importing '$moduleName' (from file ${it.origin})")
importedModule = importModule(it)
},
failure = {
errors.err("no module found with name $moduleName", import.position)
}
)
}
)
val importedModule =
moduleResourceSrc.fold(
success = {
println("importing '$moduleName' (from internal ${it.origin})")
importModule(it)
},
failure = {
// try filesystem next
val moduleSrc = getModuleFromFile(moduleName, importingModule)
moduleSrc.fold(
success = {
println("importing '$moduleName' (from file ${it.origin})")
importModule(it)
},
failure = {
errors.err("no module found with name $moduleName", import.position)
return null
}
)
}
)
return if(importedModule==null)
null
else {
removeDirectivesFromImportedModule(importedModule!!)
importedModule
}
removeDirectivesFromImportedModule(importedModule)
return importedModule
}
private fun removeDirectivesFromImportedModule(importedModule: Module) {

View File

@ -250,14 +250,14 @@ class TestModuleImporter {
val result = importer.importLibraryModule(filenameNoExt)
assertThat(count[n] + " call / NO .p8 extension", result, Is(nullValue()))
assertFalse(errors.noErrors(), count[n] + " call / NO .p8 extension")
assertEquals(errors.errors.single(), "imported file not found: i_do_not_exist.p8")
assertEquals(errors.errors.single(), "no module found with name i_do_not_exist")
errors.report()
assertThat(program.modules.size, equalTo(1))
val result2 = importer.importLibraryModule(filenameWithExt)
assertThat(count[n] + " call / with .p8 extension", result2, Is(nullValue()))
assertFalse(importer.errors.noErrors(), count[n] + " call / with .p8 extension")
assertEquals(errors.errors.single(), "imported file not found: i_do_not_exist.p8.p8") // TODO don't duplicate the p8 extension in the import logic...
assertEquals(errors.errors.single(), "no module found with name i_do_not_exist.p8") // TODO don't add a p8 extension in the import logic...
errors.report()
assertThat(program.modules.size, equalTo(1))
}