Skip to content

Commit 08472bc

Browse files
committed
ASR: Support comparison for arrays
1 parent 5c49226 commit 08472bc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6157,6 +6157,37 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
61576157
if( ASR::is_a<ASR::Enum_t>(*dest_type) || ASR::is_a<ASR::Const_t>(*dest_type) ) {
61586158
dest_type = ASRUtils::get_contained_type(dest_type);
61596159
}
6160+
6161+
if (ASRUtils::is_array(dest_type)) {
6162+
ASR::dimension_t* m_dims = nullptr;
6163+
int n_dims = ASRUtils::extract_dimensions_from_ttype(dest_type, m_dims);
6164+
int array_size = ASRUtils::get_fixed_size_of_array(m_dims, n_dims);
6165+
if (array_size == -1) {
6166+
throw SemanticError("The truth value of an array is ambiguous. Use a.any() or a.all()", x.base.base.loc);
6167+
} else if (array_size != 1) {
6168+
throw SemanticError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", x.base.base.loc);
6169+
} else {
6170+
Vec<ASR::array_index_t> argsL, argsR;
6171+
argsL.reserve(al, 1);
6172+
argsR.reserve(al, 1);
6173+
for (int i = 0; i < n_dims; i++) {
6174+
ASR::array_index_t aiL, aiR;
6175+
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4));
6176+
ASR::expr_t* const_zero = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, 0, int_type));
6177+
aiL.m_right = aiR.m_right = const_zero;
6178+
aiL.m_left = aiR.m_left = nullptr;
6179+
aiL.m_step = aiR.m_step = nullptr;
6180+
aiL.loc = left->base.loc;
6181+
aiR.loc = right->base.loc;
6182+
argsL.push_back(al, aiL);
6183+
argsR.push_back(al, aiR);
6184+
}
6185+
dest_type = ASRUtils::type_get_past_array(dest_type);
6186+
left = ASRUtils::EXPR(make_ArrayItem_t(al, left->base.loc, left, argsL.p, argsL.n, dest_type, ASR::arraystorageType::RowMajor, nullptr));
6187+
right = ASRUtils::EXPR(make_ArrayItem_t(al, right->base.loc, right, argsR.p, argsR.n, dest_type, ASR::arraystorageType::RowMajor, nullptr));
6188+
}
6189+
}
6190+
61606191
if (ASRUtils::is_integer(*dest_type)) {
61616192
if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
61626193
int64_t left_value = -1;

0 commit comments

Comments
 (0)