From c419910279b3620b146c1efaef2267946e494cbe Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 19 Sep 2024 18:06:32 +0530 Subject: [PATCH 01/26] Adding Classes for Complex32 and Complex64 --- src/astx/datatypes.py | 97 +++++++++++++++++++++++++++++++++++++++++++ src/astx/viz.py | 2 +- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/astx/datatypes.py b/src/astx/datatypes.py index 1abe7e6..41ec681 100644 --- a/src/astx/datatypes.py +++ b/src/astx/datatypes.py @@ -534,3 +534,100 @@ def __init__( self.value = value self.type_ = Float64 self.loc = loc + + +@public +class Complex(Number): + """Base class for complex numbers.""" + + def __init__(self, real: float, imag: float) -> None: + """Initialize a complex number with real and imaginary parts.""" + self.real = real + self.imag = imag + + def __str__(self) -> str: + """Return a string representation of the complex number.""" + return f"{self.real} + {self.imag}j" + + +@public +class Complex32(Complex): + """Complex32 data type class.""" + + nbytes: int = 8 + + def __init__(self, real: float, imag: float) -> None: + """Initialize a 32-bit complex number.""" + super().__init__(real, imag) + + +@public +class Complex64(Complex): + """Complex64 data type class.""" + + nbytes: int = 16 + + def __init__(self, real: float, imag: float) -> None: + """Initialize a 64-bit complex number.""" + super().__init__(real, imag) + + +@public +class LiteralComplex(Literal): + """Base class for literal complex numbers.""" + + value: Complex + + def __init__( + self, value: Complex, loc: SourceLocation = NO_SOURCE_LOCATION + ) -> None: + """Initialize LiteralComplex with a complex number.""" + super().__init__(loc) + if isinstance(value, Complex): + self.value = value + self.type_ = ( + Complex64 if isinstance(value, Complex64) else Complex32 + ) + else: + raise TypeError("Value must be an instance of Complex.") + self.loc = loc + + def __str__(self) -> str: + """Return a string that represents the object.""" + return f"LiteralComplex({self.value.real} + {self.value.imag}j)" + + def get_struct(self, simplified: bool = False) -> ReprStruct: + """Return the AST representation for the object.""" + key = f"LiteralComplex[{self.type_}]" + value = f"{self.value.real} + {self.value.imag}j" + return self._prepare_struct(key, value, simplified) + + +@public +class LiteralComplex32(LiteralComplex): + """LiteralComplex32 data type class.""" + + def __init__( + self, + real: float, + imag: float, + loc: SourceLocation = NO_SOURCE_LOCATION, + ) -> None: + """Initialize LiteralComplex32.""" + super().__init__(Complex32(real, imag), loc) + self.type_ = Complex32 + + +@public +class LiteralComplex64(LiteralComplex): + """LiteralComplex64 data type class.""" + + def __init__( + self, + real: float, + imag: float, + loc: SourceLocation = NO_SOURCE_LOCATION, + ) -> None: + """Initialize LiteralComplex64.""" + super().__init__(Complex64(real, imag), loc) + self.type_ = Complex64 diff --git a/src/astx/viz.py b/src/astx/viz.py index 7f4f878..b73416f 100644 --- a/src/astx/viz.py +++ b/src/astx/viz.py @@ -22,7 +22,7 @@ _AsciiGraphProxy, ) from graphviz import Digraph -from IPython.display import Image, display # type: ignore[attr-defined] +from IPython.display import Image, display from msgpack import dumps, loads from astx.types import DictDataTypesStruct, ReprStruct From 88b20f8fc83426c716784c366c20dd9f772116a8 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 19 Sep 2024 18:08:57 +0530 Subject: [PATCH 02/26] Importing New Classes --- src/astx/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/astx/__init__.py b/src/astx/__init__.py index e7db3bb..60efd1c 100644 --- a/src/astx/__init__.py +++ b/src/astx/__init__.py @@ -72,6 +72,12 @@ UInt64, UInt128, UnsignedInteger, + Complex, + Complex32, + Complex64, + LiteralComplex, + LiteralComplex32, + LiteralComplex64, ) from astx.flows import ( ForCountLoop, @@ -188,6 +194,12 @@ def get_version() -> str: "variables", "VisibilityKind", "While", + "Complex", + "Complex32", + "Complex64", + "LiteralComplex", + "LiteralComplex32", + "LiteralComplex64", ] From b798c8094da085f7b262c6a936e040e9ecc43c36 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 19 Sep 2024 23:19:09 +0530 Subject: [PATCH 03/26] Removing Linter errors --- src/astx/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/astx/__init__.py b/src/astx/__init__.py index 60efd1c..d6d4fbf 100644 --- a/src/astx/__init__.py +++ b/src/astx/__init__.py @@ -39,6 +39,9 @@ ) from astx.datatypes import ( Boolean, + Complex, + Complex32, + Complex64, DataTypeOps, Float16, Float32, @@ -51,6 +54,9 @@ Integer, Literal, LiteralBoolean, + LiteralComplex, + LiteralComplex32, + LiteralComplex64, LiteralFloat16, LiteralFloat32, LiteralFloat64, @@ -72,12 +78,6 @@ UInt64, UInt128, UnsignedInteger, - Complex, - Complex32, - Complex64, - LiteralComplex, - LiteralComplex32, - LiteralComplex64, ) from astx.flows import ( ForCountLoop, From b97e12c29946e1e79c0e0a09208de208d90ce166 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 19 Sep 2024 23:31:23 +0530 Subject: [PATCH 04/26] Updating for mypy error --- src/astx/viz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/viz.py b/src/astx/viz.py index b73416f..b4833b6 100644 --- a/src/astx/viz.py +++ b/src/astx/viz.py @@ -22,7 +22,7 @@ _AsciiGraphProxy, ) from graphviz import Digraph -from IPython.display import Image, display +from IPython.display import Image, display # type: ignore from msgpack import dumps, loads from astx.types import DictDataTypesStruct, ReprStruct From cf12f7ee2ddeca702009a26d65c2357a1c8fe011 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 19 Sep 2024 23:36:25 +0530 Subject: [PATCH 05/26] Reformatting file --- src/astx/viz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/viz.py b/src/astx/viz.py index b4833b6..c5e8ae4 100644 --- a/src/astx/viz.py +++ b/src/astx/viz.py @@ -22,7 +22,7 @@ _AsciiGraphProxy, ) from graphviz import Digraph -from IPython.display import Image, display # type: ignore +from IPython.display import Image, display # type: ignore from msgpack import dumps, loads from astx.types import DictDataTypesStruct, ReprStruct From d6cbf55b4d96a101ff7fb2a2b956bcfd369a6544 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Fri, 20 Sep 2024 01:52:15 +0530 Subject: [PATCH 06/26] Adding tests for Complex Numbers --- tests/test_datatype_complex.py | 82 ++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/test_datatype_complex.py diff --git a/tests/test_datatype_complex.py b/tests/test_datatype_complex.py new file mode 100644 index 0000000..2dab50d --- /dev/null +++ b/tests/test_datatype_complex.py @@ -0,0 +1,82 @@ +"""Tests for complex number data types.""" + +from __future__ import annotations + +from typing import Callable, Type + +import astx +import pytest + +from astx.operators import BinaryOp, UnaryOp +from astx.variables import Variable + +VAR_A = Variable("a") + +COMPLEX_LITERAL_CLASSES = [ + astx.LiteralComplex32, + astx.LiteralComplex64, +] + + +def test_variable() -> None: + """Test variable complex.""" + var_a = Variable("a") + var_b = Variable("b") + + BinaryOp(op_code="+", lhs=var_a, rhs=var_b) + + +@pytest.mark.parametrize("literal_class", COMPLEX_LITERAL_CLASSES) +def test_literal(literal_class: Type[astx.Literal]) -> None: + """Test complex literals.""" + lit_a = literal_class(1.5, 2.5) + lit_b = literal_class(3.0, -4.0) + BinaryOp(op_code="+", lhs=lit_a, rhs=lit_b) + + +@pytest.mark.parametrize( + "fn_bin_op,op_code", + [ + (lambda literal_class: VAR_A + literal_class(1.5, 2.5), "+"), + (lambda literal_class: VAR_A - literal_class(1.5, 2.5), "-"), + (lambda literal_class: VAR_A / literal_class(1.5, 2.5), "/"), + (lambda literal_class: VAR_A * literal_class(1.5, 2.5), "*"), + (lambda literal_class: VAR_A == literal_class(1.5, 2.5), "=="), + (lambda literal_class: VAR_A != literal_class(1.5, 2.5), "!="), + ], +) +@pytest.mark.parametrize("literal_class", COMPLEX_LITERAL_CLASSES) +def test_bin_ops( + literal_class: Type[astx.Literal], + fn_bin_op: Callable[[Type[astx.Literal]], BinaryOp], + op_code: str, +) -> None: + """Test binary operations on complex numbers.""" + bin_op = fn_bin_op(literal_class) + assert bin_op.op_code == op_code + assert str(bin_op) != "" + assert repr(bin_op) != "" + assert bin_op.get_struct() != {} + assert bin_op.get_struct(simplified=True) != {} + + +@pytest.mark.parametrize( + "fn_unary_op,op_code", + [ + (lambda literal_class: +literal_class(1.5, 2.5), "+"), + (lambda literal_class: -literal_class(1.5, 2.5), "-"), + ], +) +@pytest.mark.parametrize("literal_class", COMPLEX_LITERAL_CLASSES) +def test_unary_ops( + literal_class: Type[astx.Literal], + fn_unary_op: Callable[[Type[astx.Literal]], UnaryOp], + op_code: str, +) -> None: + """Test unary operations on complex numbers.""" + unary_op = fn_unary_op(literal_class) + assert unary_op.op_code == op_code + assert str(unary_op) != "" + assert repr(unary_op) != "" + assert unary_op.get_struct() != {} + assert unary_op.get_struct(simplified=True) != {} From e7f3f4de306c22b1d92a51cb4f66a6a4272e2b9a Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Fri, 20 Sep 2024 23:15:09 +0530 Subject: [PATCH 07/26] Update Literal Complex return statement From a3640276dd93522d9faaa6d44daede9c9832015e Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Fri, 20 Sep 2024 23:19:44 +0530 Subject: [PATCH 08/26] Updating Return Structure for literal class --- src/astx/datatypes.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/astx/datatypes.py b/src/astx/datatypes.py index 41ec681..77a9eed 100644 --- a/src/astx/datatypes.py +++ b/src/astx/datatypes.py @@ -597,9 +597,12 @@ def __str__(self) -> str: return f"LiteralComplex({self.value.real} + {self.value.imag}j)" def get_struct(self, simplified: bool = False) -> ReprStruct: - """Return the AST representation for the object.""" - key = f"LiteralComplex[{self.type_}]" - value = f"{self.value.real} + {self.value.imag}j" + """Return the AST representation for the complex literal.""" + key = f"{self.__class__.__name__}: {self.value}" + value = { + "real": self.value.real, + "imag": self.value.imag, + } return self._prepare_struct(key, value, simplified) From 27f59e5df227d2d1d3408e2d18143df47dafeb9e Mon Sep 17 00:00:00 2001 From: Ivan Ogasawara Date: Tue, 24 Sep 2024 07:31:08 -0400 Subject: [PATCH 09/26] Update src/astx/datatypes.py --- src/astx/datatypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/datatypes.py b/src/astx/datatypes.py index 77a9eed..5a9cd08 100644 --- a/src/astx/datatypes.py +++ b/src/astx/datatypes.py @@ -599,7 +599,7 @@ def __str__(self) -> str: def get_struct(self, simplified: bool = False) -> ReprStruct: """Return the AST representation for the complex literal.""" key = f"{self.__class__.__name__}: {self.value}" - value = { + value: ReprStruct = { "real": self.value.real, "imag": self.value.imag, } From 554f0b95a196466cb2dc8d83605cf5302dafb69c Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 3 Oct 2024 23:27:09 +0530 Subject: [PATCH 10/26] Adding transpilers --- src/astx/transpilers/python.py | 54 ++++++++++++++++++++++++++++++++ tests/transpilers/test_python.py | 42 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/src/astx/transpilers/python.py b/src/astx/transpilers/python.py index 2f586a3..23739ea 100644 --- a/src/astx/transpilers/python.py +++ b/src/astx/transpilers/python.py @@ -113,3 +113,57 @@ def visit(self, node: astx.VariableAssignment) -> str: target = node.name value = self.visit(node.value) return f"{target} = {value}" + + @dispatch # type: ignore[no-redef] + def visit(self, node: Type[astx.Float16]) -> str: + """Handle Float nodes.""" + return "float" + + @dispatch # type: ignore[no-redef] + def visit(self, node: Type[astx.Float32]) -> str: + """Handle Float nodes.""" + return "float" + + @dispatch # type: ignore[no-redef] + def visit(self, node: Type[astx.Float64]) -> str: + """Handle Float nodes.""" + return "float" + + @dispatch # type: ignore[no-redef] + def visit(self, node: astx.LiteralFloat16) -> str: + """Handle LiteralFloat nodes.""" + return str(node.value) + + @dispatch # type: ignore[no-redef] + def visit(self, node: astx.LiteralFloat32) -> str: + """Handle LiteralFloat nodes.""" + return str(node.value) + + @dispatch # type: ignore[no-redef] + def visit(self, node: astx.LiteralFloat64) -> str: + """Handle LiteralFloat nodes.""" + return str(node.value) + + @dispatch # type: ignore[no-redef] + def visit(self, node: Type[astx.Complex32]) -> str: + """Handle Complex32 nodes.""" + return "(float, float)" + + @dispatch # type: ignore[no-redef] + def visit(self, node: Type[astx.Complex64]) -> str: + """Handle Complex64 nodes.""" + return "(float, float)" + + @dispatch # type: ignore[no-redef] + def visit(self, node: astx.LiteralComplex32) -> str: + """Handle LiteralComplex32 nodes.""" + real = node.value.real + imag = node.value.imag + return f"({real}, {imag})" + + @dispatch # type: ignore[no-redef] + def visit(self, node: astx.LiteralComplex64) -> str: + """Handle LiteralComplex64 nodes.""" + real = node.value.real + imag = node.value.imag + return f"({real}, {imag})" diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 101a75a..f31641b 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -59,3 +59,45 @@ def test_function() -> None: ) assert generated_code == expected_code, "generated_code != expected_code" + +def test_float32() -> None: + """Test astx.Float32.""" + # Variable declaration + float32_var = astx.VariableAssignment( + name="x", + value=astx.LiteralFloat32(10.5), + loc=astx.SourceLocation(line=1, col=0), + ) + # Binary operation + binary_op = astx.BinaryOp( + op_code="+", + lhs=astx.Variable(name="x"), + rhs=astx.LiteralFloat32(5.5), + loc=astx.SourceLocation(line=2, col=4), + ) + # Function body + body = astx.Block() + body.append(float32_var) + body.append(binary_op) + # Function definition + add_float32_function = astx.Function( + prototype=astx.FunctionPrototype( + name="add_float32", + args=astx.Arguments(), + return_type=astx.Float32, + ), + body=body, + loc=astx.SourceLocation(line=1, col=0), + ) + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + # Generate Python code + generated_code = generator.visit(add_float32_function) + expected_code = "\n".join( + [ + "def add_float32() -> float:", + " x = 10.5", + " return (x + 5.5)", + ] + ) + assert generated_code == expected_code, "generated_code != expected_code" From b62c86f2802c5dc813856f79159848bc70c5afcd Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:05:15 +0530 Subject: [PATCH 11/26] Adding test for datatype Int32 --- tests/transpilers/test_python.py | 46 +++++++------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index f31641b..8e669d2 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -60,44 +60,16 @@ def test_function() -> None: assert generated_code == expected_code, "generated_code != expected_code" -def test_float32() -> None: - """Test astx.Float32.""" - # Variable declaration - float32_var = astx.VariableAssignment( - name="x", - value=astx.LiteralFloat32(10.5), - loc=astx.SourceLocation(line=1, col=0), - ) - # Binary operation - binary_op = astx.BinaryOp( - op_code="+", - lhs=astx.Variable(name="x"), - rhs=astx.LiteralFloat32(5.5), - loc=astx.SourceLocation(line=2, col=4), - ) - # Function body - body = astx.Block() - body.append(float32_var) - body.append(binary_op) - # Function definition - add_float32_function = astx.Function( - prototype=astx.FunctionPrototype( - name="add_float32", - args=astx.Arguments(), - return_type=astx.Float32, - ), - body=body, - loc=astx.SourceLocation(line=1, col=0), - ) +def test_int32_type() -> None: + """Test astx.Int32 type.""" + # Create an Int32 node + int32_node = astx.Int32() + # Initialize the generator generator = astx2py.ASTxPythonTranspiler() + # Generate Python code - generated_code = generator.visit(add_float32_function) - expected_code = "\n".join( - [ - "def add_float32() -> float:", - " x = 10.5", - " return (x + 5.5)", - ] - ) + generated_code = generator.visit(int32_node) + expected_code = "int" + assert generated_code == expected_code, "generated_code != expected_code" From f207a3cfcb37534ad21d74d65e183acf7f90c25a Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:12:00 +0530 Subject: [PATCH 12/26] Adding test for Literal Int32 --- tests/transpilers/test_python.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 8e669d2..4561a4c 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -60,16 +60,16 @@ def test_function() -> None: assert generated_code == expected_code, "generated_code != expected_code" -def test_int32_type() -> None: - """Test astx.Int32 type.""" - # Create an Int32 node - int32_node = astx.Int32() +def test_literal_int32() -> None: + """Test astx.LiteralInt32.""" + # Create a LiteralInt32 node + literal_int32_node = astx.LiteralInt32(value=42) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() # Generate Python code - generated_code = generator.visit(int32_node) - expected_code = "int" + generated_code = generator.visit(literal_int32_node) + expected_code = "42" assert generated_code == expected_code, "generated_code != expected_code" From f0bf9f68b53e82ad3f26b17c5f963b0904cadb1e Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:17:29 +0530 Subject: [PATCH 13/26] Adding tests for LiteraFloat Transpilers --- tests/transpilers/test_python.py | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 4561a4c..45c7b03 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -60,6 +60,7 @@ def test_function() -> None: assert generated_code == expected_code, "generated_code != expected_code" + def test_literal_int32() -> None: """Test astx.LiteralInt32.""" # Create a LiteralInt32 node @@ -73,3 +74,48 @@ def test_literal_int32() -> None: expected_code = "42" assert generated_code == expected_code, "generated_code != expected_code" + + +def test_literal_float16() -> None: + """Test astx.LiteralFloat16.""" + # Create a LiteralFloat16 node + literal_float16_node = astx.LiteralFloat16(value=3.14) + + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + + # Generate Python code + generated_code = generator.visit(literal_float16_node) + expected_code = "3.14" + + assert generated_code == expected_code, "generated_code != expected_code" + + +def test_literal_float32() -> None: + """Test astx.LiteralFloat32.""" + # Create a LiteralFloat32 node + literal_float32_node = astx.LiteralFloat32(value=2.718) + + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + + # Generate Python code + generated_code = generator.visit(literal_float32_node) + expected_code = "2.718" + + assert generated_code == expected_code, "generated_code != expected_code" + + +def test_literal_float64() -> None: + """Test astx.LiteralFloat64.""" + # Create a LiteralFloat64 node + literal_float64_node = astx.LiteralFloat64(value=1.414) + + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + + # Generate Python code + generated_code = generator.visit(literal_float64_node) + expected_code = "1.414" + + assert generated_code == expected_code, "generated_code != expected_code" From 4bde48716c016cc69bb66019f1376448dcfc8b83 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:29:19 +0530 Subject: [PATCH 14/26] Adding tests fot Literalcomplex32 and LiteralComplex64 visit methods --- tests/transpilers/test_python.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 45c7b03..0c1f34e 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -119,3 +119,33 @@ def test_literal_float64() -> None: expected_code = "1.414" assert generated_code == expected_code, "generated_code != expected_code" + + +def test_literal_complex32() -> None: + """Test astx.LiteralComplex32.""" + # Create a LiteralComplex32 node + literal_complex32_node = astx.LiteralComplex32(value=complex(1.5, 2.5)) + + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + + # Generate Python code + generated_code = generator.visit(literal_complex32_node) + expected_code = "(1.5, 2.5)" + + assert generated_code == expected_code, "generated_code != expected_code" + + +def test_literal_complex64() -> None: + """Test astx.LiteralComplex64.""" + # Create a LiteralComplex64 node + literal_complex64_node = astx.LiteralComplex64(value=complex(3.5, 4.5)) + + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + + # Generate Python code + generated_code = generator.visit(literal_complex64_node) + expected_code = "(3.5, 4.5)" + + assert generated_code == expected_code, "generated_code != expected_code" From f92413ac534537fec533e74ccb81c3e8ca67866b Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:35:43 +0530 Subject: [PATCH 15/26] Removing Typo Errors --- tests/transpilers/test_python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 0c1f34e..7922fb0 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -124,7 +124,7 @@ def test_literal_float64() -> None: def test_literal_complex32() -> None: """Test astx.LiteralComplex32.""" # Create a LiteralComplex32 node - literal_complex32_node = astx.LiteralComplex32(value=complex(1.5, 2.5)) + literal_complex32_node = astx.LiteralComplex32(value=Complex32(1.5, 2.5)) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() @@ -139,7 +139,7 @@ def test_literal_complex32() -> None: def test_literal_complex64() -> None: """Test astx.LiteralComplex64.""" # Create a LiteralComplex64 node - literal_complex64_node = astx.LiteralComplex64(value=complex(3.5, 4.5)) + literal_complex64_node = astx.LiteralComplex64(value=Complex64 (3.5, 4.5)) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() From c214c9718694c25e1f01dd416cc63d3f34fb40d7 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:41:41 +0530 Subject: [PATCH 16/26] Testing complex visit methods --- tests/transpilers/test_python.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 7922fb0..f44cc15 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -124,14 +124,14 @@ def test_literal_float64() -> None: def test_literal_complex32() -> None: """Test astx.LiteralComplex32.""" # Create a LiteralComplex32 node - literal_complex32_node = astx.LiteralComplex32(value=Complex32(1.5, 2.5)) + literal_complex32_node = astx.LiteralComplex32(value=complex(1, 2)) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() # Generate Python code generated_code = generator.visit(literal_complex32_node) - expected_code = "(1.5, 2.5)" + expected_code = "(1, 2)" assert generated_code == expected_code, "generated_code != expected_code" @@ -139,13 +139,13 @@ def test_literal_complex32() -> None: def test_literal_complex64() -> None: """Test astx.LiteralComplex64.""" # Create a LiteralComplex64 node - literal_complex64_node = astx.LiteralComplex64(value=Complex64 (3.5, 4.5)) + literal_complex64_node = astx.LiteralComplex64(value=complex(3, 4)) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() # Generate Python code generated_code = generator.visit(literal_complex64_node) - expected_code = "(3.5, 4.5)" + expected_code = "(3, 4)" assert generated_code == expected_code, "generated_code != expected_code" From 81a5055bda51275e5b23141611ef424d280cf589 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:46:24 +0530 Subject: [PATCH 17/26] Updating Tests for visit methods --- tests/transpilers/test_python.py | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index f44cc15..a58cc2d 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -121,25 +121,10 @@ def test_literal_float64() -> None: assert generated_code == expected_code, "generated_code != expected_code" -def test_literal_complex32() -> None: - """Test astx.LiteralComplex32.""" - # Create a LiteralComplex32 node - literal_complex32_node = astx.LiteralComplex32(value=complex(1, 2)) - - # Initialize the generator - generator = astx2py.ASTxPythonTranspiler() - - # Generate Python code - generated_code = generator.visit(literal_complex32_node) - expected_code = "(1, 2)" - - assert generated_code == expected_code, "generated_code != expected_code" - - -def test_literal_complex64() -> None: - """Test astx.LiteralComplex64.""" - # Create a LiteralComplex64 node - literal_complex64_node = astx.LiteralComplex64(value=complex(3, 4)) +def test_literal_complex() -> None: + """Test astx.LiteralComplex.""" + # Create a LiteralComplex node + literal_complex64_node = astx.LiteralComplex(value=complex(3, 4)) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() From 9a100e440e8a213bb32ea33ad8a74b9ce150717f Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:49:32 +0530 Subject: [PATCH 18/26] Updating test --- tests/transpilers/test_python.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index a58cc2d..e55b290 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -124,13 +124,13 @@ def test_literal_float64() -> None: def test_literal_complex() -> None: """Test astx.LiteralComplex.""" # Create a LiteralComplex node - literal_complex64_node = astx.LiteralComplex(value=complex(3, 4)) + literal_complex_node = astx.LiteralComplex(value=Complex32(3, 4)) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() # Generate Python code - generated_code = generator.visit(literal_complex64_node) + generated_code = generator.visit(literal_complex_node) expected_code = "(3, 4)" assert generated_code == expected_code, "generated_code != expected_code" From cb0aef484927df7a933f6f44ef77e0ea2f0500f5 Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:55:14 +0530 Subject: [PATCH 19/26] Updating complex tests --- tests/transpilers/test_python.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index e55b290..216b581 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -121,16 +121,31 @@ def test_literal_float64() -> None: assert generated_code == expected_code, "generated_code != expected_code" -def test_literal_complex() -> None: - """Test astx.LiteralComplex.""" - # Create a LiteralComplex node - literal_complex_node = astx.LiteralComplex(value=Complex32(3, 4)) +def test_literal_complex32() -> None: + """Test astx.LiteralComplex32.""" + # Create a LiteralComplex32 node + literal_complex32_node = astx.LiteralComplex32(real=1, imag=2.8) # Initialize the generator generator = astx2py.ASTxPythonTranspiler() # Generate Python code - generated_code = generator.visit(literal_complex_node) - expected_code = "(3, 4)" + generated_code = generator.visit(literal_complex32_node) + expected_code = "(1, 2.8)" - assert generated_code == expected_code, "generated_code != expected_code" + assert generated_code == expected_code, f"Expected '{expected_code}', but got '{generated_code}'" + + +def test_literal_complex64() -> None: + """Test astx.LiteralComplex64.""" + # Create a LiteralComplex64 node + literal_complex64_node = astx.LiteralComplex64(real=3.5, imag=4) + + # Initialize the generator + generator = astx2py.ASTxPythonTranspiler() + + # Generate Python code + generated_code = generator.visit(literal_complex64_node) + expected_code = "(3.5, 4)" + + assert generated_code == expected_code, f"Expected '{expected_code}', but got '{generated_code}'" From 4e13b3e92aeb27d4a67b763bc0c9bfb0a6478b3d Mon Sep 17 00:00:00 2001 From: vikasgrewal16 Date: Thu, 10 Oct 2024 13:57:15 +0530 Subject: [PATCH 20/26] Removing Linter Errors --- tests/transpilers/test_python.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 216b581..6bfbdfc 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -133,7 +133,9 @@ def test_literal_complex32() -> None: generated_code = generator.visit(literal_complex32_node) expected_code = "(1, 2.8)" - assert generated_code == expected_code, f"Expected '{expected_code}', but got '{generated_code}'" + assert ( + generated_code == expected_code + ), f"Expected '{expected_code}', but got '{generated_code}'" def test_literal_complex64() -> None: @@ -148,4 +150,6 @@ def test_literal_complex64() -> None: generated_code = generator.visit(literal_complex64_node) expected_code = "(3.5, 4)" - assert generated_code == expected_code, f"Expected '{expected_code}', but got '{generated_code}'" + assert ( + generated_code == expected_code + ), f"Expected '{expected_code}', but got '{generated_code}'" From eb3016e553b6fb2bcaa859ca253ea009d376a5f6 Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Sat, 12 Oct 2024 14:19:38 +0530 Subject: [PATCH 21/26] Update src/astx/transpilers/python.py Co-authored-by: Ivan Ogasawara --- src/astx/transpilers/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/transpilers/python.py b/src/astx/transpilers/python.py index 23739ea..c9811fa 100644 --- a/src/astx/transpilers/python.py +++ b/src/astx/transpilers/python.py @@ -159,7 +159,7 @@ def visit(self, node: astx.LiteralComplex32) -> str: """Handle LiteralComplex32 nodes.""" real = node.value.real imag = node.value.imag - return f"({real}, {imag})" + return f"complex({real}, {imag})" @dispatch # type: ignore[no-redef] def visit(self, node: astx.LiteralComplex64) -> str: From 34cbde5af174f0da3afec6cce92c65b627686f3d Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Sat, 12 Oct 2024 14:19:46 +0530 Subject: [PATCH 22/26] Update src/astx/transpilers/python.py Co-authored-by: Ivan Ogasawara --- src/astx/transpilers/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/transpilers/python.py b/src/astx/transpilers/python.py index c9811fa..671f510 100644 --- a/src/astx/transpilers/python.py +++ b/src/astx/transpilers/python.py @@ -166,4 +166,4 @@ def visit(self, node: astx.LiteralComplex64) -> str: """Handle LiteralComplex64 nodes.""" real = node.value.real imag = node.value.imag - return f"({real}, {imag})" + return f"complex({real}, {imag})" From 8f778a3bea164b1363c01f97c99f8acc62612699 Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Sat, 12 Oct 2024 14:19:52 +0530 Subject: [PATCH 23/26] Update src/astx/transpilers/python.py Co-authored-by: Ivan Ogasawara --- src/astx/transpilers/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/transpilers/python.py b/src/astx/transpilers/python.py index 671f510..3618b25 100644 --- a/src/astx/transpilers/python.py +++ b/src/astx/transpilers/python.py @@ -152,7 +152,7 @@ def visit(self, node: Type[astx.Complex32]) -> str: @dispatch # type: ignore[no-redef] def visit(self, node: Type[astx.Complex64]) -> str: """Handle Complex64 nodes.""" - return "(float, float)" + return "Complex" @dispatch # type: ignore[no-redef] def visit(self, node: astx.LiteralComplex32) -> str: From f610cb7c20431817237e762ce790b703796214ab Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Sat, 12 Oct 2024 14:19:59 +0530 Subject: [PATCH 24/26] Update tests/transpilers/test_python.py Co-authored-by: Ivan Ogasawara --- tests/transpilers/test_python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 6bfbdfc..4a0d82a 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -148,7 +148,7 @@ def test_literal_complex64() -> None: # Generate Python code generated_code = generator.visit(literal_complex64_node) - expected_code = "(3.5, 4)" + expected_code = "complex(3.5, 4)" assert ( generated_code == expected_code From f45560d3bc538b4b9d778f00062904b3191a5cbc Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Sat, 12 Oct 2024 14:20:06 +0530 Subject: [PATCH 25/26] Update tests/transpilers/test_python.py Co-authored-by: Ivan Ogasawara --- tests/transpilers/test_python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/transpilers/test_python.py b/tests/transpilers/test_python.py index 4a0d82a..3f192d8 100644 --- a/tests/transpilers/test_python.py +++ b/tests/transpilers/test_python.py @@ -131,7 +131,7 @@ def test_literal_complex32() -> None: # Generate Python code generated_code = generator.visit(literal_complex32_node) - expected_code = "(1, 2.8)" + expected_code = "complex(1, 2.8)" assert ( generated_code == expected_code From fc36e76efa4c871fc41ea70868d1354580b000ba Mon Sep 17 00:00:00 2001 From: Vikas Grewal Date: Sat, 12 Oct 2024 14:20:23 +0530 Subject: [PATCH 26/26] Update src/astx/transpilers/python.py Co-authored-by: Ivan Ogasawara --- src/astx/transpilers/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/astx/transpilers/python.py b/src/astx/transpilers/python.py index 3618b25..201c393 100644 --- a/src/astx/transpilers/python.py +++ b/src/astx/transpilers/python.py @@ -147,7 +147,7 @@ def visit(self, node: astx.LiteralFloat64) -> str: @dispatch # type: ignore[no-redef] def visit(self, node: Type[astx.Complex32]) -> str: """Handle Complex32 nodes.""" - return "(float, float)" + return "Complex" @dispatch # type: ignore[no-redef] def visit(self, node: Type[astx.Complex64]) -> str: