mirror of
https://github.com/irmen/prog8.git
synced 2025-01-12 04:30:03 +00:00
simplify
This commit is contained in:
parent
30aa72dc8e
commit
e5a1b37981
@ -17,8 +17,8 @@ import prog8.compiler.target.ICompilationTarget
|
||||
import prog8.compiler.target.asmGeneratorFor
|
||||
import prog8.optimizer.*
|
||||
import prog8.parser.ParsingFailedError
|
||||
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.nameWithoutExtension
|
||||
@ -352,38 +352,19 @@ fun printAst(programAst: Program) {
|
||||
}
|
||||
|
||||
internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): Result<String> {
|
||||
if (filename.startsWith("library:")) {
|
||||
val resource = getEmbeddedResource(filename.substring(8))
|
||||
resource.fold(
|
||||
onSuccess = { resource ->
|
||||
val text = resource.bufferedReader().use { it.readText() }
|
||||
return Result.success(text)
|
||||
},
|
||||
onFailure = {
|
||||
return Result.failure(IllegalArgumentException("library file '$filename' not found")) // TODO FileNotFoundException instead?
|
||||
}
|
||||
)
|
||||
return if (filename.startsWith(libraryFilePrefix)) {
|
||||
runCatching {
|
||||
val stream = object {}.javaClass.getResourceAsStream("/prog8lib/${filename.substring(libraryFilePrefix.length)}") // TODO handle via SourceCode
|
||||
stream ?: throw NoSuchFileException(File(filename))
|
||||
}.mapCatching {
|
||||
it.bufferedReader().use { r -> r.readText() }
|
||||
}
|
||||
} else {
|
||||
// first try in the isSameAs folder as where the containing file was imported from
|
||||
val sib = sourcePath.resolveSibling(filename)
|
||||
return if (sib.toFile().isFile)
|
||||
if (sib.toFile().isFile)
|
||||
Result.success(sib.toFile().readText())
|
||||
else
|
||||
Result.success(File(filename).readText())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle via SourceCode
|
||||
*/
|
||||
internal fun getEmbeddedResource(name: String): Result<InputStream> {
|
||||
return try {
|
||||
val stream = object {}.javaClass.getResourceAsStream("/prog8lib/$name")
|
||||
if(stream!=null)
|
||||
Result.success(stream)
|
||||
else
|
||||
Result.failure(NoSuchFileException(File(name)))
|
||||
} catch(ex: Exception) {
|
||||
Result.failure(ex)
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import prog8.compiler.IErrorReporter
|
||||
|
||||
class ErrorReporterForTests: IErrorReporter {
|
||||
|
||||
|
||||
val errors = mutableListOf<String>()
|
||||
val warnings = mutableListOf<String>()
|
||||
|
||||
|
@ -61,6 +61,11 @@ abstract class SourceCode {
|
||||
// "static" factory methods
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* filename prefix to designate library files that will be retreived from internal resources rather than disk
|
||||
*/
|
||||
const val libraryFilePrefix = "library:"
|
||||
|
||||
/**
|
||||
* Turn a plain String into a [SourceCode] object.
|
||||
* [origin] will be something like `<String@44c56085>`.
|
||||
@ -121,7 +126,7 @@ abstract class SourceCode {
|
||||
}
|
||||
return object : SourceCode() {
|
||||
override val isFromResources = true
|
||||
override val origin = "library:$normalized"
|
||||
override val origin = "$libraryFilePrefix$normalized"
|
||||
override fun getCharStream(): CharStream {
|
||||
val inpStr = object {}.javaClass.getResourceAsStream(normalized)
|
||||
return CharStreams.fromStream(inpStr)
|
||||
|
@ -4,6 +4,7 @@ import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
import prog8.parser.SourceCode
|
||||
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
||||
import prog8tests.helpers.*
|
||||
import kotlin.io.path.Path
|
||||
import kotlin.io.path.absolutePathString
|
||||
@ -76,7 +77,7 @@ class TestSourceCode {
|
||||
val srcFile = assumeReadableFile(resourcesDir, pathString).toFile()
|
||||
val src = SourceCode.fromResources(pathString)
|
||||
|
||||
assertEquals("library:/$pathString", src.origin)
|
||||
assertEquals("$libraryFilePrefix/$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
}
|
||||
|
||||
@ -86,7 +87,7 @@ class TestSourceCode {
|
||||
val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile()
|
||||
val src = SourceCode.fromResources(pathString)
|
||||
|
||||
assertEquals("library:$pathString", src.origin)
|
||||
assertEquals("$libraryFilePrefix$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
}
|
||||
|
||||
@ -96,7 +97,7 @@ class TestSourceCode {
|
||||
val srcFile = assumeReadableFile(resourcesDir, pathString).toFile()
|
||||
val src = SourceCode.fromResources(pathString)
|
||||
|
||||
assertEquals("library:/$pathString", src.origin)
|
||||
assertEquals("$libraryFilePrefix/$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertTrue(src.isFromResources, ".isFromResources")
|
||||
}
|
||||
@ -107,7 +108,7 @@ class TestSourceCode {
|
||||
val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile()
|
||||
val src = SourceCode.fromResources(pathString)
|
||||
|
||||
assertEquals("library:$pathString", src.origin)
|
||||
assertEquals("$libraryFilePrefix$pathString", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
}
|
||||
|
||||
@ -117,7 +118,7 @@ class TestSourceCode {
|
||||
val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile()
|
||||
val src = SourceCode.fromResources(pathString)
|
||||
|
||||
assertEquals("library:/prog8lib/math.p8", src.origin)
|
||||
assertEquals("$libraryFilePrefix/prog8lib/math.p8", src.origin)
|
||||
assertEquals(srcFile.readText(), src.asString())
|
||||
assertTrue(src.isFromResources, ".isFromResources")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user