mirror of
https://github.com/irmen/prog8.git
synced 2024-11-23 07:32:10 +00:00
refactor tryGetModuleFromResource
This commit is contained in:
parent
c4523ea470
commit
0fcd61e00f
@ -80,26 +80,35 @@ class ModuleImporter(private val program: Program,
|
|||||||
if (existing!=null)
|
if (existing!=null)
|
||||||
return null // TODO: why return null instead of Module instance?
|
return null // TODO: why return null instead of Module instance?
|
||||||
|
|
||||||
val srcCode = tryGetModuleFromResource("$moduleName.p8", compilationTargetName)
|
// try internal library first
|
||||||
val importedModule =
|
val moduleResourceSrc = getModuleFromResource("$moduleName.p8", compilationTargetName)
|
||||||
if (srcCode != null) {
|
var importedModule: Module? = null
|
||||||
println("importing '$moduleName' (from internal ${srcCode.origin})")
|
moduleResourceSrc.fold(
|
||||||
importModule(srcCode)
|
success = {
|
||||||
} else {
|
println("importing '$moduleName' (from internal ${it.origin})")
|
||||||
|
importedModule=importModule(it)
|
||||||
|
},
|
||||||
|
failure = {
|
||||||
|
// try filesystem next
|
||||||
val moduleSrc = getModuleFromFile(moduleName, importingModule)
|
val moduleSrc = getModuleFromFile(moduleName, importingModule)
|
||||||
moduleSrc.fold(
|
moduleSrc.fold(
|
||||||
success = {
|
success = {
|
||||||
importModule(it)
|
println("importing '$moduleName' (from file ${it.origin})")
|
||||||
|
importedModule = importModule(it)
|
||||||
},
|
},
|
||||||
failure = {
|
failure = {
|
||||||
errors.err("no module found with name $moduleName", import.position)
|
errors.err("no module found with name $moduleName", import.position)
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
removeDirectivesFromImportedModule(importedModule)
|
return if(importedModule==null)
|
||||||
return importedModule
|
null
|
||||||
|
else {
|
||||||
|
removeDirectivesFromImportedModule(importedModule!!)
|
||||||
|
importedModule
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun removeDirectivesFromImportedModule(importedModule: Module) {
|
private fun removeDirectivesFromImportedModule(importedModule: Module) {
|
||||||
@ -111,17 +120,12 @@ class ModuleImporter(private val program: Program,
|
|||||||
importedModule.statements.addAll(0, directives)
|
importedModule.statements.addAll(0, directives)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun tryGetModuleFromResource(name: String, compilationTargetName: String): SourceCode? {
|
private fun getModuleFromResource(name: String, compilationTargetName: String): Result<SourceCode, NoSuchFileException> {
|
||||||
// try target speficic first
|
val result =
|
||||||
try {
|
runCatching { SourceCode.Resource("/prog8lib/$compilationTargetName/$name") }
|
||||||
return SourceCode.Resource("/prog8lib/$compilationTargetName/$name")
|
.orElse { runCatching { SourceCode.Resource("/prog8lib/$name") } }
|
||||||
} catch (e: FileSystemException) {
|
|
||||||
}
|
return result.mapError { NoSuchFileException(File(name)) }
|
||||||
try {
|
|
||||||
return SourceCode.Resource("/prog8lib/$name")
|
|
||||||
} catch (e: FileSystemException) {
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getModuleFromFile(name: String, importingModule: Module?): Result<SourceCode, NoSuchFileException> {
|
private fun getModuleFromFile(name: String, importingModule: Module?): Result<SourceCode, NoSuchFileException> {
|
||||||
|
Loading…
Reference in New Issue
Block a user