Skip to content

Commit f55ab69

Browse files
authored
Merge pull request #252 from Smit-create/pr232_1
Improve binop of complex
2 parents da4a664 + c197142 commit f55ab69

File tree

8 files changed

+49
-11
lines changed

8 files changed

+49
-11
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,13 +1028,38 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
10281028
al, right->base.loc, right, ASR::cast_kindType::RealToReal,
10291029
left_type, nullptr));
10301030
}
1031+
} else if (ASRUtils::is_complex(*left_type) && ASRUtils::is_complex(*right_type)) {
1032+
bool is_l64 = ASR::down_cast<ASR::Complex_t>(left_type)->m_kind == 8;
1033+
bool is_r64 = ASR::down_cast<ASR::Complex_t>(right_type)->m_kind == 8;
1034+
if ((is_assign && (is_l64 != is_r64)) || (is_l64 && !is_r64)) {
1035+
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
1036+
al, right->base.loc, right, ASR::cast_kindType::ComplexToComplex,
1037+
left_type, nullptr));
1038+
}
10311039
} else if (!is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) {
10321040
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
10331041
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal,
10341042
left_type, nullptr));
10351043
} else if (is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) {
10361044
throw SemanticError("Assigning integer to float is not supported",
10371045
right->base.loc);
1046+
} else if (is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) {
1047+
throw SemanticError("Assigning non-complex to complex is not supported",
1048+
right->base.loc);
1049+
} else if (!is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) {
1050+
if (ASRUtils::is_real(*right_type)) {
1051+
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
1052+
al, right->base.loc, right, ASR::cast_kindType::RealToComplex,
1053+
left_type, nullptr));
1054+
} else if (ASRUtils::is_integer(*right_type)) {
1055+
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
1056+
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex,
1057+
left_type, nullptr));
1058+
} else {
1059+
std::string rtype = ASRUtils::type_to_str(right_type);
1060+
throw SemanticError("Casting " + rtype + " to complex is not Implemented",
1061+
right->base.loc);
1062+
}
10381063
}
10391064
return right;
10401065
}
@@ -1406,8 +1431,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
14061431
value));
14071432
}
14081433
}
1409-
} else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type)) &&
1410-
(ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type)) ){
1434+
} else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) ||
1435+
ASRUtils::is_complex(*left_type)) &&
1436+
(ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) ||
1437+
ASRUtils::is_complex(*right_type))) {
14111438
left = implicitcast_helper(ASRUtils::expr_type(right), left);
14121439
right = implicitcast_helper(ASRUtils::expr_type(left), right);
14131440
dest_type = ASRUtils::expr_type(left);
@@ -2326,7 +2353,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
23262353
if (ASR::is_a<ASR::ExternalSymbol_t>(*stemp)) {
23272354
local_sym = std::string(p->m_name) + "@" + local_sym;
23282355
}
2329-
2356+
23302357
SymbolTable *symtab = current_scope;
23312358
while (symtab->parent != nullptr && symtab->scope.find(local_sym) == symtab->scope.end()) {
23322359
symtab = symtab->parent;

tests/complex1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ def test_complex():
2323
c = complex(1, 2) ** complex(3.34534, 4.8678678)
2424
c = complex(1, 2) * complex(3, 4)
2525
c = complex(4, 5) - complex(3, 4)
26+
27+
28+
def test():
29+
x: c64
30+
y: c64
31+
z: c32
32+
x = 2 + 3j
33+
y = 5 + 5j
34+
z = x + y
35+
z = x - y
36+
z = 2 * x

tests/reference/asr-complex1-f26c460.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-complex1-f26c460",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/complex1.py",
5-
"infile_hash": "e5e897465f4a0e1e59bcde88e49ce7adb1ddb84283450b631d5479f7",
5+
"infile_hash": "2a5092ec83a51155b432548f3571c56d4348f36976c5c079a0e5daa5",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-complex1-f26c460.stdout",
9-
"stdout_hash": "2edcd8a8c909395f649fcd1c014899f82c53c13ed4695dfb5080ce4d",
9+
"stdout_hash": "ad6d981d374283dd344f860604121d3777ad15fd84cf52982d400ef4",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 27 {}) main_program [] []), test_complex: (Subroutine (SymbolTable 2 {b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), c1: (Variable 2 c1 Local () () Default (Complex 4 []) Source Public Required .false.), c2: (Variable 2 c2 Local () () Default (Complex 4 []) Source Public Required .false.), c3: (Variable 2 c3 Local () () Default (Complex 8 []) Source Public Required .false.), complex: (ExternalSymbol 2 complex 4 complex lpython_builtin [] complex Private)}) test_complex [] [(= (Var 2 c) (FunctionCall 2 complex () [] [] (Complex 8 []) (ConstantComplex 0.000000 0.000000 (Complex 8 [])) ()) ()) (= (Var 2 c) (FunctionCall 2 complex () [(ConstantReal 3.400000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 3.400000 0.000000 (Complex 8 [])) ()) ()) (= (Var 2 c) (FunctionCall 2 complex () [(ConstantReal 5.000000 (Real 8 [])) (ConstantReal 4.300000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 5.000000 4.300000 (Complex 8 [])) ()) ()) (= (Var 2 c) (FunctionCall 2 complex () [(ConstantInteger 1 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 1.000000 0.000000 (Complex 8 [])) ()) ()) (= (Var 2 c1) (FunctionCall 2 complex () [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) ()) (= (Var 2 c2) (FunctionCall 2 complex () [(ConstantInteger 2 (Integer 4 [])) (ConstantReal 4.500000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 2.000000 4.500000 (Complex 8 [])) ()) ()) (= (Var 2 c3) (FunctionCall 2 complex () [(ConstantReal 3.000000 (Real 8 [])) (ConstantReal 4.000000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) ()) (= (Var 2 b) (Compare (Var 2 c1) NotEq (Var 2 c2) (Logical 4 []) () ()) ()) (= (Var 2 b) (Compare (Var 2 c1) Eq (Var 2 c3) (Logical 4 []) () ()) ()) (= (Var 2 c) (BinOp (Var 2 c1) Add (Var 2 c2) (Complex 4 []) () ()) ()) (= (Var 2 c) (BinOp (Var 2 c2) Sub (Var 2 c1) (Complex 4 []) () ()) ()) (= (Var 2 c) (BinOp (Var 2 c1) Mul (Var 2 c2) (Complex 4 []) () ()) ()) (= (Var 2 c) (BinOp (FunctionCall 2 complex () [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 1.000000 2.000000 (Complex 8 [])) ()) Pow (FunctionCall 2 complex () [(ConstantReal 3.345340 (Real 8 [])) (ConstantReal 4.867868 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 3.345340 4.867868 (Complex 8 [])) ()) (Complex 8 []) (ConstantComplex 0.015553 0.065561 (Complex 8 [])) ()) ()) (= (Var 2 c) (BinOp (FunctionCall 2 complex () [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 1.000000 2.000000 (Complex 8 [])) ()) Mul (FunctionCall 2 complex () [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) (Complex 8 []) (ConstantComplex -5.000000 10.000000 (Complex 8 [])) ()) ()) (= (Var 2 c) (BinOp (FunctionCall 2 complex () [(ConstantInteger 4 (Integer 4 [])) (ConstantInteger 5 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 4.000000 5.000000 (Complex 8 [])) ()) Sub (FunctionCall 2 complex () [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) (Complex 8 []) (ConstantComplex 1.000000 1.000000 (Complex 8 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 28 {}) main_program [] []), test: (Subroutine (SymbolTable 3 {x: (Variable 3 x Local () () Default (Complex 8 []) Source Public Required .false.), y: (Variable 3 y Local () () Default (Complex 8 []) Source Public Required .false.), z: (Variable 3 z Local () () Default (Complex 4 []) Source Public Required .false.)}) test [] [(= (Var 3 x) (BinOp (ImplicitCast (ConstantInteger 2 (Integer 4 [])) IntegerToComplex (Complex 8 []) ()) Add (ConstantComplex 0.000000 3.000000 (Complex 8 [])) (Complex 8 []) () ()) ()) (= (Var 3 y) (BinOp (ImplicitCast (ConstantInteger 5 (Integer 4 [])) IntegerToComplex (Complex 8 []) ()) Add (ConstantComplex 0.000000 5.000000 (Complex 8 [])) (Complex 8 []) () ()) ()) (= (Var 3 z) (ImplicitCast (BinOp (Var 3 x) Add (Var 3 y) (Complex 8 []) () ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 3 z) (ImplicitCast (BinOp (Var 3 x) Sub (Var 3 y) (Complex 8 []) () ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 3 z) (ImplicitCast (BinOp (ImplicitCast (ConstantInteger 2 (Integer 4 [])) IntegerToComplex (Complex 8 []) ()) Mul (Var 3 x) (Complex 8 []) () ()) ComplexToComplex (Complex 4 []) ()) ())] Source Public Implementation () .false. .false.), test_complex: (Subroutine (SymbolTable 2 {b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), c1: (Variable 2 c1 Local () () Default (Complex 4 []) Source Public Required .false.), c2: (Variable 2 c2 Local () () Default (Complex 4 []) Source Public Required .false.), c3: (Variable 2 c3 Local () () Default (Complex 8 []) Source Public Required .false.), complex: (ExternalSymbol 2 complex 5 complex lpython_builtin [] complex Private)}) test_complex [] [(= (Var 2 c) (ImplicitCast (FunctionCall 2 complex () [] [] (Complex 8 []) (ConstantComplex 0.000000 0.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (FunctionCall 2 complex () [(ConstantReal 3.400000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 3.400000 0.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (FunctionCall 2 complex () [(ConstantReal 5.000000 (Real 8 [])) (ConstantReal 4.300000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 5.000000 4.300000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (FunctionCall 2 complex () [(ConstantInteger 1 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 1.000000 0.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c1) (ImplicitCast (FunctionCall 2 complex () [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c2) (ImplicitCast (FunctionCall 2 complex () [(ConstantInteger 2 (Integer 4 [])) (ConstantReal 4.500000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 2.000000 4.500000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c3) (FunctionCall 2 complex () [(ConstantReal 3.000000 (Real 8 [])) (ConstantReal 4.000000 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) ()) (= (Var 2 b) (Compare (Var 2 c1) NotEq (Var 2 c2) (Logical 4 []) () ()) ()) (= (Var 2 b) (Compare (ImplicitCast (Var 2 c1) ComplexToComplex (Complex 8 []) ()) Eq (Var 2 c3) (Logical 4 []) () ()) ()) (= (Var 2 c) (BinOp (Var 2 c1) Add (Var 2 c2) (Complex 4 []) () ()) ()) (= (Var 2 c) (BinOp (Var 2 c2) Sub (Var 2 c1) (Complex 4 []) () ()) ()) (= (Var 2 c) (BinOp (Var 2 c1) Mul (Var 2 c2) (Complex 4 []) () ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (FunctionCall 2 complex () [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 1.000000 2.000000 (Complex 8 [])) ()) Pow (FunctionCall 2 complex () [(ConstantReal 3.345340 (Real 8 [])) (ConstantReal 4.867868 (Real 8 []))] [] (Complex 8 []) (ConstantComplex 3.345340 4.867868 (Complex 8 [])) ()) (Complex 8 []) (ConstantComplex 0.015553 0.065561 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (FunctionCall 2 complex () [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 1.000000 2.000000 (Complex 8 [])) ()) Mul (FunctionCall 2 complex () [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) (Complex 8 []) (ConstantComplex -5.000000 10.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (FunctionCall 2 complex () [(ConstantInteger 4 (Integer 4 [])) (ConstantInteger 5 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 4.000000 5.000000 (Complex 8 [])) ()) Sub (FunctionCall 2 complex () [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] [] (Complex 8 []) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) (Complex 8 []) (ConstantComplex 1.000000 1.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ())] Source Public Implementation () .false. .false.)}) [])

tests/reference/asr-expr10-efcbb1b.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-expr10-efcbb1b.stdout",
9-
"stdout_hash": "6c6438b68d228d0dbe8d114c794ebc37ac9934a9f572924cfb75f2c1",
9+
"stdout_hash": "34c41e698593e4c015341a7e8023a4e2353dd851162f6e110343a015",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)