Skip to content

Commit 5f7f9f2

Browse files
committed
Add attribute real and imag for complex
1 parent ed57052 commit 5f7f9f2

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
@@ -1094,6 +1094,49 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
10941094
}
10951095
}
10961096

1097+
void visit_Attribute(const AST::Attribute_t &x) {
1098+
if (AST::is_a<AST::Name_t>(*x.m_value)) {
1099+
std::string value = AST::down_cast<AST::Name_t>(x.m_value)->m_id;
1100+
ASR::symbol_t *t = current_scope->scope[value];
1101+
if (!t) {
1102+
throw SemanticError("'" + value + "' is not defined in the scope",
1103+
x.base.base.loc);
1104+
}
1105+
if (ASR::is_a<ASR::Variable_t>(*t)) {
1106+
ASR::Variable_t *var = ASR::down_cast<ASR::Variable_t>(t);
1107+
LFORTRAN_ASSERT(var->m_value);
1108+
if (ASR::is_a<ASR::ConstantComplex_t>(*var->m_value)) {
1109+
std::string attr = x.m_attr;
1110+
if (attr == "imag") {
1111+
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_im;
1112+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1113+
8, nullptr, 0));
1114+
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1115+
} else if (attr == "real") {
1116+
double val = ASR::down_cast<ASR::ConstantComplex_t>(var->m_value)->m_im;
1117+
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc,
1118+
8, nullptr, 0));
1119+
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type);
1120+
} else {
1121+
throw SemanticError("'" + attr + "' is not implemented for Complex type",
1122+
x.base.base.loc);
1123+
}
1124+
1125+
} else {
1126+
throw SemanticError("Only Complex type supported for now in Attribute",
1127+
x.base.base.loc);
1128+
}
1129+
} else {
1130+
throw SemanticError("Only Variable type is supported for now in Attribute",
1131+
x.base.base.loc);
1132+
}
1133+
1134+
} else {
1135+
throw SemanticError("Only Name is supported for now in Attribute",
1136+
x.base.base.loc);
1137+
}
1138+
}
1139+
10971140
void visit_Assert(const AST::Assert_t &x) {
10981141
this->visit_expr(*x.m_test);
10991142
ASR::expr_t *test = ASRUtils::EXPR(tmp);

0 commit comments

Comments
 (0)