Skip to content

Commit

Permalink
fix(compiler): Escape double quotes and single quote in the string li…
Browse files Browse the repository at this point in the history
…terals before transpiling
  • Loading branch information
mertyildiran committed Nov 29, 2020
1 parent fd80920 commit 9e251b8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/string.kaos
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,19 @@ print a
del a
str a = "hello\t\tworld"
print a

del a
str a = 'hello"world'
print a

del a
str a = "hello'world"
print a

del a
str a = "hello\"world"
print a

del a
str a = 'hello\'world'
print a
4 changes: 4 additions & 0 deletions tests/string.out
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ world
hello world
hello world
hello world
hello"world
hello'world
hello"world
hello'world
26 changes: 26 additions & 0 deletions utilities/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ char* escape_the_sequences_in_string_literal(char* string) {
new_string[i+1] = '\v';
remove_nth_char(new_string, i);
break;
case '"':
remove_nth_char(new_string, i);
break;
case '\'':
remove_nth_char(new_string, i);
break;
default:
break;
}
Expand All @@ -543,8 +549,15 @@ char* escape_the_sequences_in_string_literal(char* string) {
char* escape_string_literal_for_transpiler(char* string) {
char* new_string = malloc(strlen(string) + 1);
strcpy(new_string, string);
bool pass_next = false;
char prev_char = '\0';

for (long long i = 0; i < (long long) strlen(new_string); i++){
if (pass_next) {
pass_next = false;
continue;
}

switch (new_string[i])
{
case '\a':
Expand Down Expand Up @@ -579,9 +592,22 @@ char* escape_string_literal_for_transpiler(char* string) {
new_string = insert_nth_char(new_string, '\\', i);
new_string[i+1] = 'v';
break;
case '"':
if (prev_char == '\\')
break;
new_string = insert_nth_char(new_string, '\\', i);
new_string[i+1] = '"';
pass_next = true;
break;
case '\'':
if (prev_char == '\\')
remove_nth_char(new_string, i-1);
break;
default:
break;
}

prev_char = new_string[i];
}

return new_string;
Expand Down

0 comments on commit 9e251b8

Please sign in to comment.