diff --git a/codeCore/src/prog8/code/source/ImportFileSystem.kt b/codeCore/src/prog8/code/source/ImportFileSystem.kt index 5b7525103..27eee1561 100644 --- a/codeCore/src/prog8/code/source/ImportFileSystem.kt +++ b/codeCore/src/prog8/code/source/ImportFileSystem.kt @@ -4,17 +4,30 @@ import prog8.code.core.Position import java.nio.file.Path import java.util.TreeMap import kotlin.io.path.Path +import kotlin.io.path.absolute // Resource caching "filesystem". // Note that it leaves the decision to load a resource or an actual disk file to the caller. object ImportFileSystem { + + fun expandTilde(path: String): String = if (path.startsWith("~")) { + val userHome = System.getProperty("user.home") + userHome + path.drop(1) + } else { + path + } + + fun expandTilde(path: Path): Path = Path(expandTilde(path.toString())) + fun getFile(path: Path): SourceCode { - val cached = cache[path.toString()] - if (cached != null) return cached - val file = SourceCode.File(path) - cache[path.toString()] = file + val normalized = path.absolute().normalize() + val cached = cache[normalized.toString()] + if (cached != null) + return cached + val file = SourceCode.File(normalized) + cache[normalized.toString()] = file return file } @@ -35,7 +48,7 @@ object ImportFileSystem { val cached = cache[position.file] if(cached != null) return getLine(cached, position.line) - val path = Path(position.file).toAbsolutePath().normalize() + val path = Path(position.file).absolute().normalize() val cached2 = cache[path.toString()] if(cached2 != null) return getLine(cached2, position.line) diff --git a/codeCore/src/prog8/code/source/SourceCode.kt b/codeCore/src/prog8/code/source/SourceCode.kt index e7eb86028..eb635594a 100644 --- a/codeCore/src/prog8/code/source/SourceCode.kt +++ b/codeCore/src/prog8/code/source/SourceCode.kt @@ -4,6 +4,7 @@ import java.io.IOException import java.nio.file.Path import java.text.Normalizer import kotlin.io.path.Path +import kotlin.io.path.absolute import kotlin.io.path.readText /** @@ -52,8 +53,8 @@ sealed class SourceCode { */ private const val LIBRARYFILEPREFIX = "library:" private const val STRINGSOURCEPREFIX = "string:" - val curdir: Path = Path(".").toAbsolutePath() - fun relative(path: Path): Path = curdir.relativize(path.toAbsolutePath()) + val curdir: Path = Path(".").absolute() + fun relative(path: Path): Path = curdir.relativize(path.absolute()) fun isRegularFilesystemPath(pathString: String) = !isLibraryResource(pathString) && !isStringResource(pathString) fun isLibraryResource(path: String) = path.startsWith(LIBRARYFILEPREFIX) fun isStringResource(path: String) = path.startsWith(STRINGSOURCEPREFIX) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 14589b5e0..04ca49ebd 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -12,6 +12,7 @@ import prog8.code.source.SourceCode import prog8.code.target.Cx16Target import prog8.codegen.cpu6502.assignment.* import kotlin.io.path.Path +import kotlin.io.path.absolute import kotlin.io.path.writeLines @@ -1083,8 +1084,8 @@ $repeatLabel""") val sourcePath = Path(incbin.definingBlock()!!.source.origin) val includedPath = sourcePath.resolveSibling(incbin.file) val pathForAssembler = options.outputDir // #54: 64tass needs the path *relative to the .asm file* - .toAbsolutePath() - .relativize(includedPath.toAbsolutePath()) + .absolute() + .relativize(includedPath.absolute()) .normalize() // avoid assembler warnings (-Wportable; only some, not all) .toString().replace('\\', '/') out(" .binary \"$pathForAssembler\" $offset $length") diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 3fe1615f3..ebb571986 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -3,6 +3,7 @@ package prog8 import kotlinx.cli.* import prog8.ast.AstException import prog8.code.core.CbmPrgLauncherType +import prog8.code.source.ImportFileSystem import prog8.code.target.CompilationTargets import prog8.code.target.Cx16Target import prog8.code.target.getCompilationTargetByName @@ -95,8 +96,8 @@ private fun compileMain(args: Array): Boolean { return false } - val srcdirs = sourceDirs.toMutableList() - if(srcdirs.firstOrNull()!=".") + val srcdirs = sourceDirs.map { ImportFileSystem.expandTilde(it) }.toMutableList() + if(sourceDirs.firstOrNull()!=".") srcdirs.add(0, ".") if(startVm==null) { diff --git a/compiler/src/prog8/compiler/ModuleImporter.kt b/compiler/src/prog8/compiler/ModuleImporter.kt index 7aac1cb5f..2c0c395e6 100644 --- a/compiler/src/prog8/compiler/ModuleImporter.kt +++ b/compiler/src/prog8/compiler/ModuleImporter.kt @@ -135,7 +135,7 @@ class ModuleImporter(private val program: Program, if (importingModule == null) { // <=> imported from library module sourcePaths } else { - val pathFromImportingModule = (Path(importingModule.position.file).parent ?: Path("")).absolute() + val pathFromImportingModule = (Path(importingModule.position.file).parent ?: Path("")).absolute().normalize() listOf(pathFromImportingModule) + sourcePaths } diff --git a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt index 3750d4d27..c56e04bb5 100644 --- a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt @@ -15,6 +15,7 @@ import prog8.code.source.SourceCode import prog8.compiler.builtinFunctionReturnType import java.io.File import kotlin.io.path.Path +import kotlin.io.path.absolute import kotlin.io.path.isRegularFile @@ -257,10 +258,10 @@ class IntermediateAstMaker(private val program: Program, private val errors: IEr val offset: UInt? = if(directive.args.size>=2) directive.args[1].int!! else null val length: UInt? = if(directive.args.size>=3) directive.args[2].int!! else null val abspath = if(File(filename).isFile) { - Path(filename).toAbsolutePath() + Path(filename).absolute() } else { val sourcePath = Path(directive.definingModule.source.origin) - sourcePath.resolveSibling(filename).toAbsolutePath() + sourcePath.resolveSibling(filename).absolute() } if(abspath.toFile().isFile) PtIncludeBinary(abspath, offset, length, directive.position) diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index f5f0c83e3..7cc8fc24d 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -61,7 +61,7 @@ private fun prepareTestFiles(source: String, optimize: Boolean, target: ICompila } val filepath = searchIn.asSequence() .map { it.resolve("$source.p8") } - .map { it.normalize().absolute() } + .map { it.absolute().normalize() } .map { workingDir.relativize(it) } .first { it.exists() } val displayName = "${examplesDir.relativize(filepath.absolute())}: ${target.name}, optimize=$optimize" diff --git a/compiler/test/TestCompilerOnImportsAndIncludes.kt b/compiler/test/TestCompilerOnImportsAndIncludes.kt index 0e5e65931..33ee6861e 100644 --- a/compiler/test/TestCompilerOnImportsAndIncludes.kt +++ b/compiler/test/TestCompilerOnImportsAndIncludes.kt @@ -11,6 +11,7 @@ import prog8.ast.statements.FunctionCallStatement import prog8.ast.statements.Label import prog8.code.target.Cx16Target import prog8tests.helpers.* +import kotlin.io.path.absolute import kotlin.io.path.name @@ -97,7 +98,7 @@ class TestCompilerOnImportsAndIncludes: FunSpec({ // the bug we're testing for (#54) was hidden if outputDir == workingDir withClue("sanity check: workingDir and outputDir should not be the same folder") { - outputDir.normalize().toAbsolutePath() shouldNotBe workingDir.normalize().toAbsolutePath() + outputDir.absolute().normalize() shouldNotBe workingDir.absolute().normalize() } withClue("argument to assembler directive .binary " + diff --git a/compiler/test/ast/TestProg8Parser.kt b/compiler/test/ast/TestProg8Parser.kt index 6182a2e15..506925dea 100644 --- a/compiler/test/ast/TestProg8Parser.kt +++ b/compiler/test/ast/TestProg8Parser.kt @@ -103,7 +103,7 @@ class TestProg8Parser: FunSpec( { val nlUnix = "\n" val nlMac = "\r" - //parseModule(Paths.get("test", "fixtures", "mac_newlines.p8").toAbsolutePath()) + //parseModule(Paths.get("test", "fixtures", "mac_newlines.p8").absolute()) // a good mix of all kinds of newlines: val srcText = diff --git a/intermediate/src/prog8/intermediate/IRFileWriter.kt b/intermediate/src/prog8/intermediate/IRFileWriter.kt index d0199da9a..d7f1bca4f 100644 --- a/intermediate/src/prog8/intermediate/IRFileWriter.kt +++ b/intermediate/src/prog8/intermediate/IRFileWriter.kt @@ -7,6 +7,7 @@ import prog8.code.source.ImportFileSystem import java.nio.file.Path import javax.xml.stream.XMLOutputFactory import javax.xml.stream.XMLStreamWriter +import kotlin.io.path.absolute import kotlin.io.path.bufferedWriter import kotlin.io.path.div @@ -198,7 +199,7 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { } xml.writeCharacters("loadAddress=${irProgram.options.loadAddress.toHex()}\n") xml.writeCharacters("optimize=${irProgram.options.optimize}\n") - xml.writeCharacters("outputDir=${irProgram.options.outputDir.toAbsolutePath()}\n") + xml.writeCharacters("outputDir=${irProgram.options.outputDir.absolute()}\n") // other options not yet useful here? xml.writeEndElement() xml.writeCharacters("\n\n") diff --git a/virtualmachine/src/prog8/vm/SysCalls.kt b/virtualmachine/src/prog8/vm/SysCalls.kt index 970a8be59..9f141ad9d 100644 --- a/virtualmachine/src/prog8/vm/SysCalls.kt +++ b/virtualmachine/src/prog8/vm/SysCalls.kt @@ -538,7 +538,7 @@ object SysCalls { Syscall.DIRECTORY -> { // no arguments val directory = Path("") - println("Directory listing for ${directory.toAbsolutePath().normalize()}") + println("Directory listing for ${directory.absolute().normalize()}") directory.listDirectoryEntries().sorted().forEach { println("${it.toFile().length()}\t${it.normalize()}") } @@ -574,7 +574,7 @@ object SysCalls { } Syscall.CURDIR -> { - val curdir = Path("").toAbsolutePath().toString() + val curdir = Path("").absolute().toString() vm.memory.setString(0xfe00, curdir, true) return returnValue(callspec.returns.single(), 0xfe00, vm) }