(fix) propery escape backslashes in embed Wren source inside C (#112)

This commit is contained in:
Josh Goebel 2021-05-21 19:09:05 -04:00 committed by GitHub
parent 9c6b693372
commit 961003d7e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 20 deletions

View File

@ -237,7 +237,7 @@ static const char* ioModuleSource =
" static readLine() {\n"
" return read_ {\n"
" // TODO: Handle Windows line separators.\n"
" var lineSeparator = __buffered.indexOf(\"\n\")\n"
" var lineSeparator = __buffered.indexOf(\"\\n\")\n"
" if (lineSeparator == -1) return null\n"
"\n"
" // Split the line at the separator.\n"

View File

@ -272,10 +272,10 @@ static const char* replModuleSource =
" // don't know how wide the terminal is, erase the longest line we've seen\n"
" // so far.\n"
" if (line.count > _erase.count) _erase = \" \" * line.count\n"
" System.write(\"\r %(_erase)\")\n"
" System.write(\"\\r %(_erase)\")\n"
"\n"
" // Show the prompt at the beginning of the line.\n"
" System.write(\"\r> \")\n"
" System.write(\"\\r> \")\n"
"\n"
" // Write the line.\n"
" System.write(line)\n"
@ -313,9 +313,9 @@ static const char* replModuleSource =
" line = line[0...cursor]\n"
" } else if (byte == Chars.ctrlL) {\n"
" // Clear the screen.\n"
" System.write(\"\x1b[2J\")\n"
" System.write(\"\\x1b[2J\")\n"
" // Move cursor to top left.\n"
" System.write(\"\x1b[H\")\n"
" System.write(\"\\x1b[H\")\n"
" } else {\n"
" // TODO: Ctrl-T to swap chars.\n"
" // TODO: ESC H and F to move to beginning and end of line. (Both ESC\n"
@ -350,11 +350,11 @@ static const char* replModuleSource =
"\n"
" refreshLine(showCompletion) {\n"
" // Erase the whole line.\n"
" System.write(\"\x1b[2K\")\n"
" System.write(\"\\x1b[2K\")\n"
"\n"
" // Show the prompt at the beginning of the line.\n"
" System.write(Color.gray)\n"
" System.write(\"\r> \")\n"
" System.write(\"\\r> \")\n"
" System.write(Color.none)\n"
"\n"
" // Syntax highlight the line.\n"
@ -374,7 +374,7 @@ static const char* replModuleSource =
" }\n"
"\n"
" // Position the cursor.\n"
" System.write(\"\r\x1b[%(2 + cursor)C\")\n"
" System.write(\"\\r\\x1b[%(2 + cursor)C\")\n"
" Stdout.flush()\n"
" }\n"
"\n"
@ -393,19 +393,19 @@ static const char* replModuleSource =
"\n"
"/// ANSI color escape sequences.\n"
"class Color {\n"
" static none { \"\x1b[0m\" }\n"
" static black { \"\x1b[30m\" }\n"
" static red { \"\x1b[31m\" }\n"
" static green { \"\x1b[32m\" }\n"
" static yellow { \"\x1b[33m\" }\n"
" static blue { \"\x1b[34m\" }\n"
" static magenta { \"\x1b[35m\" }\n"
" static cyan { \"\x1b[36m\" }\n"
" static white { \"\x1b[37m\" }\n"
" static none { \"\\x1b[0m\" }\n"
" static black { \"\\x1b[30m\" }\n"
" static red { \"\\x1b[31m\" }\n"
" static green { \"\\x1b[32m\" }\n"
" static yellow { \"\\x1b[33m\" }\n"
" static blue { \"\\x1b[34m\" }\n"
" static magenta { \"\\x1b[35m\" }\n"
" static cyan { \"\\x1b[36m\" }\n"
" static white { \"\\x1b[37m\" }\n"
"\n"
" static gray { \"\x1b[30;1m\" }\n"
" static pink { \"\x1b[31;1m\" }\n"
" static brightWhite { \"\x1b[37;1m\" }\n"
" static gray { \"\\x1b[30;1m\" }\n"
" static pink { \"\\x1b[31;1m\" }\n"
" static brightWhite { \"\\x1b[37;1m\" }\n"
"}\n"
"\n"
"/// Utilities for working with characters.\n"

View File

@ -24,6 +24,7 @@ static const char* {1}ModuleSource =
def wren_to_c_string(input_path, wren_source_lines, module):
wren_source = ""
for line in wren_source_lines:
line = line.replace("\\", "\\\\")
line = line.replace('"', "\\\"")
line = line.replace("\n", "\\n\"")
if wren_source: wren_source += "\n"