@@ -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
}
@@ -503,6 +504,24 @@ struct PythonIntrinsicProcedures {
503
504
return ASR::down_cast<ASR::expr_t >(make_ConstantComplex_t (al, loc, c1, c2, type));
504
505
}
505
506
507
+ static ASR::expr_t *eval_imag (Allocator &al, const Location &loc,
508
+ Vec<ASR::expr_t *> &args
509
+ ) {
510
+ LFORTRAN_ASSERT (ASRUtils::all_args_evaluated (args));
511
+ if (args.size () != 1 ) {
512
+ throw SemanticError (" Intrinsic imag function accepts exactly 1 argument" , loc);
513
+ }
514
+ ASR::expr_t * imag_arg = args[0 ];
515
+ ASR::ttype_t * t = ASRUtils::expr_type (args[0 ]);
516
+ if (LFortran::ASR::is_a<LFortran::ASR::Complex_t>(*t)) {
517
+ double im = ASR::down_cast<ASR::ConstantComplex_t>(imag_arg)->m_im ;
518
+ double result = im;
519
+ return ASR::down_cast<ASR::expr_t >(ASR::make_ConstantReal_t (al, loc, result, t));
520
+ } else {
521
+ throw SemanticError (" Argument of the aimag() function must be Complex" , loc);
522
+ }
523
+ }
524
+
506
525
static ASR::expr_t *eval_divmod (Allocator &al, const Location &loc, Vec<ASR::expr_t *> &args) {
507
526
LFORTRAN_ASSERT (ASRUtils::all_args_evaluated (args));
508
527
if (args.size () != 2 ) {
0 commit comments