From 33733a400188a6b5cae9c84002d9e011c8ac06e5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 12 Oct 2021 01:45:32 +0200 Subject: [PATCH] improve errorhandling --- compiler/src/prog8/compiler/Compiler.kt | 16 +++++++++++----- .../prog8/compiler/target/IAssemblyGenerator.kt | 1 + .../prog8/compiler/target/cbm/AssemblyProgram.kt | 7 ++++++- .../compiler/target/cpu6502/codegen/AsmGen.kt | 12 +++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 6891e2c26..9c7a496e3 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -335,12 +335,18 @@ private fun writeAssembly(programAst: Program, compilerOptions.compTarget.machine.zeropage, compilerOptions, outputDir).compileToAssembly() - val assemblerReturnStatus = assembly.assemble(compilerOptions) - return if(assemblerReturnStatus!=0) - Pair(false, "assembler step failed with return code $assemblerReturnStatus") - else { + + return if(assembly.valid && errors.noErrors()) { + val assemblerReturnStatus = assembly.assemble(compilerOptions) + if(assemblerReturnStatus!=0) + Pair(false, "assembler step failed with return code $assemblerReturnStatus") + else { + errors.report() + Pair(true, assembly.name) + } + } else { errors.report() - Pair(true, assembly.name) + Pair(false, "compiler failed with errors") } } diff --git a/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt b/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt index 0b01f492a..d184d05ca 100644 --- a/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt +++ b/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt @@ -11,6 +11,7 @@ internal const val subroutineFloatEvalResultVar1 = "_prog8_float_eval_result1" internal const val subroutineFloatEvalResultVar2 = "_prog8_float_eval_result2" internal interface IAssemblyProgram { + val valid: Boolean val name: String fun assemble(options: CompilationOptions): Int } diff --git a/compiler/src/prog8/compiler/target/cbm/AssemblyProgram.kt b/compiler/src/prog8/compiler/target/cbm/AssemblyProgram.kt index db79dcbf0..30bba8159 100644 --- a/compiler/src/prog8/compiler/target/cbm/AssemblyProgram.kt +++ b/compiler/src/prog8/compiler/target/cbm/AssemblyProgram.kt @@ -9,7 +9,12 @@ import java.nio.file.Path internal const val viceMonListPostfix = "vice-mon-list" -class AssemblyProgram(override val name: String, outputDir: Path, private val compTarget: String) : IAssemblyProgram { +class AssemblyProgram( + override val valid: Boolean, + override val name: String, + outputDir: Path, + private val compTarget: String) : IAssemblyProgram { + private val assemblyFile = outputDir.resolve("$name.asm") private val prgFile = outputDir.resolve("$name.prg") private val binFile = outputDir.resolve("$name.bin") diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 8c55cedc1..17059c877 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -87,7 +87,11 @@ internal class AsmGen(private val program: Program, } } - return AssemblyProgram(program.name, outputDir, compTarget.name) + return if(errors.noErrors()) + AssemblyProgram(true, program.name, outputDir, compTarget.name) + else { + AssemblyProgram(false, "", outputDir, compTarget.name) + } } internal fun isTargetCpu(cpu: CpuType) = compTarget.machine.cpu == cpu @@ -1317,8 +1321,10 @@ $repeatLabel lda $counterVar // TODO: handle %asminclude with SourceCode val includedName = stmt.args[0].str!! val sourcePath = Path(stmt.definingModule.source!!.pathString()) // FIXME: %asminclude inside non-library, non-filesystem module - val sourcecode = loadAsmIncludeFile(includedName, sourcePath).getOrThrow() - assemblyLines.add(sourcecode.trimEnd().trimStart('\n')) + loadAsmIncludeFile(includedName, sourcePath).fold( + onSuccess = { assemblyLines.add(it.trimEnd().trimStart('\n')) }, + onFailure = { errors.err(it.toString(), stmt.position) } + ) } "%asmbinary" -> { val includedName = stmt.args[0].str!!