From 3c12acbb43fef8ad4560b467f20c955f6c904fe7 Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sun, 18 Aug 2019 17:00:26 +0200 Subject: [PATCH] Added support for \\ escape code. #241 --- .../dk/camelot64/kickc/model/values/ConstantChar.java | 4 ++++ .../camelot64/kickc/model/values/ConstantString.java | 6 ++++++ src/test/kc/string-escapes-0.kc | 2 +- src/test/kc/string-escapes-2.kc | 2 +- src/test/ref/string-escapes-0.asm | 2 +- src/test/ref/string-escapes-0.log | 10 +++++----- src/test/ref/string-escapes-0.sym | 2 +- src/test/ref/string-escapes-2.asm | 2 +- src/test/ref/string-escapes-2.log | 10 +++++----- src/test/ref/string-escapes-2.sym | 2 +- 10 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantChar.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantChar.java index 39b6b67d7..7f2e5e444 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantChar.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantChar.java @@ -76,6 +76,8 @@ public class ConstantChar implements ConstantLiteral { return '\"'; case '\'': return '\''; + case '\\': + return '\\'; default: throw new CompileError("Illegal char escape sequence \\" + charString.charAt(1)); } @@ -99,6 +101,8 @@ public class ConstantChar implements ConstantLiteral { return "\\f"; case '\'': return "\\'"; + case '\\': + return "\\\\"; default: return Character.toString(aChar); } diff --git a/src/main/java/dk/camelot64/kickc/model/values/ConstantString.java b/src/main/java/dk/camelot64/kickc/model/values/ConstantString.java index 7dfa6b131..4ec394edd 100644 --- a/src/main/java/dk/camelot64/kickc/model/values/ConstantString.java +++ b/src/main/java/dk/camelot64/kickc/model/values/ConstantString.java @@ -133,6 +133,9 @@ public class ConstantString implements ConstantLiteral { case '\'': stringChar = '\''; break; + case '\\': + stringChar = '\\'; + break; default: throw new CompileError("Illegal string escape sequence \\" + escapeChar); } @@ -167,6 +170,9 @@ public class ConstantString implements ConstantLiteral { case '"': stringResult.append("\\\""); break; + case '\\': + stringResult.append("\\\\"); + break; default: stringResult.append(stringChar); } diff --git a/src/test/kc/string-escapes-0.kc b/src/test/kc/string-escapes-0.kc index 280e1f1ae..6598f241e 100644 --- a/src/test/kc/string-escapes-0.kc +++ b/src/test/kc/string-escapes-0.kc @@ -1,6 +1,6 @@ // Test using some simple supported string escapes \r \f \n \' \" -char[] MESSAGE = "\r\f\n\"\'"; +char[] MESSAGE = "\r\f\n\"\'\\"; char* SCREEN = 0x0400; void main() { diff --git a/src/test/kc/string-escapes-2.kc b/src/test/kc/string-escapes-2.kc index e4a14b6fa..6ecb823b2 100644 --- a/src/test/kc/string-escapes-2.kc +++ b/src/test/kc/string-escapes-2.kc @@ -2,7 +2,7 @@ #pragma encoding(petscii_mixed) -char[] MESSAGE = "hello\nworld"; +char[] MESSAGE = "hello\nworld\\"; const char* memA = 0xff; diff --git a/src/test/ref/string-escapes-0.asm b/src/test/ref/string-escapes-0.asm index 79bff9cf6..ce490105f 100644 --- a/src/test/ref/string-escapes-0.asm +++ b/src/test/ref/string-escapes-0.asm @@ -16,5 +16,5 @@ main: { iny jmp b1 } - MESSAGE: .text @"\r\f\n\"'" + MESSAGE: .text @"\r\f\n\"'\\" .byte 0 diff --git a/src/test/ref/string-escapes-0.log b/src/test/ref/string-escapes-0.log index 8350f486c..9bd4b777a 100644 --- a/src/test/ref/string-escapes-0.log +++ b/src/test/ref/string-escapes-0.log @@ -35,7 +35,7 @@ main::@return: scope:[main] from main::@1 SYMBOL TABLE SSA (const string) $0 = (string) " -"'" +"'\" (label) @1 (label) @2 (label) @begin @@ -198,7 +198,7 @@ main: { jmp b1 } // File Data - MESSAGE: .text @"\r\f\n\"'" + MESSAGE: .text @"\r\f\n\"'\\" .byte 0 REGISTER UPLIFT POTENTIAL REGISTERS @@ -272,7 +272,7 @@ main: { jmp b1 } // File Data - MESSAGE: .text @"\r\f\n\"'" + MESSAGE: .text @"\r\f\n\"'\\" .byte 0 ASSEMBLER OPTIMIZATIONS @@ -303,7 +303,7 @@ FINAL SYMBOL TABLE (label) @end (byte[]) MESSAGE (const byte[]) MESSAGE#0 MESSAGE = (string) " -"'" +"'\" (byte*) SCREEN (const byte*) SCREEN#0 SCREEN = (byte*) 1024 (void()) main() @@ -365,6 +365,6 @@ main: { jmp b1 } // File Data - MESSAGE: .text @"\r\f\n\"'" + MESSAGE: .text @"\r\f\n\"'\\" .byte 0 diff --git a/src/test/ref/string-escapes-0.sym b/src/test/ref/string-escapes-0.sym index 010d706d9..605a182f8 100644 --- a/src/test/ref/string-escapes-0.sym +++ b/src/test/ref/string-escapes-0.sym @@ -3,7 +3,7 @@ (label) @end (byte[]) MESSAGE (const byte[]) MESSAGE#0 MESSAGE = (string) " -"'" +"'\" (byte*) SCREEN (const byte*) SCREEN#0 SCREEN = (byte*) 1024 (void()) main() diff --git a/src/test/ref/string-escapes-2.asm b/src/test/ref/string-escapes-2.asm index aa0843be1..77414a57b 100644 --- a/src/test/ref/string-escapes-2.asm +++ b/src/test/ref/string-escapes-2.asm @@ -27,5 +27,5 @@ chrout: { rts } .encoding "petscii_mixed" - MESSAGE: .text @"hello\nworld" + MESSAGE: .text @"hello\nworld\\" .byte 0 diff --git a/src/test/ref/string-escapes-2.log b/src/test/ref/string-escapes-2.log index 6b0f881c4..333c3700d 100644 --- a/src/test/ref/string-escapes-2.log +++ b/src/test/ref/string-escapes-2.log @@ -47,7 +47,7 @@ chrout::@return: scope:[chrout] from chrout SYMBOL TABLE SSA (const string) $0 = (string) "hello -world"pm +world\"pm (label) @2 (label) @3 (label) @begin @@ -260,7 +260,7 @@ chrout: { } // File Data .encoding "petscii_mixed" - MESSAGE: .text @"hello\nworld" + MESSAGE: .text @"hello\nworld\\" .byte 0 REGISTER UPLIFT POTENTIAL REGISTERS @@ -365,7 +365,7 @@ chrout: { } // File Data .encoding "petscii_mixed" - MESSAGE: .text @"hello\nworld" + MESSAGE: .text @"hello\nworld\\" .byte 0 ASSEMBLER OPTIMIZATIONS @@ -402,7 +402,7 @@ FINAL SYMBOL TABLE (label) @end (byte[]) MESSAGE (const byte[]) MESSAGE#0 MESSAGE = (string) "hello -world"pm +world\"pm (void()) chrout((byte) chrout::c) (label) chrout::@return (byte) chrout::c @@ -491,6 +491,6 @@ chrout: { } // File Data .encoding "petscii_mixed" - MESSAGE: .text @"hello\nworld" + MESSAGE: .text @"hello\nworld\\" .byte 0 diff --git a/src/test/ref/string-escapes-2.sym b/src/test/ref/string-escapes-2.sym index acfa2c30e..be62ce685 100644 --- a/src/test/ref/string-escapes-2.sym +++ b/src/test/ref/string-escapes-2.sym @@ -3,7 +3,7 @@ (label) @end (byte[]) MESSAGE (const byte[]) MESSAGE#0 MESSAGE = (string) "hello -world"pm +world\"pm (void()) chrout((byte) chrout::c) (label) chrout::@return (byte) chrout::c