refactor loadAsmIncludeFile response

This commit is contained in:
Irmen de Jong 2021-10-29 00:35:49 +02:00
parent 7d22b9b9f9
commit ce75b776bb
5 changed files with 24 additions and 64 deletions

View File

@ -24,6 +24,7 @@ import prog8.parser.SourceCode.Companion.libraryFilePrefix
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.Path import kotlin.io.path.Path
import kotlin.io.path.isRegularFile
import kotlin.io.path.nameWithoutExtension import kotlin.io.path.nameWithoutExtension
import kotlin.system.measureTimeMillis import kotlin.system.measureTimeMillis
@ -385,16 +386,13 @@ fun printAst(programAst: Program) {
internal fun loadAsmIncludeFile(filename: String, source: SourceCode): Result<String, NoSuchFileException> { internal fun loadAsmIncludeFile(filename: String, source: SourceCode): Result<String, NoSuchFileException> {
return if (filename.startsWith(libraryFilePrefix)) { return if (filename.startsWith(libraryFilePrefix)) {
return runCatching { return runCatching {
val stream = object {}.javaClass.getResourceAsStream("/prog8lib/${filename.substring(libraryFilePrefix.length)}") // TODO handle via SourceCode SourceCode.Resource("/prog8lib/${filename.substring(libraryFilePrefix.length)}").readText()
stream!!.bufferedReader().use { r -> r.readText() }
}.mapError { NoSuchFileException(File(filename)) } }.mapError { NoSuchFileException(File(filename)) }
} else { } else {
// first try in the isSameAs folder as where the containing file was imported from
val sib = Path(source.origin).resolveSibling(filename) val sib = Path(source.origin).resolveSibling(filename)
if (sib.isRegularFile())
if (sib.toFile().isFile) Ok(SourceCode.File(sib).readText())
Ok(sib.toFile().readText())
else else
Ok(File(filename).readText()) Ok(SourceCode.File(Path(filename)).readText())
} }
} }

View File

@ -19,41 +19,6 @@ import java.nio.file.Path
import java.time.LocalDate import java.time.LocalDate
import java.time.LocalDateTime import java.time.LocalDateTime
import java.util.* import java.util.*
import kotlin.collections.Iterable
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.collections.MutableSet
import kotlin.collections.any
import kotlin.collections.chunked
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.contains
import kotlin.collections.drop
import kotlin.collections.filter
import kotlin.collections.filterIsInstance
import kotlin.collections.first
import kotlin.collections.firstOrNull
import kotlin.collections.forEach
import kotlin.collections.getValue
import kotlin.collections.isNotEmpty
import kotlin.collections.iterator
import kotlin.collections.joinToString
import kotlin.collections.last
import kotlin.collections.listOf
import kotlin.collections.map
import kotlin.collections.mutableListOf
import kotlin.collections.mutableMapOf
import kotlin.collections.mutableSetOf
import kotlin.collections.partition
import kotlin.collections.plus
import kotlin.collections.removeLast
import kotlin.collections.set
import kotlin.collections.setOf
import kotlin.collections.single
import kotlin.collections.sortedBy
import kotlin.collections.toList
import kotlin.collections.toMutableList
import kotlin.collections.zip
import kotlin.io.path.Path import kotlin.io.path.Path
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
@ -1357,7 +1322,6 @@ $repeatLabel lda $counterVar
private fun translate(stmt: Directive) { private fun translate(stmt: Directive) {
when(stmt.directive) { when(stmt.directive) {
"%asminclude" -> { "%asminclude" -> {
// TODO: handle %asminclude with SourceCode
val includedName = stmt.args[0].str!! val includedName = stmt.args[0].str!!
if(stmt.definingModule.source is SourceCode.Generated) if(stmt.definingModule.source is SourceCode.Generated)
TODO("%asminclude inside non-library, non-filesystem module") TODO("%asminclude inside non-library, non-filesystem module")

View File

@ -8,9 +8,7 @@ import java.nio.channels.Channels
import java.nio.charset.CodingErrorAction import java.nio.charset.CodingErrorAction
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.exists import kotlin.io.path.*
import kotlin.io.path.isDirectory
import kotlin.io.path.isReadable
/** /**
* Encapsulates - and ties together - actual source code (=text) * Encapsulates - and ties together - actual source code (=text)
@ -45,9 +43,8 @@ sealed class SourceCode {
/** /**
* The source code as plain string. * The source code as plain string.
* *Note: this is meant for testing and debugging, do NOT use in application code!*
*/ */
fun asString() = this.getCharStream().toString() abstract fun readText(): String
/** /**
* Deliberately does NOT return the actual text. * Deliberately does NOT return the actual text.
@ -62,7 +59,7 @@ sealed class SourceCode {
*/ */
const val libraryFilePrefix = "library:" const val libraryFilePrefix = "library:"
const val stringSourcePrefix = "<String@" const val stringSourcePrefix = "<String@"
val curdir: Path = Path.of(".").toAbsolutePath() val curdir: Path = Path(".").toAbsolutePath()
fun relative(path: Path): Path = curdir.relativize(path.toAbsolutePath()) fun relative(path: Path): Path = curdir.relativize(path.toAbsolutePath())
fun isRegularFilesystemPath(pathString: String) = fun isRegularFilesystemPath(pathString: String) =
!(pathString.startsWith(libraryFilePrefix) || pathString.startsWith(stringSourcePrefix)) !(pathString.startsWith(libraryFilePrefix) || pathString.startsWith(stringSourcePrefix))
@ -77,6 +74,7 @@ sealed class SourceCode {
override val isFromFilesystem = false override val isFromFilesystem = false
override val origin = "$stringSourcePrefix${System.identityHashCode(text).toString(16)}>" override val origin = "$stringSourcePrefix${System.identityHashCode(text).toString(16)}>"
override fun getCharStream(): CharStream = CharStreams.fromString(text, origin) override fun getCharStream(): CharStream = CharStreams.fromString(text, origin)
override fun readText() = text
} }
/** /**
@ -107,6 +105,7 @@ sealed class SourceCode {
override val isFromFilesystem = true override val isFromFilesystem = true
override val origin = relative(normalized).toString() override val origin = relative(normalized).toString()
override fun getCharStream(): CharStream = CharStreams.fromPath(normalized) override fun getCharStream(): CharStream = CharStreams.fromPath(normalized)
override fun readText() = normalized.readText()
} }
/** /**
@ -129,12 +128,17 @@ sealed class SourceCode {
override val isFromResources = true override val isFromResources = true
override val isFromFilesystem = false override val isFromFilesystem = false
override val origin = "$libraryFilePrefix$normalized" override val origin = "$libraryFilePrefix$normalized"
override fun getCharStream(): CharStream { public override fun getCharStream(): CharStream {
val inpStr = object {}.javaClass.getResourceAsStream(normalized)!! val inpStr = object {}.javaClass.getResourceAsStream(normalized)!!
// CharStreams.fromStream() doesn't allow us to set the stream name properly, so we use a lower level api // CharStreams.fromStream() doesn't allow us to set the stream name properly, so we use a lower level api
val channel = Channels.newChannel(inpStr) val channel = Channels.newChannel(inpStr)
return CharStreams.fromChannel(channel, StandardCharsets.UTF_8, 4096, CodingErrorAction.REPLACE, origin, -1); return CharStreams.fromChannel(channel, StandardCharsets.UTF_8, 4096, CodingErrorAction.REPLACE, origin, -1);
} }
override fun readText(): String {
val stream = object {}.javaClass.getResourceAsStream(normalized)
return stream!!.bufferedReader().use { r -> r.readText() }
}
} }
/** /**
@ -145,6 +149,7 @@ sealed class SourceCode {
override val isFromResources: Boolean = false override val isFromResources: Boolean = false
override val isFromFilesystem: Boolean = false override val isFromFilesystem: Boolean = false
override val origin: String = name override val origin: String = name
override fun readText() = throw IOException("generated code nodes don't have a text representation")
} }
// TODO: possibly more, like fromURL(..) // TODO: possibly more, like fromURL(..)

View File

@ -58,7 +58,7 @@ class TestSourceCode {
val src = SourceCode.File(path) val src = SourceCode.File(path)
val expectedOrigin = SourceCode.relative(path).toString() val expectedOrigin = SourceCode.relative(path).toString()
assertEquals(expectedOrigin, src.origin) assertEquals(expectedOrigin, src.origin)
assertEquals(path.toFile().readText(), src.asString()) assertEquals(path.toFile().readText(), src.readText())
assertFalse(src.isFromResources) assertFalse(src.isFromResources)
assertTrue(src.isFromFilesystem) assertTrue(src.isFromFilesystem)
} }
@ -71,7 +71,7 @@ class TestSourceCode {
val src = SourceCode.File(path) val src = SourceCode.File(path)
val expectedOrigin = SourceCode.relative(path).toString() val expectedOrigin = SourceCode.relative(path).toString()
assertEquals(expectedOrigin, src.origin) assertEquals(expectedOrigin, src.origin)
assertEquals(srcFile.readText(), src.asString()) assertEquals(srcFile.readText(), src.readText())
} }
@Test @Test
@ -81,7 +81,7 @@ class TestSourceCode {
val src = SourceCode.Resource(pathString) val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix/$pathString", src.origin) assertEquals("$libraryFilePrefix/$pathString", src.origin)
assertEquals(srcFile.readText(), src.asString()) assertEquals(srcFile.readText(), src.readText())
assertTrue(src.isFromResources) assertTrue(src.isFromResources)
assertFalse(src.isFromFilesystem) assertFalse(src.isFromFilesystem)
} }
@ -93,7 +93,7 @@ class TestSourceCode {
val src = SourceCode.Resource(pathString) val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix$pathString", src.origin) assertEquals("$libraryFilePrefix$pathString", src.origin)
assertEquals(srcFile.readText(), src.asString()) assertEquals(srcFile.readText(), src.readText())
} }
@Test @Test
@ -103,7 +103,7 @@ class TestSourceCode {
val src = SourceCode.Resource(pathString) val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix/$pathString", src.origin) assertEquals("$libraryFilePrefix/$pathString", src.origin)
assertEquals(srcFile.readText(), src.asString()) assertEquals(srcFile.readText(), src.readText())
assertTrue(src.isFromResources, ".isFromResources") assertTrue(src.isFromResources, ".isFromResources")
} }
@ -114,7 +114,7 @@ class TestSourceCode {
val src = SourceCode.Resource(pathString) val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix$pathString", src.origin) assertEquals("$libraryFilePrefix$pathString", src.origin)
assertEquals(srcFile.readText(), src.asString()) assertEquals(srcFile.readText(), src.readText())
} }
@Test @Test
@ -124,7 +124,7 @@ class TestSourceCode {
val src = SourceCode.Resource(pathString) val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix/prog8lib/math.p8", src.origin) assertEquals("$libraryFilePrefix/prog8lib/math.p8", src.origin)
assertEquals(srcFile.readText(), src.asString()) assertEquals(srcFile.readText(), src.readText())
assertTrue(src.isFromResources, ".isFromResources") assertTrue(src.isFromResources, ".isFromResources")
} }

View File

@ -5,12 +5,5 @@ main {
} }
sub fubar(ubyte aa, ubyte bb, ubyte cc, ubyte dd) { sub fubar(ubyte aa, ubyte bb, ubyte cc, ubyte dd) {
ubyte aa
bb:
sub cc() {
dd++
}
} }
} }