Skip to content

Commit ac6797b

Browse files
committed
fix real and imag for complex types
1 parent 1a8982f commit ac6797b

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
@@ -1847,19 +1847,26 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
18471847
}
18481848
if (ASR::is_a<ASR::Variable_t>(*t)) {
18491849
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t);
1850-
LFORTRAN_ASSERT(var->m_value);
1851-
if (ASR::is_a<ASR::ConstantComplex_t>(*var->m_value)) {
1850+
// LFORTRAN_ASSERT(var->m_value);
1851+
if (ASRUtils::is_complex(*var->m_type)) {
18521852
std::string attr = x.m_attr;
18531853
if (attr == "imag") {
1854-
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_im;
1855-
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1856-
8, nullptr, 0));
1857-
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1854+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
1855+
ASR::symbol_t *fn_imag = resolve_intrinsic_function(x.base.base.loc, "imag");
1856+
Vec<ASR::expr_t*> args;
1857+
args.reserve(al, 1);
1858+
args.push_back(al, val);
1859+
make_call_helper(al, fn_imag, current_scope, args, "imag", x.base.base.loc);
1860+
return;
18581861
} else if (attr == "real") {
1859-
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_re;
1860-
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1861-
8, nullptr, 0));
1862-
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1862+
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, x.base.base.loc, t));
1863+
int kind = ASRUtils::extract_kind_from_ttype_t(var->m_type);
1864+
ASR::ttype_t *dest_type = ASR::down_cast<ASR::ttype_t>(ASR::make_Real_t(al, x.base.base.loc,
1865+
kind, nullptr, 0));
1866+
tmp = (ASR::asr_t*)ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
1867+
al, val->base.loc, val, ASR::cast_kindType::ComplexToReal, dest_type,
1868+
nullptr));
1869+
return;
18631870
} else {
18641871
throw SemanticError("'" + attr + "' is not implemented for Complex type",
18651872
x.base.base.loc);

0 commit comments

Comments
 (0)