switched to more featureful Result library

This commit is contained in:
Irmen de Jong 2021-10-12 21:32:47 +02:00
parent 36bec62c9a
commit f891fc698c
9 changed files with 40 additions and 12 deletions

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaProjectCodeInsightSettings">
<excluded-names>
<name>kotlin.Result</name>
</excluded-names>
</component>
</project>

View File

@ -0,0 +1,15 @@
<component name="libraryTable">
<library name="michael.bull.kotlin.result.jvm" type="repository">
<properties maven-id="com.michael-bull.kotlin-result:kotlin-result-jvm:1.1.12" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/michael-bull/kotlin-result/kotlin-result-jvm/1.1.12/kotlin-result-jvm-1.1.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.5.10/kotlin-stdlib-jdk8-1.5.10.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.5.10/kotlin-stdlib-1.5.10.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.5.10/kotlin-stdlib-jdk7-1.5.10.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.5.10/kotlin-stdlib-common-1.5.10.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -22,6 +22,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
// implementation "org.jetbrains.kotlin:kotlin-reflect"
implementation 'org.jetbrains.kotlinx:kotlinx-cli:0.3.3'
implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:1.1.12"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'

View File

@ -21,5 +21,6 @@
<orderEntry type="library" name="hamcrest" level="project" />
<orderEntry type="library" name="jetbrains.kotlinx.cli.jvm" level="project" />
<orderEntry type="library" name="junit.jupiter" level="project" />
<orderEntry type="library" name="michael.bull.kotlin.result.jvm" level="project" />
</component>
</module>

View File

@ -1,5 +1,6 @@
package prog8.compiler
import com.github.michaelbull.result.*
import prog8.ast.AstToSourceCode
import prog8.ast.IBuiltinFunctions
import prog8.ast.IMemSizer
@ -357,20 +358,19 @@ fun printAst(programAst: Program) {
println()
}
internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): Result<String> {
internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): Result<String, NoSuchFileException> {
return if (filename.startsWith(libraryFilePrefix)) {
runCatching {
return 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() }
}
stream!!.bufferedReader().use { r -> r.readText() }
}.mapError { NoSuchFileException(File(filename)) }
} else {
// first try in the isSameAs folder as where the containing file was imported from
val sib = sourcePath.resolveSibling(filename)
if (sib.toFile().isFile)
Result.success(sib.toFile().readText())
Ok(sib.toFile().readText())
else
Result.success(File(filename).readText())
Ok(File(filename).readText())
}
}

View File

@ -1,5 +1,6 @@
package prog8.compiler.target.cpu6502.codegen
import com.github.michaelbull.result.*
import prog8.ast.*
import prog8.ast.antlr.escape
import prog8.ast.base.*
@ -1322,8 +1323,8 @@ $repeatLabel lda $counterVar
val includedName = stmt.args[0].str!!
val sourcePath = Path(stmt.definingModule.source!!.pathString()) // FIXME: %asminclude inside non-library, non-filesystem module
loadAsmIncludeFile(includedName, sourcePath).fold(
onSuccess = { assemblyLines.add(it.trimEnd().trimStart('\n')) },
onFailure = { errors.err(it.toString(), stmt.position) }
success = { assemblyLines.add(it.trimEnd().trimStart('\n')) },
failure = { errors.err(it.toString(), stmt.position) }
)
}
"%asmbinary" -> {

View File

@ -13,6 +13,7 @@ repositories {
dependencies {
implementation 'org.antlr:antlr4-runtime:4.9.2'
implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:1.1.12"
implementation project(':parser')
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5"
@ -45,7 +46,7 @@ sourceSets {
main {
java {
srcDirs = ["${project.projectDir}/src"]
}
}
}
test {
java {

View File

@ -15,5 +15,6 @@
<orderEntry type="library" name="antlr.antlr4" level="project" />
<orderEntry type="library" name="hamcrest" level="project" />
<orderEntry type="library" name="junit.jupiter" level="project" />
<orderEntry type="library" name="michael.bull.kotlin.result.jvm" level="project" />
</component>
</module>

View File

@ -1,4 +1,4 @@
%import textio2
%import textio
%import test_stack
%zeropage basicsafe