Skip to content

Improve binop of complex #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,13 +1028,38 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
al, right->base.loc, right, ASR::cast_kindType::RealToReal,
left_type, nullptr));
}
} else if (ASRUtils::is_complex(*left_type) && ASRUtils::is_complex(*right_type)) {
bool is_l64 = ASR::down_cast<ASR::Complex_t>(left_type)->m_kind == 8;
bool is_r64 = ASR::down_cast<ASR::Complex_t>(right_type)->m_kind == 8;
if ((is_assign && (is_l64 != is_r64)) || (is_l64 && !is_r64)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::ComplexToComplex,
left_type, nullptr));
}
} else if (!is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal,
left_type, nullptr));
} else if (is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) {
throw SemanticError("Assigning integer to float is not supported",
right->base.loc);
} else if (is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) {
throw SemanticError("Assigning non-complex to complex is not supported",
right->base.loc);
} else if (!is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) {
if (ASRUtils::is_real(*right_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::RealToComplex,
left_type, nullptr));
} else if (ASRUtils::is_integer(*right_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex,
left_type, nullptr));
} else {
std::string rtype = ASRUtils::type_to_str(right_type);
throw SemanticError("Casting " + rtype + " to complex is not Implemented",
right->base.loc);
}
}
return right;
}
Expand Down Expand Up @@ -1406,8 +1431,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
value));
}
}
} else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type)) &&
(ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type)) ){
} else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) ||
ASRUtils::is_complex(*left_type)) &&
(ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) ||
ASRUtils::is_complex(*right_type))) {
left = implicitcast_helper(ASRUtils::expr_type(right), left);
right = implicitcast_helper(ASRUtils::expr_type(left), right);
dest_type = ASRUtils::expr_type(left);
Expand Down Expand Up @@ -2326,7 +2353,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
if (ASR::is_a<ASR::ExternalSymbol_t>(*stemp)) {
local_sym = std::string(p->m_name) + "@" + local_sym;
}

SymbolTable *symtab = current_scope;
while (symtab->parent != nullptr && symtab->scope.find(local_sym) == symtab->scope.end()) {
symtab = symtab->parent;
Expand Down
11 changes: 11 additions & 0 deletions tests/complex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ def test_complex():
c = complex(1, 2) ** complex(3.34534, 4.8678678)
c = complex(1, 2) * complex(3, 4)
c = complex(4, 5) - complex(3, 4)


def test():
x: c64
y: c64
z: c32
x = 2 + 3j
y = 5 + 5j
z = x + y
z = x - y
z = 2 * x
4 changes: 2 additions & 2 deletions tests/reference/asr-complex1-f26c460.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-complex1-f26c460",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/complex1.py",
"infile_hash": "e5e897465f4a0e1e59bcde88e49ce7adb1ddb84283450b631d5479f7",
"infile_hash": "2a5092ec83a51155b432548f3571c56d4348f36976c5c079a0e5daa5",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-complex1-f26c460.stdout",
"stdout_hash": "2edcd8a8c909395f649fcd1c014899f82c53c13ed4695dfb5080ce4d",
"stdout_hash": "ad6d981d374283dd344f860604121d3777ad15fd84cf52982d400ef4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.stdout
Original file line number Diff line number Diff line change
@@ -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.)}) [])
(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.)}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-expr10-efcbb1b.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr10-efcbb1b.stdout",
"stdout_hash": "6c6438b68d228d0dbe8d114c794ebc37ac9934a9f572924cfb75f2c1",
"stdout_hash": "34c41e698593e4c015341a7e8023a4e2353dd851162f6e110343a015",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading