mirror of
https://github.com/irmen/prog8.git
synced 2024-11-29 17:50:35 +00:00
refactor loadAsmIncludeFile response
This commit is contained in:
parent
7d22b9b9f9
commit
ce75b776bb
@ -24,6 +24,7 @@ import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.isRegularFile
|
||||
import kotlin.io.path.nameWithoutExtension
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
@ -385,16 +386,13 @@ fun printAst(programAst: Program) {
|
||||
internal fun loadAsmIncludeFile(filename: String, source: SourceCode): Result<String, NoSuchFileException> {
|
||||
return if (filename.startsWith(libraryFilePrefix)) {
|
||||
return runCatching {
|
||||
val stream = object {}.javaClass.getResourceAsStream("/prog8lib/${filename.substring(libraryFilePrefix.length)}") // TODO handle via SourceCode
|
||||
stream!!.bufferedReader().use { r -> r.readText() }
|
||||
SourceCode.Resource("/prog8lib/${filename.substring(libraryFilePrefix.length)}").readText()
|
||||
}.mapError { NoSuchFileException(File(filename)) }
|
||||
} else {
|
||||
// first try in the isSameAs folder as where the containing file was imported from
|
||||
val sib = Path(source.origin).resolveSibling(filename)
|
||||
|
||||
if (sib.toFile().isFile)
|
||||
Ok(sib.toFile().readText())
|
||||
if (sib.isRegularFile())
|
||||
Ok(SourceCode.File(sib).readText())
|
||||
else
|
||||
Ok(File(filename).readText())
|
||||
Ok(SourceCode.File(Path(filename)).readText())
|
||||
}
|
||||
}
|
||||
|
@ -19,41 +19,6 @@ import java.nio.file.Path
|
||||
import java.time.LocalDate
|
||||
import java.time.LocalDateTime
|
||||
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.math.absoluteValue
|
||||
|
||||
@ -1357,7 +1322,6 @@ $repeatLabel lda $counterVar
|
||||
private fun translate(stmt: Directive) {
|
||||
when(stmt.directive) {
|
||||
"%asminclude" -> {
|
||||
// TODO: handle %asminclude with SourceCode
|
||||
val includedName = stmt.args[0].str!!
|
||||
if(stmt.definingModule.source is SourceCode.Generated)
|
||||
TODO("%asminclude inside non-library, non-filesystem module")
|
||||
|
@ -8,9 +8,7 @@ import java.nio.channels.Channels
|
||||
import java.nio.charset.CodingErrorAction
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.isDirectory
|
||||
import kotlin.io.path.isReadable
|
||||
import kotlin.io.path.*
|
||||
|
||||
/**
|
||||
* Encapsulates - and ties together - actual source code (=text)
|
||||
@ -45,9 +43,8 @@ sealed class SourceCode {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@ -62,7 +59,7 @@ sealed class SourceCode {
|
||||
*/
|
||||
const val libraryFilePrefix = "library:"
|
||||
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 isRegularFilesystemPath(pathString: String) =
|
||||
!(pathString.startsWith(libraryFilePrefix) || pathString.startsWith(stringSourcePrefix))
|
||||
@ -77,6 +74,7 @@ sealed class SourceCode {
|
||||
override val isFromFilesystem = false
|
||||
override val origin = "$stringSourcePrefix${System.identityHashCode(text).toString(16)}>"
|
||||
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 origin = relative(normalized).toString()
|
||||
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 isFromFilesystem = false
|
||||
override val origin = "$libraryFilePrefix$normalized"
|
||||
override fun getCharStream(): CharStream {
|
||||
public override fun getCharStream(): CharStream {
|
||||
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
|
||||
val channel = Channels.newChannel(inpStr)
|
||||
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 isFromFilesystem: Boolean = false
|
||||
override val origin: String = name
|
||||
override fun readText() = throw IOException("generated code nodes don't have a text representation")
|
||||
}
|
||||
|
||||
// TODO: possibly more, like fromURL(..)
|
||||
|
@ -58,7 +58,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.File(path)
|
||||
val expectedOrigin = SourceCode.relative(path).toString()
|
||||
assertEquals(expectedOrigin, src.origin)
|
||||
assertEquals(path.toFile().readText(), src.asString())
|
||||
assertEquals(path.toFile().readText(), src.readText())
|
||||
assertFalse(src.isFromResources)
|
||||
assertTrue(src.isFromFilesystem)
|
||||
}
|
||||
@ -71,7 +71,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.File(path)
|
||||
val expectedOrigin = SourceCode.relative(path).toString()
|
||||
assertEquals(expectedOrigin, src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertEquals(srcFile.readText(), src.readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -81,7 +81,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.Resource(pathString)
|
||||
|
||||
assertEquals("$libraryFilePrefix/$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertEquals(srcFile.readText(), src.readText())
|
||||
assertTrue(src.isFromResources)
|
||||
assertFalse(src.isFromFilesystem)
|
||||
}
|
||||
@ -93,7 +93,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.Resource(pathString)
|
||||
|
||||
assertEquals("$libraryFilePrefix$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertEquals(srcFile.readText(), src.readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -103,7 +103,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.Resource(pathString)
|
||||
|
||||
assertEquals("$libraryFilePrefix/$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertEquals(srcFile.readText(), src.readText())
|
||||
assertTrue(src.isFromResources, ".isFromResources")
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.Resource(pathString)
|
||||
|
||||
assertEquals("$libraryFilePrefix$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertEquals(srcFile.readText(), src.readText())
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -124,7 +124,7 @@ class TestSourceCode {
|
||||
val src = SourceCode.Resource(pathString)
|
||||
|
||||
assertEquals("$libraryFilePrefix/prog8lib/math.p8", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertEquals(srcFile.readText(), src.readText())
|
||||
assertTrue(src.isFromResources, ".isFromResources")
|
||||
}
|
||||
|
||||
|
@ -5,12 +5,5 @@ main {
|
||||
}
|
||||
|
||||
sub fubar(ubyte aa, ubyte bb, ubyte cc, ubyte dd) {
|
||||
ubyte aa
|
||||
|
||||
bb:
|
||||
|
||||
sub cc() {
|
||||
dd++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user