From ce75b776bb82e4b8ea36562f5b240f08ae4a5b5a Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 29 Oct 2021 00:35:49 +0200 Subject: [PATCH] refactor loadAsmIncludeFile response --- compiler/src/prog8/compiler/Compiler.kt | 12 +++---- .../compiler/target/cpu6502/codegen/AsmGen.kt | 36 ------------------- compilerAst/src/prog8/parser/SourceCode.kt | 19 ++++++---- compilerAst/test/TestSourceCode.kt | 14 ++++---- examples/test.p8 | 7 ---- 5 files changed, 24 insertions(+), 64 deletions(-) diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 525434be6..41a5f602f 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -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 { 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()) } } diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 763805dec..d973919b0 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -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") diff --git a/compilerAst/src/prog8/parser/SourceCode.kt b/compilerAst/src/prog8/parser/SourceCode.kt index c41c676f2..40dc5afdf 100644 --- a/compilerAst/src/prog8/parser/SourceCode.kt +++ b/compilerAst/src/prog8/parser/SourceCode.kt @@ -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 = " 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(..) diff --git a/compilerAst/test/TestSourceCode.kt b/compilerAst/test/TestSourceCode.kt index aef08e459..dec7ecd7b 100644 --- a/compilerAst/test/TestSourceCode.kt +++ b/compilerAst/test/TestSourceCode.kt @@ -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") } diff --git a/examples/test.p8 b/examples/test.p8 index 48ff76b5d..4c5a23162 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,12 +5,5 @@ main { } sub fubar(ubyte aa, ubyte bb, ubyte cc, ubyte dd) { - ubyte aa - -bb: - - sub cc() { - dd++ - } } }