From f891fc698c0cac7d78fea9ec1c0a98a3a19947ff Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 12 Oct 2021 21:32:47 +0200 Subject: [PATCH] switched to more featureful Result library --- .idea/codeInsightSettings.xml | 8 ++++++++ .../libraries/michael_bull_kotlin_result_jvm.xml | 15 +++++++++++++++ compiler/build.gradle | 1 + compiler/compiler.iml | 1 + compiler/src/prog8/compiler/Compiler.kt | 16 ++++++++-------- .../compiler/target/cpu6502/codegen/AsmGen.kt | 5 +++-- compilerAst/build.gradle | 3 ++- compilerAst/compilerAst.iml | 1 + examples/test.p8 | 2 +- 9 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 .idea/codeInsightSettings.xml create mode 100644 .idea/libraries/michael_bull_kotlin_result_jvm.xml diff --git a/.idea/codeInsightSettings.xml b/.idea/codeInsightSettings.xml new file mode 100644 index 000000000..9e23bf5ba --- /dev/null +++ b/.idea/codeInsightSettings.xml @@ -0,0 +1,8 @@ + + + + + kotlin.Result + + + \ No newline at end of file diff --git a/.idea/libraries/michael_bull_kotlin_result_jvm.xml b/.idea/libraries/michael_bull_kotlin_result_jvm.xml new file mode 100644 index 000000000..6cb4502f3 --- /dev/null +++ b/.idea/libraries/michael_bull_kotlin_result_jvm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/build.gradle b/compiler/build.gradle index 12cd9081f..b4afc5714 100644 --- a/compiler/build.gradle +++ b/compiler/build.gradle @@ -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' diff --git a/compiler/compiler.iml b/compiler/compiler.iml index 854b6ae2f..3bfbcdad9 100644 --- a/compiler/compiler.iml +++ b/compiler/compiler.iml @@ -21,5 +21,6 @@ + \ No newline at end of file diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 9c7a496e3..c9237ec73 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -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 { +internal fun loadAsmIncludeFile(filename: String, sourcePath: Path): Result { 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()) } } diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 17059c877..2442d717a 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -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" -> { diff --git a/compilerAst/build.gradle b/compilerAst/build.gradle index a6be79821..8eeb093e9 100644 --- a/compilerAst/build.gradle +++ b/compilerAst/build.gradle @@ -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 { diff --git a/compilerAst/compilerAst.iml b/compilerAst/compilerAst.iml index e4955a864..80bcad8dc 100644 --- a/compilerAst/compilerAst.iml +++ b/compilerAst/compilerAst.iml @@ -15,5 +15,6 @@ + \ No newline at end of file diff --git a/examples/test.p8 b/examples/test.p8 index c429b4e4d..7ff6e37be 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,4 +1,4 @@ -%import textio2 +%import textio %import test_stack %zeropage basicsafe