From dfbef8495d777d4399b73e3657730b50f78a5c48 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 30 Oct 2021 17:05:23 +0200 Subject: [PATCH] got rid of ParsingFailedError --- .../target/c64/C64MachineDefinition.kt | 6 +++--- .../target/cx16/CX16MachineDefinition.kt | 4 ++-- compiler/build/tmp/test/readme.txt | 1 - compiler/src/prog8/CompilerMain.kt | 3 --- .../compiler/BeforeAsmGenerationAstChanger.kt | 4 ++-- compiler/src/prog8/compiler/Compiler.kt | 18 ++++++++++-------- .../astprocessing/VerifyFunctionArgTypes.kt | 6 +++--- compiler/test/TestNumbers.kt | 10 +++++----- compiler/test/ZeropageTests.kt | 10 +++++----- compilerAst/build/tmp/test/readme.txt | 1 - compilerAst/src/prog8/parser/Prog8Parser.kt | 9 +-------- .../compilerinterface/BuiltinFunctions.kt | 2 +- .../src/prog8/compilerinterface/Exceptions.kt | 4 +++- .../prog8/compilerinterface/IErrorReporter.kt | 3 +-- .../src/prog8/compilerinterface/Zeropage.kt | 6 +++--- 15 files changed, 39 insertions(+), 48 deletions(-) delete mode 100644 compiler/build/tmp/test/readme.txt delete mode 100644 compilerAst/build/tmp/test/readme.txt diff --git a/codeGeneration/src/prog8/compiler/target/c64/C64MachineDefinition.kt b/codeGeneration/src/prog8/compiler/target/c64/C64MachineDefinition.kt index c466b5e28..d5aebc664 100644 --- a/codeGeneration/src/prog8/compiler/target/c64/C64MachineDefinition.kt +++ b/codeGeneration/src/prog8/compiler/target/c64/C64MachineDefinition.kt @@ -84,7 +84,7 @@ object C64MachineDefinition: IMachineDefinition { init { if (options.floats && options.zeropage !in arrayOf(ZeropageType.FLOATSAFE, ZeropageType.BASICSAFE, ZeropageType.DONTUSE )) - throw CompilerException("when floats are enabled, zero page type should be 'floatsafe' or 'basicsafe' or 'dontuse'") + throw InternalCompilerException("when floats are enabled, zero page type should be 'floatsafe' or 'basicsafe' or 'dontuse'") if (options.zeropage == ZeropageType.FULL) { free.addAll(0x04..0xf9) @@ -149,7 +149,7 @@ object C64MachineDefinition: IMachineDefinition { val flt = num.toDouble() if (flt < FLOAT_MAX_NEGATIVE || flt > FLOAT_MAX_POSITIVE) - throw CompilerException("floating point number out of 5-byte mflpt range: $this") + throw InternalCompilerException("floating point number out of 5-byte mflpt range: $this") if (flt == 0.0) return zero @@ -170,7 +170,7 @@ object C64MachineDefinition: IMachineDefinition { return when { exponent < 0 -> zero // underflow, use zero instead - exponent > 255 -> throw CompilerException("floating point overflow: $this") + exponent > 255 -> throw InternalCompilerException("floating point overflow: $this") exponent == 0 -> zero else -> { val mantLong = mantissa.toLong() diff --git a/codeGeneration/src/prog8/compiler/target/cx16/CX16MachineDefinition.kt b/codeGeneration/src/prog8/compiler/target/cx16/CX16MachineDefinition.kt index 29244c4f1..45822a391 100644 --- a/codeGeneration/src/prog8/compiler/target/cx16/CX16MachineDefinition.kt +++ b/codeGeneration/src/prog8/compiler/target/cx16/CX16MachineDefinition.kt @@ -97,7 +97,7 @@ object CX16MachineDefinition: IMachineDefinition { init { if (options.floats && options.zeropage !in arrayOf(ZeropageType.BASICSAFE, ZeropageType.DONTUSE )) - throw CompilerException("when floats are enabled, zero page type should be 'basicsafe' or 'dontuse'") + throw InternalCompilerException("when floats are enabled, zero page type should be 'basicsafe' or 'dontuse'") // the addresses 0x02 to 0x21 (inclusive) are taken for sixteen virtual 16-bit api registers. @@ -115,7 +115,7 @@ object CX16MachineDefinition: IMachineDefinition { ZeropageType.DONTUSE -> { free.clear() // don't use zeropage at all } - else -> throw CompilerException("for this machine target, zero page type 'floatsafe' is not available. ${options.zeropage}") + else -> throw InternalCompilerException("for this machine target, zero page type 'floatsafe' is not available. ${options.zeropage}") } removeReservedFromFreePool() diff --git a/compiler/build/tmp/test/readme.txt b/compiler/build/tmp/test/readme.txt deleted file mode 100644 index b64b44e52..000000000 --- a/compiler/build/tmp/test/readme.txt +++ /dev/null @@ -1 +0,0 @@ -tmp files for unit tests diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 65c4354ac..48afb75f4 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -6,7 +6,6 @@ import prog8.compiler.CompilationResult import prog8.compiler.compileProgram import prog8.compiler.target.C64Target import prog8.compiler.target.Cx16Target -import prog8.parser.ParsingFailedError import java.io.File import java.nio.file.FileSystems import java.nio.file.Path @@ -119,8 +118,6 @@ private fun compileMain(args: Array): Boolean { compilationTarget, srcdirs, outputPath) if(!compilationResult.success) return false - } catch (x: ParsingFailedError) { - return false } catch (x: AstException) { return false } diff --git a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt index 8c0d0d51a..879087823 100644 --- a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt +++ b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt @@ -11,7 +11,7 @@ import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstModification import prog8.ast.walk.IAstVisitor import prog8.compiler.astprocessing.isSubroutineParameter -import prog8.compilerinterface.CompilerException +import prog8.compilerinterface.InternalCompilerException import prog8.compilerinterface.ICompilationTarget import prog8.compilerinterface.IErrorReporter import prog8.compilerinterface.isInRegularRAMof @@ -206,7 +206,7 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, val errors: I if((binExpr.operator=="==" || binExpr.operator=="!=") && (binExpr.left as? NumericLiteralValue)?.number==0 && (binExpr.right as? NumericLiteralValue)?.number!=0) - throw CompilerException("if 0==X should have been swapped to if X==0") + throw InternalCompilerException("if 0==X should have been swapped to if X==0") // split the conditional expression into separate variables if the operand(s) is not simple. // DISABLED FOR NOW AS IT GENEREATES LARGER CODE IN THE SIMPLE CASES LIKE IF X {...} or IF NOT X {...} diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 3b7109505..1f6a83193 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -16,7 +16,6 @@ import prog8.compiler.target.cpu6502.codegen.AsmGen import prog8.compilerinterface.* import prog8.optimizer.* import prog8.parser.ParseError -import prog8.parser.ParsingFailedError import java.nio.file.Path import kotlin.io.path.Path import kotlin.io.path.nameWithoutExtension @@ -90,10 +89,12 @@ fun compileProgram(filepath: Path, System.err.print("\u001b[91m") // bright red System.err.println("${px.position.toClickableStr()} parse error: ${px.message}".trim()) System.err.print("\u001b[0m") // reset - } catch (pfx: ParsingFailedError) { - System.err.print("\u001b[91m") // bright red - System.err.println(pfx.message) - System.err.print("\u001b[0m") // reset + } catch (ac: AbortCompilation) { + if(!ac.message.isNullOrEmpty()) { + System.err.print("\u001b[91m") // bright red + System.err.println(ac.message) + System.err.print("\u001b[0m") // reset + } } catch (nsf: NoSuchFileException) { System.err.print("\u001b[91m") // bright red System.err.println("File not found: ${nsf.message}") @@ -168,9 +169,6 @@ fun parseImports(filepath: Path, .filter { it.isFromFilesystem } .map { Path(it.origin) } val compilerOptions = determineCompilationOptions(program, compTarget) - if (compilerOptions.launcher == LauncherType.BASIC && compilerOptions.output != OutputType.PRG) - throw ParsingFailedError("${program.modules.first().position} BASIC launcher requires output type PRG.") - // depending on the machine and compiler options we may have to include some libraries for(lib in compTarget.machine.importLibs(compilerOptions, compTarget.name)) importer.importLibraryModule(lib) @@ -178,7 +176,11 @@ fun parseImports(filepath: Path, // always import prog8_lib and math importer.importLibraryModule("math") importer.importLibraryModule("prog8_lib") + + if (compilerOptions.launcher == LauncherType.BASIC && compilerOptions.output != OutputType.PRG) + errors.err("BASIC launcher requires output type PRG", program.toplevelModule.position) errors.report() + return Triple(program, compilerOptions, importedFiles) } diff --git a/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt index 8f342ad82..6b1c31050 100644 --- a/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt +++ b/compiler/src/prog8/compiler/astprocessing/VerifyFunctionArgTypes.kt @@ -9,20 +9,20 @@ import prog8.ast.expressions.TypecastExpression import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor import prog8.compilerinterface.BuiltinFunctions -import prog8.compilerinterface.CompilerException +import prog8.compilerinterface.InternalCompilerException class VerifyFunctionArgTypes(val program: Program) : IAstVisitor { override fun visit(functionCall: FunctionCall) { val error = checkTypes(functionCall as IFunctionCall, program) if(error!=null) - throw CompilerException(error) + throw InternalCompilerException(error) } override fun visit(functionCallStatement: FunctionCallStatement) { val error = checkTypes(functionCallStatement as IFunctionCall, program) if (error!=null) - throw CompilerException(error) + throw InternalCompilerException(error) } companion object { diff --git a/compiler/test/TestNumbers.kt b/compiler/test/TestNumbers.kt index 3ae85e6c6..71c44d29e 100644 --- a/compiler/test/TestNumbers.kt +++ b/compiler/test/TestNumbers.kt @@ -9,7 +9,7 @@ import prog8.ast.toHex import prog8.compiler.target.c64.C64MachineDefinition.FLOAT_MAX_NEGATIVE import prog8.compiler.target.c64.C64MachineDefinition.FLOAT_MAX_POSITIVE import prog8.compiler.target.c64.C64MachineDefinition.Mflpt5 -import prog8.compilerinterface.CompilerException +import prog8.compilerinterface.InternalCompilerException import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -81,10 +81,10 @@ class TestNumbers { assertThat(Mflpt5.fromNumber(1.7e-39), equalTo(Mflpt5(0x00, 0x00, 0x00, 0x00, 0x00))) assertThat(Mflpt5.fromNumber(-1.7e-38), equalTo(Mflpt5(0x03, 0xb9, 0x1d, 0x15, 0x63))) assertThat(Mflpt5.fromNumber(-1.7e-39), equalTo(Mflpt5(0x00, 0x00, 0x00, 0x00, 0x00))) - assertFailsWith { Mflpt5.fromNumber(1.7014118346e+38) } - assertFailsWith { Mflpt5.fromNumber(-1.7014118346e+38) } - assertFailsWith { Mflpt5.fromNumber(1.7014118347e+38) } - assertFailsWith { Mflpt5.fromNumber(-1.7014118347e+38) } + assertFailsWith { Mflpt5.fromNumber(1.7014118346e+38) } + assertFailsWith { Mflpt5.fromNumber(-1.7014118346e+38) } + assertFailsWith { Mflpt5.fromNumber(1.7014118347e+38) } + assertFailsWith { Mflpt5.fromNumber(-1.7014118347e+38) } } @Test diff --git a/compiler/test/ZeropageTests.kt b/compiler/test/ZeropageTests.kt index 7c77eaa58..b8b41dae3 100644 --- a/compiler/test/ZeropageTests.kt +++ b/compiler/test/ZeropageTests.kt @@ -91,11 +91,11 @@ class TestC64Zeropage { @Test fun testZpFloatEnable() { val zp = C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FULL, emptyList(), false, false, C64Target)) - assertFailsWith { + assertFailsWith { zp.allocate("", DataType.FLOAT, null, errors) } val zp2 = C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.DONTUSE, emptyList(), true, false, C64Target)) - assertFailsWith { + assertFailsWith { zp2.allocate("", DataType.FLOAT, null, errors) } val zp3 = C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FLOATSAFE, emptyList(), true, false, C64Target)) @@ -110,10 +110,10 @@ class TestC64Zeropage { C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FLOATSAFE, emptyList(), false, false, C64Target)) C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.BASICSAFE, emptyList(), true, false, C64Target)) C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FLOATSAFE, emptyList(), true, false, C64Target)) - assertFailsWith { + assertFailsWith { C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FULL, emptyList(), true, false, C64Target)) } - assertFailsWith { + assertFailsWith { C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.KERNALSAFE, emptyList(), true, false, C64Target)) } } @@ -123,7 +123,7 @@ class TestC64Zeropage { val zp = C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.DONTUSE, emptyList(), false, false, C64Target)) println(zp.free) assertEquals(0, zp.availableBytes()) - assertFailsWith { + assertFailsWith { zp.allocate("", DataType.BYTE, null, errors) } } diff --git a/compilerAst/build/tmp/test/readme.txt b/compilerAst/build/tmp/test/readme.txt deleted file mode 100644 index fe0dcd070..000000000 --- a/compilerAst/build/tmp/test/readme.txt +++ /dev/null @@ -1 +0,0 @@ -tmp outputs for the unit tests diff --git a/compilerAst/src/prog8/parser/Prog8Parser.kt b/compilerAst/src/prog8/parser/Prog8Parser.kt index 9801d6593..b8210f318 100644 --- a/compilerAst/src/prog8/parser/Prog8Parser.kt +++ b/compilerAst/src/prog8/parser/Prog8Parser.kt @@ -8,14 +8,7 @@ import prog8.ast.statements.Block import prog8.ast.statements.Directive -open class ParsingFailedError(override var message: String) : Exception(message) - -class ParseError(override var message: String, val position: Position, cause: RuntimeException) - : ParsingFailedError("${position.toClickableStr()}$message") { - init { - initCause(cause) - } -} +class ParseError(override var message: String, val position: Position, cause: RuntimeException): Exception(message, cause) object Prog8Parser { diff --git a/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt b/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt index 9471e5fe3..0ba762b10 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt @@ -339,7 +339,7 @@ private fun builtinLen(args: List, position: Position, program: Prog NumericLiteralValue.optimalInteger(refLv.value.length, args[0].position) } in NumericDatatypes -> throw SyntaxError("cannot use len on numeric value, did you mean sizeof?", args[0].position) - else -> throw CompilerException("weird datatype") + else -> throw InternalCompilerException("weird datatype") } } diff --git a/compilerInterfaces/src/prog8/compilerinterface/Exceptions.kt b/compilerInterfaces/src/prog8/compilerinterface/Exceptions.kt index 288d8573e..1d82dc6d8 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/Exceptions.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/Exceptions.kt @@ -1,3 +1,5 @@ package prog8.compilerinterface -class CompilerException(message: String?) : Exception(message) +class InternalCompilerException(message: String?) : Exception(message) + +class AbortCompilation(message: String?) : Exception(message) diff --git a/compilerInterfaces/src/prog8/compilerinterface/IErrorReporter.kt b/compilerInterfaces/src/prog8/compilerinterface/IErrorReporter.kt index e48ba51b8..f0faf2c7f 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/IErrorReporter.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/IErrorReporter.kt @@ -1,7 +1,6 @@ package prog8.compilerinterface import prog8.ast.base.Position -import prog8.parser.ParsingFailedError interface IErrorReporter { @@ -11,7 +10,7 @@ interface IErrorReporter { fun report() fun finalizeNumErrors(numErrors: Int, numWarnings: Int) { if(numErrors>0) - throw ParsingFailedError("There are $numErrors errors and $numWarnings warnings.") + throw AbortCompilation("There are $numErrors errors and $numWarnings warnings.") } } diff --git a/compilerInterfaces/src/prog8/compilerinterface/Zeropage.kt b/compilerInterfaces/src/prog8/compilerinterface/Zeropage.kt index 2d27cb3b4..0c6c71e60 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/Zeropage.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/Zeropage.kt @@ -54,7 +54,7 @@ abstract class Zeropage(protected val options: CompilationOptions) { assert(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"scopedname can't be allocated twice"} if(options.zeropage== ZeropageType.DONTUSE) - throw CompilerException("zero page usage has been disabled") + throw InternalCompilerException("zero page usage has been disabled") val size = when (datatype) { @@ -67,9 +67,9 @@ abstract class Zeropage(protected val options: CompilationOptions) { else errors.warn("$scopedname: allocated a large value (float) in zeropage", position ?: Position.DUMMY) 5 - } else throw CompilerException("floating point option not enabled") + } else throw InternalCompilerException("floating point option not enabled") } - else -> throw CompilerException("cannot put datatype $datatype in zeropage") + else -> throw InternalCompilerException("cannot put datatype $datatype in zeropage") } if(free.size > 0) {