diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 11bc16ff6..0b56b7e52 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -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) } diff --git a/compiler/src/prog8/compiler/target/ICompilationTarget.kt b/compiler/src/prog8/compiler/target/ICompilationTarget.kt index f6563f3ff..b778e3182 100644 --- a/compiler/src/prog8/compiler/target/ICompilationTarget.kt +++ b/compiler/src/prog8/compiler/target/ICompilationTarget.kt @@ -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, 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, 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 { diff --git a/examples/test.p8 b/examples/test.p8 index d1109f13a..220bb3873 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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... }