fix crashes for string encoding errors: give normal compiler error instead

This commit is contained in:
Irmen de Jong 2021-05-11 21:33:11 +02:00
parent a20efa56eb
commit 74f918d911
3 changed files with 19 additions and 12 deletions

View File

@ -15,6 +15,7 @@ import prog8.compiler.functions.builtinFunctionReturnType
import prog8.compiler.target.C64Target
import prog8.compiler.target.Cx16Target
import prog8.compiler.target.ICompilationTarget
import java.io.CharConversionException
import java.io.File
import java.util.*
@ -764,6 +765,13 @@ internal class AstChecker(private val program: Program,
override fun visit(string: StringLiteralValue) {
checkValueTypeAndRangeString(DataType.STR, string)
try {
compTarget.encodeString(string.value, string.altEncoding)
} catch (cx: CharConversionException) {
errors.err(cx.message ?: "can't encode string", string.position)
}
super.visit(string)
}

View File

@ -7,7 +7,6 @@ import prog8.ast.base.*
import prog8.ast.expressions.IdentifierReference
import prog8.ast.expressions.NumericLiteralValue
import prog8.ast.statements.AssignTarget
import prog8.compiler.AssemblyError
import prog8.compiler.CompilationOptions
import prog8.compiler.IErrorReporter
import prog8.compiler.Zeropage
@ -75,13 +74,13 @@ internal object C64Target: ICompilationTarget {
try {
if (altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
} catch (x: CharConversionException) {
throw AssemblyError("There was a problem converting a string to the target machine's char encoding: ${x.message}")
throw CharConversionException("can't convert string to target machine's char encoding: ${x.message}")
}
override fun decodeString(bytes: List<Short>, altEncoding: Boolean) =
try {
if (altEncoding) Petscii.decodeScreencode(bytes, true) else Petscii.decodePetscii(bytes, true)
} catch (x: CharConversionException) {
throw AssemblyError("There was a problem decoding to a string: ${x.message}")
throw CharConversionException("can't decode string: ${x.message}")
}
override fun memorySize(dt: DataType): Int {
@ -102,13 +101,13 @@ internal object Cx16Target: ICompilationTarget {
try {
if (altEncoding) Petscii.encodeScreencode(str, true) else Petscii.encodePetscii(str, true)
} catch (x: CharConversionException) {
throw AssemblyError("There was a problem converting a string to the target machine's char encoding: ${x.message}")
throw CharConversionException("can't convert string to target machine's char encoding: ${x.message}")
}
override fun decodeString(bytes: List<Short>, altEncoding: Boolean) =
try {
if (altEncoding) Petscii.decodeScreencode(bytes, true) else Petscii.decodePetscii(bytes, true)
} catch (x: CharConversionException) {
throw AssemblyError("There was a problem decoding to a string: ${x.message}")
throw CharConversionException("can't decode string: ${x.message}")
}
override fun memorySize(dt: DataType): Int {

View File

@ -8,19 +8,19 @@
txt.print("\"") ; fine
txt.print("\n") ; fine
txt.print("\r") ; fine
; txt.print("\\") ; yields CharConversionException
; txt.print("xyz\\") ; yields prog8.compiler.AssemblyError
txt.print("\\") ; yields CharConversionException
txt.print("xyz\\") ; yields prog8.compiler.AssemblyError
; @-strings, i.e. translated into
; the alternate character encoding (Screencodes/pokes)
; ----------------------------------------------------
txt.print(@"\"") ; fine
txt.print(@"\n") ; yields CharConversionException
; txt.print(@"xyz\n") ; yields prog8.compiler.AssemblyError
; txt.print(@"\r") ; yields CharConversionException
; txt.print(@"xyz\r") ; yields prog8.compiler.AssemblyError
; txt.print(@"\\") ; yields CharConversionException
; txt.print(@"\\") ; yields prog8.compiler.AssemblyError
txt.print(@"xyz\n") ; yields prog8.compiler.AssemblyError
txt.print(@"\r") ; yields CharConversionException
txt.print(@"xyz\r") ; yields prog8.compiler.AssemblyError
txt.print(@"\\") ; yields CharConversionException
txt.print(@"\\") ; yields prog8.compiler.AssemblyError
; there may be more...
}