From c2db8020d5f1e4faac10304bc42dc47ee619a576 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 19:41:55 +0530 Subject: [PATCH 1/5] implement binop for complex --- src/lpython/semantics/python_ast_to_asr.cpp | 37 ++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 125a85c78d..17d90c4866 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1014,6 +1014,21 @@ class BodyVisitor : public CommonVisitor { 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(left_type)->m_kind == 8; + bool is_r64 = ASR::down_cast(right_type)->m_kind == 8; + if (is_assign) { + if (is_l64 != is_r64) { + // here we need to cast to left type strictly + return ASR::down_cast(ASR::make_ImplicitCast_t( + al, right->base.loc, right, ASR::cast_kindType::ComplexToComplex, + left_type, nullptr)); + } + } else if (is_l64 && !is_r64) { + return ASR::down_cast(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::make_ImplicitCast_t( al, right->base.loc, right, ASR::cast_kindType::IntegerToReal, @@ -1021,6 +1036,20 @@ class BodyVisitor : public CommonVisitor { } 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 (ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) { + if (ASRUtils::is_real(*right_type)) { + return ASR::down_cast(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::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; } @@ -1392,8 +1421,10 @@ class BodyVisitor : public CommonVisitor { 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); @@ -1467,8 +1498,6 @@ class BodyVisitor : public CommonVisitor { value); return; - } else if (ASRUtils::is_complex(*left_type) && ASRUtils::is_complex(*right_type)) { - dest_type = left_type; } else if (ASRUtils::is_logical(*left_type) && ASRUtils::is_logical(*right_type)) { dest_type = left_type; } else { From 3ab048e7c798594518db0fdccde4c751302ed48e Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 14 Mar 2022 19:56:51 +0530 Subject: [PATCH 2/5] update tests --- tests/reference/asr-complex1-f26c460.json | 2 +- tests/reference/asr-complex1-f26c460.stdout | 2 +- tests/reference/asr-expr10-efcbb1b.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/reference/asr-complex1-f26c460.json b/tests/reference/asr-complex1-f26c460.json index 32b998bfed..b8809bb34e 100644 --- a/tests/reference/asr-complex1-f26c460.json +++ b/tests/reference/asr-complex1-f26c460.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-complex1-f26c460.stdout", - "stdout_hash": "3d30525175033241e93db499e684dd2744b4272da6e49756d4a9aa46", + "stdout_hash": "2d995401e7d420c8638a5630ab3102a686038ba296dbd079c4d5a5c6", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-complex1-f26c460.stdout b/tests/reference/asr-complex1-f26c460.stdout index c286e91910..34d045feff 100644 --- a/tests/reference/asr-complex1-f26c460.stdout +++ b/tests/reference/asr-complex1-f26c460.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) 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.)}) test_complex [] [(= (Var 2 c) (ConstantComplex 0.000000 0.000000 (Complex 8 [])) ()) (= (Var 2 c) (ConstantComplex 3.400000 0.000000 (Complex 8 [])) ()) (= (Var 2 c) (ConstantComplex 5.000000 4.300000 (Complex 8 [])) ()) (= (Var 2 c) (ConstantComplex 1.000000 0.000000 (Complex 8 [])) ()) (= (Var 2 c1) (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ()) (= (Var 2 c2) (ConstantComplex 2.000000 4.500000 (Complex 8 [])) ()) (= (Var 2 c3) (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 (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Pow (ConstantComplex 3.345340 4.867868 (Complex 8 [])) (Complex 8 []) (ConstantComplex 0.015553 0.065561 (Complex 8 [])) ()) ()) (= (Var 2 c) (BinOp (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Mul (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex -5.000000 10.000000 (Complex 8 [])) ()) ()) (= (Var 2 c) (BinOp (ConstantComplex 4.000000 5.000000 (Complex 8 [])) Sub (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex 1.000000 1.000000 (Complex 8 [])) ()) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) 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.)}) test_complex [] [(= (Var 2 c) (ImplicitCast (ConstantComplex 0.000000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 3.400000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 5.000000 4.300000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 1.000000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c1) (ImplicitCast (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c2) (ImplicitCast (ConstantComplex 2.000000 4.500000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c3) (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 (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Pow (ConstantComplex 3.345340 4.867868 (Complex 8 [])) (Complex 8 []) (ConstantComplex 0.015553 0.065561 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Mul (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex -5.000000 10.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (ConstantComplex 4.000000 5.000000 (Complex 8 [])) Sub (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex 1.000000 1.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/asr-expr10-efcbb1b.json b/tests/reference/asr-expr10-efcbb1b.json index 39f5079e29..f848e252a3 100644 --- a/tests/reference/asr-expr10-efcbb1b.json +++ b/tests/reference/asr-expr10-efcbb1b.json @@ -10,4 +10,4 @@ "stderr": null, "stderr_hash": null, "returncode": 0 -} \ No newline at end of file +} From ed57052d56217d6bf5148375484b2bd229ae5240 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 15 Mar 2022 19:29:16 +0530 Subject: [PATCH 3/5] add test for complex --- tests/complex1.py | 11 +++++++++++ tests/reference/asr-complex1-f26c460.json | 4 ++-- tests/reference/asr-complex1-f26c460.stdout | 2 +- tests/reference/ast-complex1-800b4bb.json | 4 ++-- tests/reference/ast-complex1-800b4bb.stdout | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/complex1.py b/tests/complex1.py index 52db281856..b7af22331d 100644 --- a/tests/complex1.py +++ b/tests/complex1.py @@ -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: c64 + x = 2 + 3j + y = 5 + 5j + z = x + y + z = x - y + z = 2 * x diff --git a/tests/reference/asr-complex1-f26c460.json b/tests/reference/asr-complex1-f26c460.json index b8809bb34e..b8af3fd060 100644 --- a/tests/reference/asr-complex1-f26c460.json +++ b/tests/reference/asr-complex1-f26c460.json @@ -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": "651cffa820748667c774f75291eed77bb0706236de41d6f25a779106", "outfile": null, "outfile_hash": null, "stdout": "asr-complex1-f26c460.stdout", - "stdout_hash": "2d995401e7d420c8638a5630ab3102a686038ba296dbd079c4d5a5c6", + "stdout_hash": "bd2408b259465bb2d8afecec3dae041c24f3e1570dd6f32c082ea8e6", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-complex1-f26c460.stdout b/tests/reference/asr-complex1-f26c460.stdout index 34d045feff..55bac09d82 100644 --- a/tests/reference/asr-complex1-f26c460.stdout +++ b/tests/reference/asr-complex1-f26c460.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) 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.)}) test_complex [] [(= (Var 2 c) (ImplicitCast (ConstantComplex 0.000000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 3.400000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 5.000000 4.300000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 1.000000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c1) (ImplicitCast (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c2) (ImplicitCast (ConstantComplex 2.000000 4.500000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c3) (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 (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Pow (ConstantComplex 3.345340 4.867868 (Complex 8 [])) (Complex 8 []) (ConstantComplex 0.015553 0.065561 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Mul (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex -5.000000 10.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (ConstantComplex 4.000000 5.000000 (Complex 8 [])) Sub (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex 1.000000 1.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 4 {}) 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 8 []) 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) (BinOp (Var 3 x) Add (Var 3 y) (Complex 8 []) () ()) ()) (= (Var 3 z) (BinOp (Var 3 x) Sub (Var 3 y) (Complex 8 []) () ()) ()) (= (Var 3 z) (BinOp (ImplicitCast (ConstantInteger 2 (Integer 4 [])) IntegerToComplex (Complex 8 []) ()) Mul (Var 3 x) (Complex 8 []) () ()) ())] 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.)}) test_complex [] [(= (Var 2 c) (ImplicitCast (ConstantComplex 0.000000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 3.400000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 5.000000 4.300000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (ConstantComplex 1.000000 0.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c1) (ImplicitCast (ConstantComplex 3.000000 4.000000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c2) (ImplicitCast (ConstantComplex 2.000000 4.500000 (Complex 8 [])) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c3) (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 (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Pow (ConstantComplex 3.345340 4.867868 (Complex 8 [])) (Complex 8 []) (ConstantComplex 0.015553 0.065561 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (ConstantComplex 1.000000 2.000000 (Complex 8 [])) Mul (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex -5.000000 10.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (BinOp (ConstantComplex 4.000000 5.000000 (Complex 8 [])) Sub (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex 1.000000 1.000000 (Complex 8 [])) ()) ComplexToComplex (Complex 4 []) ()) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/ast-complex1-800b4bb.json b/tests/reference/ast-complex1-800b4bb.json index 75d13d83a3..a11d22c92f 100644 --- a/tests/reference/ast-complex1-800b4bb.json +++ b/tests/reference/ast-complex1-800b4bb.json @@ -2,11 +2,11 @@ "basename": "ast-complex1-800b4bb", "cmd": "lpython --show-ast --no-color {infile} -o {outfile}", "infile": "tests/complex1.py", - "infile_hash": "e5e897465f4a0e1e59bcde88e49ce7adb1ddb84283450b631d5479f7", + "infile_hash": "651cffa820748667c774f75291eed77bb0706236de41d6f25a779106", "outfile": null, "outfile_hash": null, "stdout": "ast-complex1-800b4bb.stdout", - "stdout_hash": "90fc1deef6ad16c1f2bb64550c84699e09c23fc39b34b28f97c143f4", + "stdout_hash": "10c37f72814e9e59a3c31481c43dd8c5ee41f4b0ca4d5c1ebf43ff1c", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/ast-complex1-800b4bb.stdout b/tests/reference/ast-complex1-800b4bb.stdout index 7cbc57ba58..d161423c33 100644 --- a/tests/reference/ast-complex1-800b4bb.stdout +++ b/tests/reference/ast-complex1-800b4bb.stdout @@ -1 +1 @@ -(Module [(FunctionDef test_complex ([] [] [] [] [] [] []) [(AnnAssign (Name c Store) (Name c32 Load) () 1) (AnnAssign (Name c1 Store) (Name c32 Load) () 1) (AnnAssign (Name c2 Store) (Name c32 Load) () 1) (AnnAssign (Name c3 Store) (Name c64 Load) () 1) (AnnAssign (Name b Store) (Name bool Load) () 1) (Assign [(Name c Store)] (Call (Name complex Load) [] []) ()) (Assign [(Name c Store)] (Call (Name complex Load) [(ConstantFloat 3.400000 ())] []) ()) (Assign [(Name c Store)] (Call (Name complex Load) [(ConstantFloat 5.000000 ()) (ConstantFloat 4.300000 ())] []) ()) (Assign [(Name c Store)] (Call (Name complex Load) [(ConstantInt 1 ())] []) ()) (Assign [(Name c1 Store)] (Call (Name complex Load) [(ConstantInt 3 ()) (ConstantInt 4 ())] []) ()) (Assign [(Name c2 Store)] (Call (Name complex Load) [(ConstantInt 2 ()) (ConstantFloat 4.500000 ())] []) ()) (Assign [(Name c3 Store)] (Call (Name complex Load) [(ConstantFloat 3.000000 ()) (ConstantFloat 4.000000 ())] []) ()) (Assign [(Name b Store)] (Compare (Name c1 Load) NotEq [(Name c2 Load)]) ()) (Assign [(Name b Store)] (Compare (Name c1 Load) Eq [(Name c3 Load)]) ()) (Assign [(Name c Store)] (BinOp (Name c1 Load) Add (Name c2 Load)) ()) (Assign [(Name c Store)] (BinOp (Name c2 Load) Sub (Name c1 Load)) ()) (Assign [(Name c Store)] (BinOp (Name c1 Load) Mult (Name c2 Load)) ()) (Assign [(Name c Store)] (BinOp (Call (Name complex Load) [(ConstantInt 1 ()) (ConstantInt 2 ())] []) Pow (Call (Name complex Load) [(ConstantFloat 3.345340 ()) (ConstantFloat 4.867868 ())] [])) ()) (Assign [(Name c Store)] (BinOp (Call (Name complex Load) [(ConstantInt 1 ()) (ConstantInt 2 ())] []) Mult (Call (Name complex Load) [(ConstantInt 3 ()) (ConstantInt 4 ())] [])) ()) (Assign [(Name c Store)] (BinOp (Call (Name complex Load) [(ConstantInt 4 ()) (ConstantInt 5 ())] []) Sub (Call (Name complex Load) [(ConstantInt 3 ()) (ConstantInt 4 ())] [])) ())] [] () ())] []) +(Module [(FunctionDef test_complex ([] [] [] [] [] [] []) [(AnnAssign (Name c Store) (Name c32 Load) () 1) (AnnAssign (Name c1 Store) (Name c32 Load) () 1) (AnnAssign (Name c2 Store) (Name c32 Load) () 1) (AnnAssign (Name c3 Store) (Name c64 Load) () 1) (AnnAssign (Name b Store) (Name bool Load) () 1) (Assign [(Name c Store)] (Call (Name complex Load) [] []) ()) (Assign [(Name c Store)] (Call (Name complex Load) [(ConstantFloat 3.400000 ())] []) ()) (Assign [(Name c Store)] (Call (Name complex Load) [(ConstantFloat 5.000000 ()) (ConstantFloat 4.300000 ())] []) ()) (Assign [(Name c Store)] (Call (Name complex Load) [(ConstantInt 1 ())] []) ()) (Assign [(Name c1 Store)] (Call (Name complex Load) [(ConstantInt 3 ()) (ConstantInt 4 ())] []) ()) (Assign [(Name c2 Store)] (Call (Name complex Load) [(ConstantInt 2 ()) (ConstantFloat 4.500000 ())] []) ()) (Assign [(Name c3 Store)] (Call (Name complex Load) [(ConstantFloat 3.000000 ()) (ConstantFloat 4.000000 ())] []) ()) (Assign [(Name b Store)] (Compare (Name c1 Load) NotEq [(Name c2 Load)]) ()) (Assign [(Name b Store)] (Compare (Name c1 Load) Eq [(Name c3 Load)]) ()) (Assign [(Name c Store)] (BinOp (Name c1 Load) Add (Name c2 Load)) ()) (Assign [(Name c Store)] (BinOp (Name c2 Load) Sub (Name c1 Load)) ()) (Assign [(Name c Store)] (BinOp (Name c1 Load) Mult (Name c2 Load)) ()) (Assign [(Name c Store)] (BinOp (Call (Name complex Load) [(ConstantInt 1 ()) (ConstantInt 2 ())] []) Pow (Call (Name complex Load) [(ConstantFloat 3.345340 ()) (ConstantFloat 4.867868 ())] [])) ()) (Assign [(Name c Store)] (BinOp (Call (Name complex Load) [(ConstantInt 1 ()) (ConstantInt 2 ())] []) Mult (Call (Name complex Load) [(ConstantInt 3 ()) (ConstantInt 4 ())] [])) ()) (Assign [(Name c Store)] (BinOp (Call (Name complex Load) [(ConstantInt 4 ()) (ConstantInt 5 ())] []) Sub (Call (Name complex Load) [(ConstantInt 3 ()) (ConstantInt 4 ())] [])) ())] [] () ()) (FunctionDef test ([] [] [] [] [] [] []) [(AnnAssign (Name x Store) (Name c64 Load) () 1) (AnnAssign (Name y Store) (Name c64 Load) () 1) (AnnAssign (Name z Store) (Name c64 Load) () 1) (Assign [(Name x Store)] (BinOp (ConstantInt 2 ()) Add (ConstantComplex 0.000000 3.000000 ())) ()) (Assign [(Name y Store)] (BinOp (ConstantInt 5 ()) Add (ConstantComplex 0.000000 5.000000 ())) ()) (Assign [(Name z Store)] (BinOp (Name x Load) Add (Name y Load)) ()) (Assign [(Name z Store)] (BinOp (Name x Load) Sub (Name y Load)) ()) (Assign [(Name z Store)] (BinOp (ConstantInt 2 ()) Mult (Name x Load)) ())] [] () ())] []) From 5f7f9f26ccd20573eaccec95712deb4caa28a349 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 15 Mar 2022 20:28:07 +0530 Subject: [PATCH 4/5] Add attribute real and imag for complex --- src/lpython/semantics/python_ast_to_asr.cpp | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 17d90c4866..97ddd502af 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1094,6 +1094,49 @@ class BodyVisitor : public CommonVisitor { } } + void visit_Attribute(const AST::Attribute_t &x) { + if (AST::is_a(*x.m_value)) { + std::string value = AST::down_cast(x.m_value)->m_id; + ASR::symbol_t *t = current_scope->scope[value]; + if (!t) { + throw SemanticError("'" + value + "' is not defined in the scope", + x.base.base.loc); + } + if (ASR::is_a(*t)) { + ASR::Variable_t *var = ASR::down_cast(t); + LFORTRAN_ASSERT(var->m_value); + if (ASR::is_a(*var->m_value)) { + std::string attr = x.m_attr; + if (attr == "imag") { + double val = ASR::down_cast(var->m_value)->m_im; + ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc, + 8, nullptr, 0)); + tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type); + } else if (attr == "real") { + double val = ASR::down_cast(var->m_value)->m_im; + ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Real_t(al, x.base.base.loc, + 8, nullptr, 0)); + tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, type); + } else { + throw SemanticError("'" + attr + "' is not implemented for Complex type", + x.base.base.loc); + } + + } else { + throw SemanticError("Only Complex type supported for now in Attribute", + x.base.base.loc); + } + } else { + throw SemanticError("Only Variable type is supported for now in Attribute", + x.base.base.loc); + } + + } else { + throw SemanticError("Only Name is supported for now in Attribute", + x.base.base.loc); + } + } + void visit_Assert(const AST::Assert_t &x) { this->visit_expr(*x.m_test); ASR::expr_t *test = ASRUtils::EXPR(tmp); From 3cfb7b511764f9a59cb64435d58010d40e39a389 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 15 Mar 2022 20:29:54 +0530 Subject: [PATCH 5/5] update test after rebasing --- tests/reference/asr-expr10-efcbb1b.json | 4 ++-- tests/reference/asr-expr10-efcbb1b.stdout | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/reference/asr-expr10-efcbb1b.json b/tests/reference/asr-expr10-efcbb1b.json index f848e252a3..5c072c34cd 100644 --- a/tests/reference/asr-expr10-efcbb1b.json +++ b/tests/reference/asr-expr10-efcbb1b.json @@ -6,8 +6,8 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-expr10-efcbb1b.stdout", - "stdout_hash": "35d921142ec8c2aa5bd5c9a9c43b4c172a0d9907a8b6e457849e3782", + "stdout_hash": "e763d27dad09a6d48e2bd1dce6a41eb9418c17b98e3d6f883d1fea54", "stderr": null, "stderr_hash": null, "returncode": 0 -} +} \ No newline at end of file diff --git a/tests/reference/asr-expr10-efcbb1b.stdout b/tests/reference/asr-expr10-efcbb1b.stdout index f4958dc293..7ac0e657f0 100644 --- a/tests/reference/asr-expr10-efcbb1b.stdout +++ b/tests/reference/asr-expr10-efcbb1b.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_UnaryOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Logical 1 []) Source Public Required .false.), b2: (Variable 2 b2 Local () () Default (Logical 1 []) Source Public Required .false.), b3: (Variable 2 b3 Local () () Default (Logical 1 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), f: (Variable 2 f Local () () Default (Real 4 []) Source Public Required .false.)}) test_UnaryOp [] [(= (Var 2 a) (UnaryOp UAdd (ConstantInteger 4 (Integer 4 [])) (Integer 4 []) (ConstantInteger 4 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp USub (ConstantInteger 500 (Integer 4 [])) (Integer 4 []) (ConstantInteger -500 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp Invert (ConstantInteger 5 (Integer 4 [])) (Integer 4 []) (ConstantInteger -6 (Integer 4 []))) ()) (= (Var 2 b) (UnaryOp Not (ConstantInteger 5 (Integer 4 [])) (Logical 4 []) (ConstantLogical .false. (Logical 4 []))) ()) (= (Var 2 b) (UnaryOp Not (UnaryOp USub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger -1 (Integer 4 []))) (Logical 4 []) (ConstantLogical .false. (Logical 4 []))) ()) (= (Var 2 b) (UnaryOp Not (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 []))) ()) (= (Var 2 f) (ImplicitCast (UnaryOp UAdd (ConstantReal 1.000000 (Real 8 [])) (Real 8 []) (ConstantReal 1.000000 (Real 8 []))) RealToReal (Real 4 []) ()) ()) (= (Var 2 f) (ImplicitCast (UnaryOp USub (ConstantReal 183745.534000 (Real 8 [])) (Real 8 []) (ConstantReal -183745.534000 (Real 8 []))) RealToReal (Real 4 []) ()) ()) (= (Var 2 b1) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 2 b2) (UnaryOp Not (ConstantLogical .false. (Logical 1 [])) (Logical 1 []) (ConstantLogical .true. (Logical 4 []))) ()) (= (Var 2 b3) (UnaryOp Not (Var 2 b2) (Logical 1 []) ()) ()) (= (Var 2 a) (UnaryOp UAdd (ConstantLogical .true. (Logical 1 [])) (Integer 4 []) (ConstantInteger 1 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp USub (ConstantLogical .false. (Logical 1 [])) (Integer 4 []) (ConstantInteger 0 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp Invert (ConstantLogical .true. (Logical 1 [])) (Integer 4 []) (ConstantInteger -2 (Integer 4 []))) ()) (= (Var 2 c) (UnaryOp UAdd (ConstantComplex 1.000000 2.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex 1.000000 2.000000 (Complex 8 []))) ()) (= (Var 2 c) (UnaryOp USub (ConstantComplex 3.000000 65.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex -3.000000 -65.000000 (Complex 8 []))) ()) (= (Var 2 b1) (UnaryOp Not (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Logical 4 []) (ConstantLogical .false. (Logical 4 []))) ()) (= (Var 2 b2) (UnaryOp Not (ConstantComplex 0.000000 0.000000 (Complex 8 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 []))) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_UnaryOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Logical 1 []) Source Public Required .false.), b2: (Variable 2 b2 Local () () Default (Logical 1 []) Source Public Required .false.), b3: (Variable 2 b3 Local () () Default (Logical 1 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), f: (Variable 2 f Local () () Default (Real 4 []) Source Public Required .false.)}) test_UnaryOp [] [(= (Var 2 a) (UnaryOp UAdd (ConstantInteger 4 (Integer 4 [])) (Integer 4 []) (ConstantInteger 4 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp USub (ConstantInteger 500 (Integer 4 [])) (Integer 4 []) (ConstantInteger -500 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp Invert (ConstantInteger 5 (Integer 4 [])) (Integer 4 []) (ConstantInteger -6 (Integer 4 []))) ()) (= (Var 2 b) (UnaryOp Not (ConstantInteger 5 (Integer 4 [])) (Integer 4 []) (ConstantLogical .false. (Logical 4 []))) ()) (= (Var 2 b) (UnaryOp Not (UnaryOp USub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger -1 (Integer 4 []))) (Integer 4 []) (ConstantLogical .false. (Logical 4 []))) ()) (= (Var 2 b) (UnaryOp Not (ConstantInteger 0 (Integer 4 [])) (Integer 4 []) (ConstantLogical .true. (Logical 4 []))) ()) (= (Var 2 f) (ImplicitCast (UnaryOp UAdd (ConstantReal 1.000000 (Real 8 [])) (Real 8 []) (ConstantReal 1.000000 (Real 8 []))) RealToReal (Real 4 []) ()) ()) (= (Var 2 f) (ImplicitCast (UnaryOp USub (ConstantReal 183745.534000 (Real 8 [])) (Real 8 []) (ConstantReal -183745.534000 (Real 8 []))) RealToReal (Real 4 []) ()) ()) (= (Var 2 b1) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 2 b2) (UnaryOp Not (ConstantLogical .false. (Logical 1 [])) (Logical 1 []) (ConstantLogical .true. (Logical 4 []))) ()) (= (Var 2 b3) (UnaryOp Not (Var 2 b2) (Logical 1 []) ()) ()) (= (Var 2 a) (UnaryOp UAdd (ConstantLogical .true. (Logical 1 [])) (Logical 1 []) (ConstantInteger 1 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp USub (ConstantLogical .false. (Logical 1 [])) (Logical 1 []) (ConstantInteger 0 (Integer 4 []))) ()) (= (Var 2 a) (UnaryOp Invert (ConstantLogical .true. (Logical 1 [])) (Logical 1 []) (ConstantInteger -2 (Integer 4 []))) ()) (= (Var 2 c) (ImplicitCast (UnaryOp UAdd (ConstantComplex 1.000000 2.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex 1.000000 2.000000 (Complex 8 []))) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 c) (ImplicitCast (UnaryOp USub (ConstantComplex 3.000000 65.000000 (Complex 8 [])) (Complex 8 []) (ConstantComplex -3.000000 -65.000000 (Complex 8 []))) ComplexToComplex (Complex 4 []) ()) ()) (= (Var 2 b1) (UnaryOp Not (ConstantComplex 3.000000 4.000000 (Complex 8 [])) (Complex 8 []) (ConstantLogical .false. (Logical 4 []))) ()) (= (Var 2 b2) (UnaryOp Not (ConstantComplex 0.000000 0.000000 (Complex 8 [])) (Complex 8 []) (ConstantLogical .true. (Logical 4 []))) ())] Source Public Implementation () .false. .false.)}) [])