diff --git a/compiler/src/prog8/compiler/target/cbm/Petscii.kt b/compiler/src/prog8/compiler/target/cbm/Petscii.kt index 04dc4d325..07b98f674 100644 --- a/compiler/src/prog8/compiler/target/cbm/Petscii.kt +++ b/compiler/src/prog8/compiler/target/cbm/Petscii.kt @@ -1,5 +1,6 @@ package prog8.compiler.target.cbm +import prog8.ast.antlr.escape import java.io.CharConversionException object Petscii { @@ -1073,7 +1074,7 @@ object Petscii { } else -> { val case = if (lowercase) "lower" else "upper" - throw CharConversionException("no ${case}Petscii character for '$chr' (${chr.code})") + throw CharConversionException("no ${case}Petscii character for '${escape(chr.toString())}' (${chr.code})") } } } @@ -1110,7 +1111,7 @@ object Petscii { } else -> { val case = if (lowercase) "lower" else "upper" - throw CharConversionException("no ${case}Screencode character for '$chr' (${chr.code})") + throw CharConversionException("no ${case}Screencode character for '${escape(chr.toString())}' (${chr.code})") } } } diff --git a/compilerAst/src/prog8/ast/antlr/EscapeChars.kt b/compilerAst/src/prog8/ast/antlr/EscapeChars.kt index c3c743d1c..7752272e1 100644 --- a/compilerAst/src/prog8/ast/antlr/EscapeChars.kt +++ b/compilerAst/src/prog8/ast/antlr/EscapeChars.kt @@ -10,9 +10,9 @@ fun escape(str: String): String { '\n' -> "\\n" '\r' -> "\\r" '"' -> "\\\"" - in '\u8000'..'\u80ff' -> "\\x" + (it.toInt() - 0x8000).toString(16).padStart(2, '0') + in '\u8000'..'\u80ff' -> "\\x" + (it.code - 0x8000).toString(16).padStart(2, '0') in '\u0000'..'\u00ff' -> it.toString() - else -> "\\u" + it.toInt().toString(16).padStart(4, '0') + else -> "\\u" + it.code.toString(16).padStart(4, '0') } } return es.joinToString("") diff --git a/examples/test.p8 b/examples/test.p8 index 37a4c513c..d1109f13a 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,23 +1,27 @@ -%import textio -%zeropage basicsafe -%option no_sysinit + %import textio ; txt.* + main { + sub start() { + ; ATTENTION: uncomment only one problematic line at a time! -main { + ; Normal string literals, i.e. PETSCII encoding + ; --------------------------------------------- + txt.print("\"") ; fine + txt.print("\n") ; fine + txt.print("\r") ; fine + ; txt.print("\\") ; yields CharConversionException + ; txt.print("xyz\\") ; yields prog8.compiler.AssemblyError - byte[] xs1 = "foo1" ; <<<<<<<<<<<< - str xs2 = "foo2" ; <<<<<<<<<<<< - sub start() { - txt.print(xs1) - stringopt() - } + ; @-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 - sub stringopt() { - str message = "a" - - txt.print(message) - txt.nl() - message[0] = '@' - txt.print(message) - txt.nl() - } -} + ; there may be more... + } + }