simplify SourceCode: just read the full text immediately. Also optimized imports.

This commit is contained in:
Irmen de Jong 2022-02-05 03:50:54 +01:00
parent 08e052380a
commit 30e1c3307c
36 changed files with 113 additions and 145 deletions

View File

@ -94,13 +94,13 @@ class AssemblyProgram(
internal fun loadAsmIncludeFile(filename: String, source: SourceCode): Result<String, NoSuchFileException> {
return if (filename.startsWith(SourceCode.libraryFilePrefix)) {
return com.github.michaelbull.result.runCatching {
SourceCode.Resource("/prog8lib/${filename.substring(SourceCode.libraryFilePrefix.length)}").readText()
SourceCode.Resource("/prog8lib/${filename.substring(SourceCode.libraryFilePrefix.length)}").text
}.mapError { NoSuchFileException(File(filename)) }
} else {
val sib = Path(source.origin).resolveSibling(filename)
if (sib.isRegularFile())
Ok(SourceCode.File(sib).readText())
Ok(SourceCode.File(sib).text)
else
Ok(SourceCode.File(Path(filename)).readText())
Ok(SourceCode.File(Path(filename)).text)
}
}

View File

@ -11,11 +11,6 @@ import prog8.ast.statements.FunctionCallStatement
import prog8.ast.statements.Subroutine
import prog8.ast.toHex
import prog8.codegen.cpu6502.assignment.*
import prog8.codegen.cpu6502.assignment.AsmAssignSource
import prog8.codegen.cpu6502.assignment.AsmAssignTarget
import prog8.codegen.cpu6502.assignment.AsmAssignment
import prog8.codegen.cpu6502.assignment.AssignmentAsmGen
import prog8.codegen.cpu6502.assignment.SourceStorageKind
import prog8.compilerinterface.AssemblyError
import prog8.compilerinterface.BuiltinFunctions
import prog8.compilerinterface.CpuType

View File

@ -7,9 +7,9 @@ import prog8.ast.base.WordDatatypes
import prog8.ast.expressions.Expression
import prog8.ast.statements.RegisterOrStatusflag
import prog8.ast.statements.Subroutine
import prog8.codegen.target.c128.C128MachineDefinition
import prog8.codegen.target.cbm.asmsub6502ArgsEvalOrder
import prog8.codegen.target.cbm.asmsub6502ArgsHaveRegisterClobberRisk
import prog8.codegen.target.c128.C128MachineDefinition
import prog8.compilerinterface.ICompilationTarget
import prog8.compilerinterface.IStringEncoding

View File

@ -7,9 +7,9 @@ import prog8.ast.base.WordDatatypes
import prog8.ast.expressions.Expression
import prog8.ast.statements.RegisterOrStatusflag
import prog8.ast.statements.Subroutine
import prog8.codegen.target.c64.C64MachineDefinition
import prog8.codegen.target.cbm.asmsub6502ArgsEvalOrder
import prog8.codegen.target.cbm.asmsub6502ArgsHaveRegisterClobberRisk
import prog8.codegen.target.c64.C64MachineDefinition
import prog8.compilerinterface.ICompilationTarget
import prog8.compilerinterface.IStringEncoding

View File

@ -2,7 +2,9 @@ package prog8.codegen.target.cbm
import prog8.ast.base.Cx16VirtualRegisters
import prog8.ast.base.RegisterOrPair
import prog8.ast.expressions.*
import prog8.ast.expressions.ArrayIndexedExpression
import prog8.ast.expressions.Expression
import prog8.ast.expressions.FunctionCallExpression
import prog8.ast.statements.RegisterOrStatusflag
import prog8.ast.statements.Subroutine

View File

@ -1,8 +1,8 @@
package prog8.codegen.target.cbm
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import java.io.CharConversionException
import java.nio.charset.Charset

View File

@ -5,10 +5,10 @@ import prog8.ast.Node
import prog8.ast.Program
import prog8.ast.base.DataType
import prog8.ast.base.FatalAstException
import prog8.ast.expressions.AugmentAssignmentOperators
import prog8.ast.expressions.BinaryExpression
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.TypecastExpression
import prog8.ast.expressions.AugmentAssignmentOperators
import prog8.ast.statements.AssignTarget
import prog8.ast.statements.Assignment
import prog8.ast.statements.AssignmentOrigin

View File

@ -1,7 +1,10 @@
package prog8.optimizer
import prog8.ast.*
import prog8.ast.base.*
import prog8.ast.base.ArrayDatatypes
import prog8.ast.base.DataType
import prog8.ast.base.IntegerDatatypes
import prog8.ast.base.VarDeclType
import prog8.ast.expressions.*
import prog8.ast.statements.*
import prog8.ast.walk.AstWalker

View File

@ -3,11 +3,11 @@ package prog8
import kotlinx.cli.*
import prog8.ast.base.AstException
import prog8.codegen.target.C128Target
import prog8.codegen.target.C64Target
import prog8.codegen.target.Cx16Target
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram
import prog8.codegen.target.C64Target
import prog8.codegen.target.Cx16Target
import java.io.File
import java.nio.file.FileSystems
import java.nio.file.Path

View File

@ -1,6 +1,6 @@
package prog8.compiler
import com.github.michaelbull.result.*
import com.github.michaelbull.result.onFailure
import prog8.ast.AstToSourceTextConverter
import prog8.ast.IBuiltinFunctions
import prog8.ast.Program
@ -11,9 +11,9 @@ import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.statements.Directive
import prog8.codegen.cpu6502.AsmGen
import prog8.codegen.target.C128Target
import prog8.compiler.astprocessing.*
import prog8.codegen.target.C64Target
import prog8.codegen.target.Cx16Target
import prog8.compiler.astprocessing.*
import prog8.compilerinterface.*
import prog8.optimizer.*
import prog8.parser.ParseError

View File

@ -2,7 +2,9 @@ package prog8.compiler.astprocessing
import prog8.ast.Node
import prog8.ast.Program
import prog8.ast.base.*
import prog8.ast.base.NumericDatatypes
import prog8.ast.base.SyntaxError
import prog8.ast.base.VarDeclType
import prog8.ast.expressions.*
import prog8.ast.statements.*
import prog8.ast.walk.AstWalker

View File

@ -7,7 +7,10 @@ import prog8.ast.expressions.ArrayIndexedExpression
import prog8.ast.expressions.BinaryExpression
import prog8.ast.expressions.DirectMemoryRead
import prog8.ast.expressions.StringLiteralValue
import prog8.ast.statements.*
import prog8.ast.statements.AssignTarget
import prog8.ast.statements.DirectMemoryWrite
import prog8.ast.statements.Subroutine
import prog8.ast.statements.VarDecl
import prog8.ast.walk.AstWalker
import prog8.ast.walk.IAstModification

View File

@ -6,11 +6,14 @@ import prog8.ast.Node
import prog8.ast.Program
import prog8.ast.base.ParentSentinel
import prog8.ast.base.Position
import prog8.ast.expressions.*
import prog8.ast.expressions.DirectMemoryRead
import prog8.ast.expressions.FunctionCallExpression
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.PrefixExpression
import prog8.ast.statements.*
import prog8.ast.walk.AstWalker
import prog8.ast.walk.IAstModification
import prog8.compilerinterface.*
import prog8.compilerinterface.IErrorReporter
internal class CodeDesugarer(val program: Program, private val errors: IErrorReporter) : AstWalker() {

View File

@ -8,7 +8,9 @@ import prog8.ast.base.ArrayDatatypes
import prog8.ast.base.DataType
import prog8.ast.base.FatalAstException
import prog8.ast.expressions.*
import prog8.ast.statements.*
import prog8.ast.statements.AnonymousScope
import prog8.ast.statements.Assignment
import prog8.ast.statements.FunctionCallStatement
import prog8.ast.walk.AstWalker
import prog8.ast.walk.IAstModification
import prog8.compilerinterface.CompilationOptions

View File

@ -2,13 +2,6 @@ package prog8tests
import com.github.michaelbull.result.getErrorOrElse
import com.github.michaelbull.result.getOrElse
import prog8.ast.Program
import prog8.ast.internedStringsModuleName
import prog8.compiler.ModuleImporter
import prog8.compilerinterface.IErrorReporter
import prog8.parser.ParseError
import prog8.parser.SourceCode
import kotlin.io.path.*
import io.kotest.assertions.fail
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
@ -16,11 +9,14 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldBeIn
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import prog8.ast.Program
import prog8.ast.internedStringsModuleName
import prog8.compiler.ModuleImporter
import prog8.compilerinterface.IErrorReporter
import prog8.parser.ParseError
import prog8.parser.SourceCode
import prog8tests.helpers.*
import prog8tests.helpers.DummyFunctions
import prog8tests.helpers.DummyMemsizer
import prog8tests.helpers.DummyStringEncoder
import prog8tests.helpers.ErrorReporterForTests
import kotlin.io.path.*
class TestModuleImporter: FunSpec({
@ -71,7 +67,7 @@ class TestModuleImporter: FunSpec({
val searchIn = Path(".", "$srcPathRel").invariantSeparatorsPathString
val importer = makeImporter(null, searchIn)
shouldThrow<AccessDeniedException> { importer.importModule(srcPathRel) }
shouldThrow<FileSystemException> { importer.importModule(srcPathRel) }
.let {
withClue(".file should be normalized") {
"${it.file}" shouldBe "${it.file.normalize()}"
@ -82,7 +78,7 @@ class TestModuleImporter: FunSpec({
}
program.modules.size shouldBe 1
shouldThrow<AccessDeniedException> { importer.importModule(srcPathAbs) }
shouldThrow<FileSystemException> { importer.importModule(srcPathAbs) }
.let {
withClue(".file should be normalized") {
"${it.file}" shouldBe "${it.file.normalize()}"

View File

@ -1,14 +1,6 @@
package prog8tests
import io.kotest.core.config.AbstractProjectConfig
import io.kotest.core.listeners.Listener
import io.kotest.core.listeners.TestListener
import io.kotest.core.spec.Spec
import io.kotest.extensions.system.NoSystemErrListener
import io.kotest.extensions.system.NoSystemOutListener
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import kotlin.math.max
object ProjectConfig : AbstractProjectConfig() {
override val parallelism = 2 // max(2, Runtime.getRuntime().availableProcessors() / 2)

View File

@ -1,14 +1,13 @@
package prog8tests
import io.kotest.core.spec.style.FunSpec
import prog8.codegen.target.C64Target
import prog8.codegen.target.Cx16Target
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram
import prog8.codegen.target.C64Target
import prog8.codegen.target.Cx16Target
import prog8.compilerinterface.ICompilationTarget
import prog8tests.helpers.*
import prog8tests.helpers.assertSuccess
import java.nio.file.Path
import kotlin.io.path.absolute
import kotlin.io.path.exists

View File

@ -7,17 +7,16 @@ import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.types.instanceOf
import prog8.ast.base.DataType
import prog8.ast.base.Position
import prog8.ast.expressions.*
import prog8.ast.expressions.ArrayLiteralValue
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.expressions.RangeExpression
import prog8.ast.statements.ForLoop
import prog8.ast.statements.VarDecl
import prog8.codegen.target.C64Target
import prog8.codegen.target.Cx16Target
import prog8.compilerinterface.Encoding
import prog8tests.helpers.*
import prog8tests.helpers.ErrorReporterForTests
import prog8tests.helpers.assertFailure
import prog8tests.helpers.assertSuccess
import prog8tests.helpers.compileText
/**

View File

@ -1,12 +1,11 @@
package prog8tests
import io.kotest.core.spec.style.FunSpec
import prog8.codegen.target.Cx16Target
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram
import prog8.codegen.target.Cx16Target
import prog8tests.helpers.*
import prog8tests.helpers.assertSuccess
import java.nio.file.Path
import kotlin.io.path.absolute
import kotlin.io.path.createTempFile

View File

@ -5,9 +5,9 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldStartWith
import prog8.ast.internedStringsModuleName
import prog8.codegen.target.C64Target
import prog8.compiler.determineCompilationOptions
import prog8.compiler.parseImports
import prog8.codegen.target.C64Target
import prog8.compilerinterface.ZeropageType
import prog8tests.helpers.ErrorReporterForTests
import prog8tests.helpers.assertSuccess

View File

@ -12,15 +12,11 @@ import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.expressions.PrefixExpression
import prog8.ast.statements.*
import prog8.compiler.printProgram
import prog8.codegen.target.C64Target
import prog8.compiler.printProgram
import prog8.compilerinterface.isIOAddress
import prog8.parser.SourceCode
import prog8tests.helpers.*
import prog8tests.helpers.DummyFunctions
import prog8tests.helpers.DummyMemsizer
import prog8tests.helpers.DummyStringEncoder
import prog8tests.helpers.compileText
class TestMemory: FunSpec({

View File

@ -7,7 +7,10 @@ import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.types.instanceOf
import prog8.ast.base.DataType
import prog8.ast.expressions.*
import prog8.ast.expressions.BinaryExpression
import prog8.ast.expressions.DirectMemoryRead
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.statements.*
import prog8.codegen.target.C64Target
import prog8tests.helpers.ErrorReporterForTests

View File

@ -20,7 +20,6 @@ import prog8.codegen.target.cx16.CX16Zeropage
import prog8.compilerinterface.*
import prog8tests.helpers.DummyCompilationTarget
import prog8tests.helpers.ErrorReporterForTests
import java.lang.IllegalArgumentException
class TestAbstractZeropage: FunSpec({

View File

@ -26,7 +26,6 @@ import prog8.parser.ParseError
import prog8.parser.Prog8Parser.parseModule
import prog8.parser.SourceCode
import prog8tests.helpers.*
import prog8tests.helpers.DummyFunctions
import kotlin.io.path.Path
import kotlin.io.path.isRegularFile
import kotlin.io.path.name

View File

@ -21,10 +21,9 @@ class TestSourceCode: AnnotationSpec() {
main { }
"""
val src = SourceCode.Text(text)
val actualText = src.getCharStream().toString()
src.origin shouldContain Regex("^<String@[0-9a-f\\-]+>$")
actualText shouldBe text
src.text shouldBe text
src.isFromResources shouldBe false
src.isFromFilesystem shouldBe false
src.toString().startsWith("prog8.parser.SourceCode") shouldBe true
@ -46,7 +45,7 @@ class TestSourceCode: AnnotationSpec() {
@Test
fun testFromPathWithDirectory() {
shouldThrow<AccessDeniedException> { SourceCode.File(fixturesDir) }
shouldThrow<FileSystemException> { SourceCode.File(fixturesDir) }
}
@Test
@ -56,7 +55,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.File(path)
val expectedOrigin = SourceCode.relative(path).toString()
src.origin shouldBe expectedOrigin
src.readText() shouldBe path.toFile().readText()
src.text shouldBe path.toFile().readText()
src.isFromResources shouldBe false
src.isFromFilesystem shouldBe true
}
@ -69,7 +68,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.File(path)
val expectedOrigin = SourceCode.relative(path).toString()
src.origin shouldBe expectedOrigin
src.readText() shouldBe srcFile.readText()
src.text shouldBe srcFile.readText()
}
@Test
@ -79,7 +78,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.Resource(pathString)
src.origin shouldBe "$libraryFilePrefix/$pathString"
src.readText() shouldBe srcFile.readText()
src.text shouldBe srcFile.readText()
src.isFromResources shouldBe true
src.isFromFilesystem shouldBe false
}
@ -91,7 +90,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.Resource(pathString)
src.origin shouldBe "$libraryFilePrefix$pathString"
src.readText() shouldBe srcFile.readText()
src.text shouldBe srcFile.readText()
}
@Test
@ -101,7 +100,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.Resource(pathString)
src.origin shouldBe "$libraryFilePrefix/$pathString"
src.readText() shouldBe srcFile.readText()
src.text shouldBe srcFile.readText()
src.isFromResources shouldBe true
}
@ -112,7 +111,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.Resource(pathString)
src.origin shouldBe "$libraryFilePrefix$pathString"
src.readText() shouldBe srcFile.readText()
src.text shouldBe srcFile.readText()
}
@Test
@ -122,7 +121,7 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.Resource(pathString)
src.origin shouldBe "$libraryFilePrefix/prog8lib/math.p8"
src.readText() shouldBe srcFile.readText()
src.text shouldBe srcFile.readText()
src.isFromResources shouldBe true
}

View File

@ -4,7 +4,10 @@ import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import prog8.ast.Module
import prog8.ast.Program
import prog8.ast.base.*
import prog8.ast.base.DataType
import prog8.ast.base.Position
import prog8.ast.base.RegisterOrPair
import prog8.ast.base.VarDeclType
import prog8.ast.expressions.AddressOf
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue
@ -12,7 +15,10 @@ import prog8.ast.statements.*
import prog8.codegen.cpu6502.AsmGen
import prog8.codegen.target.C64Target
import prog8.codegen.target.c64.C64Zeropage
import prog8.compilerinterface.*
import prog8.compilerinterface.CompilationOptions
import prog8.compilerinterface.LauncherType
import prog8.compilerinterface.OutputType
import prog8.compilerinterface.ZeropageType
import prog8.parser.SourceCode
import prog8tests.helpers.DummyFunctions
import prog8tests.helpers.DummyMemsizer

View File

@ -1,10 +1,7 @@
package prog8tests.codegeneration
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import prog8.codegen.target.C64Target
import prog8tests.helpers.ErrorReporterForTests
import prog8tests.helpers.assertSuccess
import prog8tests.helpers.compileText

View File

@ -4,11 +4,11 @@ import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe
import prog8.ast.Program
import prog8.codegen.cpu6502.AsmGen
import prog8.codegen.target.C64Target
import prog8.codegen.target.c64.C64Zeropage
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram
import prog8.codegen.target.C64Target
import prog8.codegen.target.c64.C64Zeropage
import prog8.compilerinterface.*
import java.nio.file.Path
import kotlin.io.path.name

View File

@ -1,6 +1,9 @@
package prog8.ast
import prog8.ast.base.*
import prog8.ast.base.FatalAstException
import prog8.ast.base.ParentSentinel
import prog8.ast.base.Position
import prog8.ast.base.findParentNode
import prog8.ast.expressions.Expression
import prog8.ast.expressions.IdentifierReference
import prog8.ast.statements.*

View File

@ -4,7 +4,6 @@ import prog8.ast.base.DataType
import prog8.ast.base.FatalAstException
import prog8.ast.base.Position
import prog8.ast.base.VarDeclType
import prog8.ast.expressions.ContainmentCheck
import prog8.ast.expressions.StringLiteralValue
import prog8.ast.statements.*
import prog8.compilerinterface.Encoding

View File

@ -2,7 +2,6 @@ package prog8.ast.antlr
import prog8.ast.base.Position
import prog8.ast.base.SyntaxError
import kotlin.NumberFormatException
fun escape(str: String): String {
val es = str.map {

View File

@ -14,7 +14,7 @@ object Prog8Parser {
fun parseModule(src: SourceCode): Module {
val antlrErrorListener = AntlrErrorListener(src)
val lexer = Prog8ANTLRLexer(src.getCharStream())
val lexer = Prog8ANTLRLexer(CharStreams.fromString(src.text, src.origin))
lexer.removeErrorListeners()
lexer.addErrorListener(antlrErrorListener)
val tokens = CommonTokenStream(lexer)

View File

@ -1,26 +1,16 @@
package prog8.parser
import org.antlr.v4.runtime.CharStream
import org.antlr.v4.runtime.CharStreams
import java.io.File
import java.io.IOException
import java.nio.channels.Channels
import java.nio.charset.CodingErrorAction
import java.nio.file.Path
import kotlin.io.path.*
import kotlin.io.path.Path
import kotlin.io.path.readText
/**
* Encapsulates - and ties together - actual source code (=text)
* and its [origin].
* Encapsulates - and ties together - actual source code (=text) and its [origin].
*/
sealed class SourceCode {
/**
* To be used *only* by the parser (as input to a TokenStream).
* DO NOT mess around with!
*/
internal abstract fun getCharStream(): CharStream
/**
* Whether this [SourceCode] instance was created as a [Resource]
*/
@ -43,11 +33,10 @@ sealed class SourceCode {
/**
* The source code as plain string.
*/
abstract fun readText(): String
abstract val text: String
/**
* Deliberately does NOT return the actual text.
* For this - if at all - use [getCharStream].
* Printable representation, deliberately does NOT return the actual text.
*/
final override fun toString() = "${this.javaClass.name}[${this.origin}]"
@ -68,43 +57,37 @@ sealed class SourceCode {
* Turn a plain String into a [SourceCode] object.
* [origin] will be something like `$stringSourcePrefix44c56085>`.
*/
class Text(val text: String): SourceCode() {
class Text(override val text: String): SourceCode() {
override val isFromResources = false
override val isFromFilesystem = false
override val origin = "$stringSourcePrefix${System.identityHashCode(text).toString(16)}>"
public override fun getCharStream(): CharStream = CharStreams.fromString(text, origin)
override fun readText() = text
}
/**
* Get [SourceCode] from the file represented by the specified Path.
* This does not actually *access* the file, but it does check
* whether it
* * exists
* * is a regular file (ie: not a directory)
* * and is actually readable
* This immediately reads the file fully into memory.
*
* [origin] will be the given path in absolute and normalized form.
* @throws NoSuchFileException if the file does not exist
* @throws AccessDeniedException if the given path points to a directory or the file is non-readable for some other reason
* @throws FileSystemException if the file cannot be read
*/
class File(path: Path): SourceCode() {
private val normalized = path.normalize()
init {
val file = normalized.toFile()
if (!path.exists())
throw NoSuchFileException(file)
if (path.isDirectory())
throw AccessDeniedException(file, reason = "Not a file but a directory")
if (!path.isReadable())
throw AccessDeniedException(file, reason = "Is not readable")
}
override val text: String
override val origin: String
override val isFromResources = false
override val isFromFilesystem = true
override val origin = relative(normalized).toString()
override fun getCharStream(): CharStream = CharStreams.fromPath(normalized)
override fun readText() = normalized.readText()
init {
val normalized = path.normalize()
origin = relative(normalized).toString()
try {
text = normalized.readText()
} catch (nfx: java.nio.file.NoSuchFileException) {
throw NoSuchFileException(normalized.toFile()).also { it.initCause(nfx) }
} catch (iox: IOException) {
throw FileSystemException(normalized.toFile()).also { it.initCause(iox) }
}
}
}
/**
@ -113,6 +96,11 @@ sealed class SourceCode {
class Resource(pathString: String): SourceCode() {
private val normalized = "/" + Path.of(pathString).normalize().toMutableList().joinToString("/")
override val isFromResources = true
override val isFromFilesystem = false
override val origin = "$libraryFilePrefix$normalized"
override val text: String
init {
val rscURL = object {}.javaClass.getResource(normalized)
if (rscURL == null) {
@ -122,21 +110,8 @@ sealed class SourceCode {
reason = "looked in resources rooted at $rscRoot"
)
}
}
override val isFromResources = true
override val isFromFilesystem = false
override val origin = "$libraryFilePrefix$normalized"
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, Charsets.UTF_8, 4096, CodingErrorAction.REPLACE, origin, -1)
}
override fun readText(): String {
val stream = object {}.javaClass.getResourceAsStream(normalized)
return stream!!.bufferedReader().use { r -> r.readText() }
text = stream!!.reader().use { it.readText() }
}
}
@ -144,10 +119,9 @@ sealed class SourceCode {
* SourceCode for internally generated nodes (usually Modules)
*/
class Generated(name: String) : SourceCode() {
override fun getCharStream(): CharStream = throw IOException("generated code nodes doesn't have a stream to read")
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")
override val text: String = "<generated code node, no text representation>"
}
}

View File

@ -1,6 +1,5 @@
package prog8.compilerinterface
import prog8.ast.base.DataType
import java.nio.file.Path

View File

@ -4,7 +4,7 @@ main {
sub start() {
word w1 = -10
byte bb = 2
w1 -= bb-1
w1 -= bb-1 - - -
txt.print_w(w1)
txt.nl()

View File

@ -4,17 +4,17 @@ import org.takes.Request
import org.takes.Response
import org.takes.Take
import org.takes.facets.fork.FkMethods
import org.takes.http.Exit
import org.takes.http.FtBasic
import org.takes.facets.fork.FkRegex
import org.takes.facets.fork.TkFork
import org.takes.http.Exit
import org.takes.http.FtBasic
import org.takes.rq.form.RqFormBase
import org.takes.rs.RsJson
import org.takes.tk.TkSlf4j
import prog8.compiler.CompilerArguments
import javax.json.Json
import prog8.compiler.compileProgram
import java.nio.file.Path
import javax.json.Json
class Jsonding: RsJson.Source {