Skip to content

Commit

Permalink
Compute the result of string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
namannimmo10 committed Jan 21, 2022
1 parent 62983a8 commit 952c479
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,28 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
} else if (ASRUtils::is_character(*left_type) && ASRUtils::is_character(*right_type)
&& op == ASR::binopType::Add) {
// string concat
dest_type = left_type;
ASR::stropType ops = ASR::stropType::Concat;
ASR::Character_t *left_type2 = ASR::down_cast<ASR::Character_t>(left_type);
ASR::Character_t *right_type2 = ASR::down_cast<ASR::Character_t>(right_type);
LFORTRAN_ASSERT(left_type2->n_dims == 0);
LFORTRAN_ASSERT(right_type2->n_dims == 0);
dest_type = ASR::down_cast<ASR::ttype_t>(
ASR::make_Character_t(al, loc, left_type2->m_kind,
left_type2->m_len + right_type2->m_len, nullptr, nullptr, 0));
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
char* left_value = ASR::down_cast<ASR::ConstantString_t>(
ASRUtils::expr_value(left))->m_s;
char* right_value = ASR::down_cast<ASR::ConstantString_t>(
ASRUtils::expr_value(right))->m_s;
char* result;
std::string result_s = std::string(left_value) + std::string(right_value);
Str s;
s.from_str_view(result_s);
result = s.c_str(al);
LFORTRAN_ASSERT((int64_t)strlen(result) == ASR::down_cast<ASR::Character_t>(dest_type)->m_len)
value = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantString_t(
al, loc, result, dest_type));
}
tmp = ASR::make_StrOp_t(al, loc, left, ops, right, dest_type,
value);
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign1-886f049.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-assign1-886f049.stdout",
"stdout_hash": "2093d0bf986c2069ffb0b0785cd198349c92cc5fc4c96241c0641483",
"stdout_hash": "97e1b8281687c2290f5c4aa794b3958547fa91a331e188f0c5f37cfc",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign1-886f049.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {test_augassign: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Character 1 -2 () []) Source Public Required .false.), r: (Variable 2 r Local () () Default (Integer 4 []) Source Public Required .false.), s: (Variable 2 s Local () () Default (Integer 4 []) Source Public Required .false.)}) test_augassign [] [(= (Var 2 r) (ConstantInteger 0 (Integer 4 [])) ()) (= (Var 2 r) (BinOp (Var 2 r) Add (ConstantInteger 4 (Integer 4 [])) (Integer 4 []) () ()) ()) (= (Var 2 s) (ConstantInteger 5 (Integer 4 [])) ()) (= (Var 2 r) (BinOp (Var 2 r) Mul (Var 2 s) (Integer 4 []) () ()) ()) (= (Var 2 r) (BinOp (Var 2 r) Sub (ConstantInteger 2 (Integer 4 [])) (Integer 4 []) () ()) ()) (= (Var 2 s) (ConstantInteger 10 (Integer 4 [])) ()) (= (Var 2 r) (BinOp (ImplicitCast (Var 2 r) IntegerToReal (Real 8 []) ()) Div (ImplicitCast (Var 2 s) IntegerToReal (Real 8 []) ()) (Real 8 []) () ()) ()) (= (Var 2 a) (ConstantString "" (Character 1 0 () [])) ()) (= (Var 2 a) (StrOp (Var 2 a) Concat (ConstantString "test" (Character 1 4 () [])) (Character 1 -2 () []) ()) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {test_augassign: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Character 1 -2 () []) Source Public Required .false.), r: (Variable 2 r Local () () Default (Integer 4 []) Source Public Required .false.), s: (Variable 2 s Local () () Default (Integer 4 []) Source Public Required .false.)}) test_augassign [] [(= (Var 2 r) (ConstantInteger 0 (Integer 4 [])) ()) (= (Var 2 r) (BinOp (Var 2 r) Add (ConstantInteger 4 (Integer 4 [])) (Integer 4 []) () ()) ()) (= (Var 2 s) (ConstantInteger 5 (Integer 4 [])) ()) (= (Var 2 r) (BinOp (Var 2 r) Mul (Var 2 s) (Integer 4 []) () ()) ()) (= (Var 2 r) (BinOp (Var 2 r) Sub (ConstantInteger 2 (Integer 4 [])) (Integer 4 []) () ()) ()) (= (Var 2 s) (ConstantInteger 10 (Integer 4 [])) ()) (= (Var 2 r) (BinOp (ImplicitCast (Var 2 r) IntegerToReal (Real 8 []) ()) Div (ImplicitCast (Var 2 s) IntegerToReal (Real 8 []) ()) (Real 8 []) () ()) ()) (= (Var 2 a) (ConstantString "" (Character 1 0 () [])) ()) (= (Var 2 a) (StrOp (Var 2 a) Concat (ConstantString "test" (Character 1 4 () [])) (Character 1 2 () []) ()) ())] Source Public Implementation () .false. .false.)}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-expr5-645ffcc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr5-645ffcc.stdout",
"stdout_hash": "c607265fa6b01ba1b5ea13fab3721e5904c6ab51d45d86dea4f4eb26",
"stdout_hash": "088875ac24fef561316a08e2c35cba013d9b8d1d268b5b7a31f21032",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-expr5-645ffcc.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {test_strOP: (Subroutine (SymbolTable 2 {s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_strOP [] [(= (Var 2 s) (StrOp (ConstantString "a" (Character 1 1 () [])) Repeat (ConstantInteger 2 (Integer 4 [])) (Character 1 1 () []) ()) ()) (= (Var 2 s) (StrOp (ConstantInteger 4 (Integer 4 [])) Repeat (ConstantString "bb" (Character 1 2 () [])) (Character 1 2 () []) ()) ()) (= (Var 2 s) (StrOp (StrOp (ConstantInteger 3 (Integer 4 [])) Repeat (ConstantString "a" (Character 1 1 () [])) (Character 1 1 () []) ()) Repeat (ConstantInteger 3 (Integer 4 [])) (Character 1 1 () []) ()) ()) (= (Var 2 s) (StrOp (ConstantString "3" (Character 1 1 () [])) Concat (ConstantString "4" (Character 1 1 () [])) (Character 1 1 () []) ()) ()) (= (Var 2 s) (StrOp (Var 2 s) Concat (ConstantString "test" (Character 1 4 () [])) (Character 1 -2 () []) ()) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {test_strOP: (Subroutine (SymbolTable 2 {s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_strOP [] [(= (Var 2 s) (StrOp (ConstantString "a" (Character 1 1 () [])) Repeat (ConstantInteger 2 (Integer 4 [])) (Character 1 1 () []) ()) ()) (= (Var 2 s) (StrOp (ConstantInteger 4 (Integer 4 [])) Repeat (ConstantString "bb" (Character 1 2 () [])) (Character 1 2 () []) ()) ()) (= (Var 2 s) (StrOp (StrOp (ConstantInteger 3 (Integer 4 [])) Repeat (ConstantString "a" (Character 1 1 () [])) (Character 1 1 () []) ()) Repeat (ConstantInteger 3 (Integer 4 [])) (Character 1 1 () []) ()) ()) (= (Var 2 s) (StrOp (ConstantString "3" (Character 1 1 () [])) Concat (ConstantString "4" (Character 1 1 () [])) (Character 1 2 () []) (ConstantString "34" (Character 1 2 () []))) ()) (= (Var 2 s) (StrOp (Var 2 s) Concat (ConstantString "test" (Character 1 4 () [])) (Character 1 2 () []) ()) ())] Source Public Implementation () .false. .false.)}) [])

0 comments on commit 952c479

Please sign in to comment.