Skip to content

Commit 9350838

Browse files
authored
Merge pull request #2216 from Shaikh-Ubaid/fix_expr_as_stmt
ASR: Fix expression as statement
2 parents 1b5bafd + 3e183fd commit 9350838

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ RUN(NAME test_list_repeat LABELS cpython llvm NOFAST)
507507
RUN(NAME test_list_reverse LABELS cpython llvm)
508508
RUN(NAME test_list_pop LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
509509
RUN(NAME test_list_pop2 LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
510+
RUN(NAME test_list_pop3 LABELS cpython llvm)
510511
RUN(NAME test_list_compare LABELS cpython llvm)
511512
RUN(NAME test_tuple_01 LABELS cpython llvm c)
512513
RUN(NAME test_tuple_02 LABELS cpython llvm c NOFAST)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from lpython import i32
2+
3+
def main0():
4+
a: list[i32] = [3, 4, 5]
5+
i: i32
6+
for i in range(10):
7+
a.append(1)
8+
a.pop()
9+
10+
print(a)
11+
assert a[-1] == 5
12+
assert len(a) == 3
13+
14+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,24 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
11241124
}
11251125

11261126

1127+
ASR::asr_t* make_dummy_assignment(ASR::expr_t* expr) {
1128+
ASR::ttype_t* type = ASRUtils::expr_type(expr);
1129+
std::string dummy_ret_name = current_scope->get_unique_name("__lcompilers_dummy", false);
1130+
SetChar variable_dependencies_vec;
1131+
variable_dependencies_vec.reserve(al, 1);
1132+
ASRUtils::collect_variable_dependencies(al, variable_dependencies_vec, type);
1133+
ASR::asr_t* variable_asr = ASR::make_Variable_t(al, expr->base.loc, current_scope,
1134+
s2c(al, dummy_ret_name), variable_dependencies_vec.p,
1135+
variable_dependencies_vec.size(), ASR::intentType::Local,
1136+
nullptr, nullptr, ASR::storage_typeType::Default,
1137+
type, nullptr, ASR::abiType::Source, ASR::accessType::Public,
1138+
ASR::presenceType::Required, false);
1139+
ASR::symbol_t* variable_sym = ASR::down_cast<ASR::symbol_t>(variable_asr);
1140+
current_scope->add_symbol(dummy_ret_name, variable_sym);
1141+
ASR::expr_t* variable_var = ASRUtils::EXPR(ASR::make_Var_t(al, expr->base.loc, variable_sym));
1142+
return ASR::make_Assignment_t(al, expr->base.loc, variable_var, expr, nullptr);
1143+
}
1144+
11271145
// Function to create appropriate call based on symbol type. If it is external
11281146
// generic symbol then it changes the name accordingly.
11291147
ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, SymbolTable *current_scope,
@@ -1290,20 +1308,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
12901308
s_generic, args_new.p, args_new.size(),
12911309
a_type, value, nullptr);
12921310
if( ignore_return_value ) {
1293-
std::string dummy_ret_name = current_scope->get_unique_name("__lcompilers_dummy", false);
1294-
SetChar variable_dependencies_vec;
1295-
variable_dependencies_vec.reserve(al, 1);
1296-
ASRUtils::collect_variable_dependencies(al, variable_dependencies_vec, a_type);
1297-
ASR::asr_t* variable_asr = ASR::make_Variable_t(al, loc, current_scope,
1298-
s2c(al, dummy_ret_name), variable_dependencies_vec.p,
1299-
variable_dependencies_vec.size(), ASR::intentType::Local,
1300-
nullptr, nullptr, ASR::storage_typeType::Default,
1301-
a_type, nullptr, ASR::abiType::Source, ASR::accessType::Public,
1302-
ASR::presenceType::Required, false);
1303-
ASR::symbol_t* variable_sym = ASR::down_cast<ASR::symbol_t>(variable_asr);
1304-
current_scope->add_symbol(dummy_ret_name, variable_sym);
1305-
ASR::expr_t* variable_var = ASRUtils::EXPR(ASR::make_Var_t(al, loc, variable_sym));
1306-
return ASR::make_Assignment_t(al, loc, variable_var, ASRUtils::EXPR(func_call_asr), nullptr);
1311+
return make_dummy_assignment(ASRUtils::EXPR(func_call_asr));
13071312
} else {
13081313
return func_call_asr;
13091314
}
@@ -4724,11 +4729,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
47244729
// Visit the statement
47254730
this->visit_stmt(*m_body[i]);
47264731
if (tmp != nullptr) {
4732+
if (ASR::is_a<ASR::expr_t>(*tmp)) {
4733+
tmp = make_dummy_assignment(ASRUtils::EXPR(tmp));
4734+
}
47274735
ASR::stmt_t* tmp_stmt = ASRUtils::STMT(tmp);
47284736
body.push_back(al, tmp_stmt);
47294737
} else if (!tmp_vec.empty()) {
47304738
for (auto t: tmp_vec) {
47314739
if (t != nullptr) {
4740+
if (ASR::is_a<ASR::expr_t>(*t)) {
4741+
t = make_dummy_assignment(ASRUtils::EXPR(t));
4742+
}
47324743
ASR::stmt_t* tmp_stmt = ASRUtils::STMT(t);
47334744
body.push_back(al, tmp_stmt);
47344745
}

0 commit comments

Comments
 (0)