Skip to content

Commit

Permalink
Merge pull request lcompilers#659 from certik/str1
Browse files Browse the repository at this point in the history
Implement string escaping
  • Loading branch information
certik authored Jun 21, 2022
2 parents 3f7aab5 + a1503ae commit 64f1af3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ RUN(NAME expr_08 LABELS llvm c)
RUN(NAME test_types_01 LABELS cpython llvm)
RUN(NAME test_str_01 LABELS cpython llvm)
RUN(NAME test_str_02 LABELS cpython llvm)
RUN(NAME test_str_03 LABELS cpython llvm c)
RUN(NAME test_list_01 LABELS cpython llvm)
RUN(NAME modules_01 LABELS cpython llvm)
RUN(NAME test_math LABELS cpython llvm)
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ex

git clean -dfx
rm -rf b1 b2 b3

# Append "-j4" or "-j" to run in parallel
jn=$1
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/test_str_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_new_line():
print("abc\n")
print("\ndef")
print("x\nyz")

test_new_line()
4 changes: 2 additions & 2 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ R"(
std::vector<std::string> v;
for (size_t i=0; i<x.n_values; i++) {
this->visit_expr(*x.m_values[i]);
ASR::ttype_t* valuei_type = ASRUtils::expr_type(x.m_values[i]);
out += get_print_type(valuei_type, ASR::is_a<ASR::ArrayRef_t>(*x.m_values[i]));
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
out += get_print_type(value_type, ASR::is_a<ASR::ArrayRef_t>(*x.m_values[i]));
if (i+1!=x.n_values) {
out += " ";
}
Expand Down
15 changes: 14 additions & 1 deletion src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,20 @@ R"(#include <stdio.h>


void visit_StringConstant(const ASR::StringConstant_t &x) {
src = "\"" + std::string(x.m_s) + "\"";
src = "\"";
std::string s = x.m_s;
for (size_t idx=0; idx < s.size(); idx++) {
if (s[idx] == '\n') {
src += "\\n";
} else if (s[idx] == '\\') {
src += "\\\\";
} else if (s[idx] == '\"') {
src += "\\\"";
} else {
src += s[idx];
}
}
src += "\"";
last_expr_precedence = 2;
}

Expand Down

0 comments on commit 64f1af3

Please sign in to comment.