@@ -44,6 +44,7 @@ struct PythonIntrinsicProcedures {
44
44
{" hex" , {m_builtin, &eval_hex}},
45
45
{" oct" , {m_builtin, &eval_oct}},
46
46
{" complex" , {m_builtin, &eval_complex}},
47
+ {" imag" , {m_builtin, &eval_imag}},
47
48
{" divmod" , {m_builtin, &eval_divmod}},
48
49
};
49
50
}
@@ -494,6 +495,24 @@ struct PythonIntrinsicProcedures {
494
495
return ASR::down_cast<ASR::expr_t >(make_ConstantComplex_t (al, loc, c1, c2, type));
495
496
}
496
497
498
+ static ASR::expr_t *eval_imag (Allocator &al, const Location &loc,
499
+ Vec<ASR::expr_t *> &args
500
+ ) {
501
+ LFORTRAN_ASSERT (ASRUtils::all_args_evaluated (args));
502
+ if (args.size () != 1 ) {
503
+ throw SemanticError (" Intrinsic imag function accepts exactly 1 argument" , loc);
504
+ }
505
+ ASR::expr_t * imag_arg = args[0 ];
506
+ ASR::ttype_t * t = ASRUtils::expr_type (args[0 ]);
507
+ if (LFortran::ASR::is_a<LFortran::ASR::Complex_t>(*t)) {
508
+ double im = ASR::down_cast<ASR::ConstantComplex_t>(imag_arg)->m_im ;
509
+ double result = im;
510
+ return ASR::down_cast<ASR::expr_t >(ASR::make_ConstantReal_t (al, loc, result, t));
511
+ } else {
512
+ throw SemanticError (" Argument of the aimag() function must be Complex" , loc);
513
+ }
514
+ }
515
+
497
516
static ASR::expr_t *eval_divmod (Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
498
517
LFORTRAN_ASSERT (ASRUtils::all_args_evaluated (args));
499
518
if (args.size () != 2 ) {
0 commit comments