mirror of
https://github.com/irmen/prog8.git
synced 2024-12-25 08:29:25 +00:00
refactor tryGetModuleFromFile
This commit is contained in:
parent
0447b3e4cc
commit
c4523ea470
@ -1,8 +1,6 @@
|
|||||||
package prog8.compiler
|
package prog8.compiler
|
||||||
|
|
||||||
import com.github.michaelbull.result.Err
|
import com.github.michaelbull.result.*
|
||||||
import com.github.michaelbull.result.Ok
|
|
||||||
import com.github.michaelbull.result.Result
|
|
||||||
import prog8.ast.Module
|
import prog8.ast.Module
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
@ -11,6 +9,7 @@ import prog8.ast.statements.Directive
|
|||||||
import prog8.ast.statements.DirectiveArg
|
import prog8.ast.statements.DirectiveArg
|
||||||
import prog8.parser.Prog8Parser
|
import prog8.parser.Prog8Parser
|
||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
|
import java.io.File
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import kotlin.io.path.*
|
import kotlin.io.path.*
|
||||||
|
|
||||||
@ -20,11 +19,11 @@ class ModuleImporter(private val program: Program,
|
|||||||
val errors: IErrorReporter,
|
val errors: IErrorReporter,
|
||||||
sourceDirs: List<String>) {
|
sourceDirs: List<String>) {
|
||||||
|
|
||||||
private val libpaths: List<Path> = sourceDirs.map { Path(it) }
|
private val sourcePaths: List<Path> = sourceDirs.map { Path(it) }
|
||||||
|
|
||||||
fun importModule(filePath: Path): Result<Module, NoSuchFileException> {
|
fun importModule(filePath: Path): Result<Module, NoSuchFileException> {
|
||||||
val currentDir = Path("").absolute()
|
val currentDir = Path("").absolute()
|
||||||
val searchIn = listOf(currentDir) + libpaths
|
val searchIn = listOf(currentDir) + sourcePaths
|
||||||
val candidates = searchIn
|
val candidates = searchIn
|
||||||
.map { it.absolute().div(filePath).normalize().absolute() }
|
.map { it.absolute().div(filePath).normalize().absolute() }
|
||||||
.filter { it.exists() }
|
.filter { it.exists() }
|
||||||
@ -81,19 +80,22 @@ 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?
|
||||||
|
|
||||||
var srcCode = tryGetModuleFromResource("$moduleName.p8", compilationTargetName)
|
val srcCode = tryGetModuleFromResource("$moduleName.p8", compilationTargetName)
|
||||||
val importedModule =
|
val importedModule =
|
||||||
if (srcCode != null) {
|
if (srcCode != null) {
|
||||||
println("importing '$moduleName' (from internal ${srcCode.origin})")
|
println("importing '$moduleName' (from internal ${srcCode.origin})")
|
||||||
importModule(srcCode)
|
importModule(srcCode)
|
||||||
} else {
|
} else {
|
||||||
srcCode = tryGetModuleFromFile(moduleName, importingModule)
|
val moduleSrc = getModuleFromFile(moduleName, importingModule)
|
||||||
if (srcCode == null) {
|
moduleSrc.fold(
|
||||||
errors.err("imported file not found: $moduleName.p8", import.position)
|
success = {
|
||||||
return null
|
importModule(it)
|
||||||
//throw NoSuchFileException(File("$moduleName.p8"))
|
},
|
||||||
}
|
failure = {
|
||||||
importModule(srcCode)
|
errors.err("no module found with name $moduleName", import.position)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
removeDirectivesFromImportedModule(importedModule)
|
removeDirectivesFromImportedModule(importedModule)
|
||||||
@ -122,13 +124,14 @@ class ModuleImporter(private val program: Program,
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun tryGetModuleFromFile(name: String, importingModule: Module?): SourceCode? {
|
private fun getModuleFromFile(name: String, importingModule: Module?): Result<SourceCode, NoSuchFileException> {
|
||||||
val fileName = "$name.p8"
|
val fileName = "$name.p8"
|
||||||
val locations =
|
val locations =
|
||||||
if (importingModule == null) { // <=> imported from library module
|
if (importingModule == null) { // <=> imported from library module
|
||||||
libpaths
|
sourcePaths
|
||||||
} else {
|
} else {
|
||||||
libpaths.drop(1) + // TODO: why drop the first?
|
val dropCurDir = if(sourcePaths[0].name == ".") 1 else 0
|
||||||
|
sourcePaths.drop(dropCurDir) +
|
||||||
// FIXME: won't work until Prog8Parser is fixed s.t. it fully initialzes the modules it returns
|
// FIXME: won't work until Prog8Parser is fixed s.t. it fully initialzes the modules it returns
|
||||||
listOf(Path(importingModule.position.file).parent ?: Path("")) +
|
listOf(Path(importingModule.position.file).parent ?: Path("")) +
|
||||||
listOf(Path(".", "prog8lib"))
|
listOf(Path(".", "prog8lib"))
|
||||||
@ -136,12 +139,11 @@ class ModuleImporter(private val program: Program,
|
|||||||
|
|
||||||
locations.forEach {
|
locations.forEach {
|
||||||
try {
|
try {
|
||||||
return SourceCode.File(it.resolve(fileName))
|
return Ok(SourceCode.File(it.resolve(fileName)))
|
||||||
} catch (e: NoSuchFileException) {
|
} catch (e: NoSuchFileException) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//throw ParsingFailedError("$position Import: no module source file '$fileName' found (I've looked in: embedded libs and $locations)")
|
return Err(NoSuchFileException(File("name")))
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user