Skip to content

Commit 09fd597

Browse files
committed
Add attribute real and imag for complex
1 parent 68475d8 commit 09fd597

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,49 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
10811081
}
10821082
}
10831083

1084+
void visit_Attribute(const AST::Attribute_t &x) {
1085+
if (AST::is_a<AST::Name_t>(*x.m_value)) {
1086+
std::string value = AST::down_cast<AST::Name_t>(x.m_value)->m_id;
1087+
ASR::symbol_t *t = current_scope->scope[value];
1088+
if (!t) {
1089+
throw SemanticError("'" + value + "' is not defined in the scope",
1090+
x.base.base.loc);
1091+
}
1092+
if (ASR::is_a<ASR::Variable_t>(*t)) {
1093+
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t);
1094+
LFORTRAN_ASSERT(var->m_value);
1095+
if (ASR::is_a<ASR::ConstantComplex_t>(*var->m_value)) {
1096+
std::string attr = x.m_attr;
1097+
if (attr == "imag") {
1098+
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_im;
1099+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1100+
8, nullptr, 0));
1101+
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1102+
} else if (attr == "real") {
1103+
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_im;
1104+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1105+
8, nullptr, 0));
1106+
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1107+
} else {
1108+
throw SemanticError("'" + attr + "' is not implemented for Complex type",
1109+
x.base.base.loc);
1110+
}
1111+
1112+
} else {
1113+
throw SemanticError("Only Complex type supported for now in Attribute",
1114+
x.base.base.loc);
1115+
}
1116+
} else {
1117+
throw SemanticError("Only Variable type is supported for now in Attribute",
1118+
x.base.base.loc);
1119+
}
1120+
1121+
} else {
1122+
throw SemanticError("Only Name is supported for now in Attribute",
1123+
x.base.base.loc);
1124+
}
1125+
}
1126+
10841127
void visit_Assert(const AST::Assert_t &x) {
10851128
this->visit_expr(*x.m_test);
10861129
ASR::expr_t *test = ASRUtils::EXPR(tmp);

0 commit comments

Comments
 (0)