* *little bit* of cleanup in ModuleImporter - *only refactoring*

This commit is contained in:
meisl 2021-07-18 19:03:51 +02:00
parent 0d06e3ff22
commit 3b97a17648
2 changed files with 10 additions and 14 deletions

View File

@ -18,11 +18,10 @@ import prog8.compiler.target.asmGeneratorFor
import prog8.optimizer.*
import prog8.parser.ModuleImporter
import prog8.parser.ParsingFailedError
import prog8.parser.moduleName
import java.io.File
import java.io.InputStream
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.*
import kotlin.system.measureTimeMillis
@ -176,7 +175,7 @@ private fun parseImports(filepath: Path,
libdirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
println("Compiler target: ${compTarget.name}. Parsing...")
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
val programAst = Program(moduleName(filepath.fileName), mutableListOf(), bf, compTarget)
val programAst = Program(filepath.nameWithoutExtension, mutableListOf(), bf, compTarget)
bf.program = programAst
val importer = ModuleImporter(programAst, compTarget.name, libdirs)

View File

@ -7,12 +7,9 @@ import prog8.ast.base.SyntaxError
import prog8.ast.statements.Directive
import prog8.ast.statements.DirectiveArg
import java.io.File
import java.nio.file.Path
import kotlin.io.FileSystemException
import java.nio.file.Path // TODO: use kotlin.io.paths.Path instead
import java.nio.file.Paths // TODO: use kotlin.io.paths.Path instead
fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
import kotlin.io.path.*
class ModuleImporter(private val program: Program,
@ -20,10 +17,10 @@ class ModuleImporter(private val program: Program,
private val libdirs: List<String>) {
fun importModule(filePath: Path): Module {
print("importing '${moduleName(filePath.fileName)}'")
print("importing '${filePath.nameWithoutExtension}'")
if (filePath.parent != null) { // TODO: use Path.relativize
var importloc = filePath.toString()
val curdir = Paths.get("").toAbsolutePath().toString()
val curdir = Path("").absolutePathString()
if(importloc.startsWith(curdir))
importloc = "." + importloc.substring(curdir.length)
println(" (from '$importloc')")
@ -124,22 +121,22 @@ class ModuleImporter(private val program: Program,
private fun tryGetModuleFromFile(name: String, importingModule: Module?): SourceCode? {
val fileName = "$name.p8"
val libpaths = libdirs.map { Path.of(it) }
val libpaths = libdirs.map { Path(it) }
val locations =
if (importingModule == null) { // <=> imported from library module
libpaths
} else {
libpaths.drop(1) + // TODO: why drop the first?
// FIXME: won't work until Prog8Parser is fixed s.t. it fully initialzes the modules it returns
listOf(Path.of(importingModule.position.file).parent ?: Path.of(".")) +
listOf(Path.of(".", "prog8lib"))
listOf(Path(importingModule.position.file).parent ?: Path("")) +
listOf(Path(".", "prog8lib"))
}
locations.forEach {
try {
return SourceCode.fromPath(it.resolve(fileName))
} catch (e: NoSuchFileException) {
}
}
}
//throw ParsingFailedError("$position Import: no module source file '$fileName' found (I've looked in: embedded libs and $locations)")