Skip to content

Commit 8172542

Browse files
committed
fix real and imag for complex types
1 parent edd5b87 commit 8172542

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,19 +1819,26 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
18191819
}
18201820
if (ASR::is_a<ASR::Variable_t>(*t)) {
18211821
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t);
1822-
LFORTRAN_ASSERT(var->m_value);
1823-
if (ASR::is_a<ASR::ConstantComplex_t>(*var->m_value)) {
1822+
// LFORTRAN_ASSERT(var->m_value);
1823+
if (ASRUtils::is_complex(*var->m_type)) {
18241824
std::string attr = x.m_attr;
18251825
if (attr == "imag") {
1826-
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_im;
1827-
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1828-
8, nullptr, 0));
1829-
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1826+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
1827+
ASR::symbol_t *fn_imag = resolve_intrinsic_function(x.base.base.loc, "imag");
1828+
Vec<ASR::expr_t*> args;
1829+
args.reserve(al, 1);
1830+
args.push_back(al, val);
1831+
make_call_helper(al, fn_imag, current_scope, args, "imag", x.base.base.loc);
1832+
return;
18301833
} else if (attr == "real") {
1831-
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_re;
1832-
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1833-
8, nullptr, 0));
1834-
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1834+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
1835+
int kind = ASRUtils::extract_kind_from_ttype_t(var->m_type);
1836+
ASR::ttype_t *dest_type = ASR::down_cast<ASR::ttype_t>(ASR::make_Real_t(al, x.base.base.loc,
1837+
kind, nullptr, 0));
1838+
tmp = (ASR::asr_t*)ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
1839+
al, val->base.loc, val, ASR::cast_kindType::ComplexToReal, dest_type,
1840+
nullptr));
1841+
return;
18351842
} else {
18361843
throw SemanticError("'" + attr + "' is not implemented for Complex type",
18371844
x.base.base.loc);

0 commit comments

Comments
 (0)