mirror of
https://github.com/irmen/prog8.git
synced 2024-09-07 19:54:26 +00:00
* *little bit* of cleanup in ModuleImporter - *only refactoring*
This commit is contained in:
parent
0d06e3ff22
commit
3b97a17648
@ -18,11 +18,10 @@ import prog8.compiler.target.asmGeneratorFor
|
|||||||
import prog8.optimizer.*
|
import prog8.optimizer.*
|
||||||
import prog8.parser.ModuleImporter
|
import prog8.parser.ModuleImporter
|
||||||
import prog8.parser.ParsingFailedError
|
import prog8.parser.ParsingFailedError
|
||||||
import prog8.parser.moduleName
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.*
|
||||||
import kotlin.system.measureTimeMillis
|
import kotlin.system.measureTimeMillis
|
||||||
|
|
||||||
|
|
||||||
@ -176,7 +175,7 @@ private fun parseImports(filepath: Path,
|
|||||||
libdirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
|
libdirs: List<String>): Triple<Program, CompilationOptions, List<Path>> {
|
||||||
println("Compiler target: ${compTarget.name}. Parsing...")
|
println("Compiler target: ${compTarget.name}. Parsing...")
|
||||||
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
|
val bf = BuiltinFunctionsFacade(BuiltinFunctions)
|
||||||
val programAst = Program(moduleName(filepath.fileName), mutableListOf(), bf, compTarget)
|
val programAst = Program(filepath.nameWithoutExtension, mutableListOf(), bf, compTarget)
|
||||||
bf.program = programAst
|
bf.program = programAst
|
||||||
|
|
||||||
val importer = ModuleImporter(programAst, compTarget.name, libdirs)
|
val importer = ModuleImporter(programAst, compTarget.name, libdirs)
|
||||||
|
@ -7,12 +7,9 @@ import prog8.ast.base.SyntaxError
|
|||||||
import prog8.ast.statements.Directive
|
import prog8.ast.statements.Directive
|
||||||
import prog8.ast.statements.DirectiveArg
|
import prog8.ast.statements.DirectiveArg
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.nio.file.Path
|
||||||
import kotlin.io.FileSystemException
|
import kotlin.io.FileSystemException
|
||||||
import java.nio.file.Path // TODO: use kotlin.io.paths.Path instead
|
import kotlin.io.path.*
|
||||||
import java.nio.file.Paths // TODO: use kotlin.io.paths.Path instead
|
|
||||||
|
|
||||||
|
|
||||||
fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
|
|
||||||
|
|
||||||
|
|
||||||
class ModuleImporter(private val program: Program,
|
class ModuleImporter(private val program: Program,
|
||||||
@ -20,10 +17,10 @@ class ModuleImporter(private val program: Program,
|
|||||||
private val libdirs: List<String>) {
|
private val libdirs: List<String>) {
|
||||||
|
|
||||||
fun importModule(filePath: Path): Module {
|
fun importModule(filePath: Path): Module {
|
||||||
print("importing '${moduleName(filePath.fileName)}'")
|
print("importing '${filePath.nameWithoutExtension}'")
|
||||||
if (filePath.parent != null) { // TODO: use Path.relativize
|
if (filePath.parent != null) { // TODO: use Path.relativize
|
||||||
var importloc = filePath.toString()
|
var importloc = filePath.toString()
|
||||||
val curdir = Paths.get("").toAbsolutePath().toString()
|
val curdir = Path("").absolutePathString()
|
||||||
if(importloc.startsWith(curdir))
|
if(importloc.startsWith(curdir))
|
||||||
importloc = "." + importloc.substring(curdir.length)
|
importloc = "." + importloc.substring(curdir.length)
|
||||||
println(" (from '$importloc')")
|
println(" (from '$importloc')")
|
||||||
@ -124,22 +121,22 @@ class ModuleImporter(private val program: Program,
|
|||||||
|
|
||||||
private fun tryGetModuleFromFile(name: String, importingModule: Module?): SourceCode? {
|
private fun tryGetModuleFromFile(name: String, importingModule: Module?): SourceCode? {
|
||||||
val fileName = "$name.p8"
|
val fileName = "$name.p8"
|
||||||
val libpaths = libdirs.map { Path.of(it) }
|
val libpaths = libdirs.map { Path(it) }
|
||||||
val locations =
|
val locations =
|
||||||
if (importingModule == null) { // <=> imported from library module
|
if (importingModule == null) { // <=> imported from library module
|
||||||
libpaths
|
libpaths
|
||||||
} else {
|
} else {
|
||||||
libpaths.drop(1) + // TODO: why drop the first?
|
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
|
// 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(importingModule.position.file).parent ?: Path("")) +
|
||||||
listOf(Path.of(".", "prog8lib"))
|
listOf(Path(".", "prog8lib"))
|
||||||
}
|
}
|
||||||
|
|
||||||
locations.forEach {
|
locations.forEach {
|
||||||
try {
|
try {
|
||||||
return SourceCode.fromPath(it.resolve(fileName))
|
return SourceCode.fromPath(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)")
|
//throw ParsingFailedError("$position Import: no module source file '$fileName' found (I've looked in: embedded libs and $locations)")
|
||||||
|
Loading…
Reference in New Issue
Block a user