Skip to content

Feat: Added missing numeric literal classes, python string can now support all numerical types #277

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
65 changes: 18 additions & 47 deletions libs/astx-transpilers/src/astx_transpilers/python_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,6 @@ def visit(self, node: astx.LiteralBoolean) -> str:
"""Handle LiteralBoolean nodes."""
return "True" if node.value else "False"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralComplex32) -> str:
"""Handle LiteralComplex32 nodes."""
real = node.value[0]
imag = node.value[1]
return f"complex({real}, {imag})"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralComplex) -> str:
"""Handle LiteralComplex nodes."""
Expand All @@ -428,37 +421,25 @@ def visit(self, node: astx.LiteralComplex) -> str:
return f"complex({real}, {imag})"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralComplex64) -> str:
"""Handle LiteralComplex64 nodes."""
real = node.value[0]
imag = node.value[1]
return f"complex({real}, {imag})"

@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:
def visit(self, node: astx.LiteralFloat) -> str:
"""Handle LiteralFloat nodes."""
return str(node.value)

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralInt32) -> str:
"""Handle LiteralInt32 nodes."""
def visit(self, node: astx.LiteralInt) -> str:
"""Handle LiteralInt nodes."""
return str(node.value)

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralString) -> str:
"""Handle LiteralUTF8String nodes."""
return repr(node.value)

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralUInt) -> str:
"""Handle LiteralUInt nodes."""
return str(node.value)

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.LiteralUTF8String) -> str:
"""Handle LiteralUTF8String nodes."""
Expand Down Expand Up @@ -529,33 +510,18 @@ def visit(self, node: astx.SwitchStmt) -> str:
return f"match {self.visit(node.value)}:\n{cases_visited}"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Complex32) -> str:
"""Handle Complex32 nodes."""
def visit(self, node: astx.Complex) -> str:
"""Handle Complex nodes."""
return "Complex"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Complex64) -> str:
"""Handle Complex64 nodes."""
return "Complex"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Float16) -> str:
def visit(self, node: astx.Floating) -> str:
"""Handle Float nodes."""
return "float"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Float32) -> str:
"""Handle Float nodes."""
return "float"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Float64) -> str:
"""Handle Float nodes."""
return "float"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.Int32) -> str:
"""Handle Int32 nodes."""
def visit(self, node: astx.SignedInteger) -> str:
"""Handle SignedInteger nodes."""
return "int"

@dispatch # type: ignore[no-redef]
Expand All @@ -577,6 +543,11 @@ def visit(self, node: astx.UnaryOp) -> str:
operand = self.visit(node.operand)
return f"({node.op_code}{operand})"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.UnsignedInteger) -> str:
"""Handle UnsignedInteger nodes."""
return "int"

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.UTF8Char) -> str:
"""Handle UTF8Char nodes."""
Expand Down
182 changes: 182 additions & 0 deletions libs/astx-transpilers/tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,48 @@ def test_transpiler_functiondef() -> None:
assert generated_code == expected_code, "generated_code != expected_code"


def test_literal_int() -> None:
"""Test astx.LiteralInt."""
# Create a LiteralInt node
literal_int_node = astx.LiteralInt(value=42)

# Generate Python code
generated_code = translate(literal_int_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_int8() -> None:
"""Test astx.LiteralInt8."""
# Create a LiteralInt8 node
literal_int8_node = astx.LiteralInt8(value=42)

# Generate Python code
generated_code = translate(literal_int8_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_int16() -> None:
"""Test astx.LiteralInt16."""
# Create a LiteralInt16 node
literal_int16_node = astx.LiteralInt16(value=42)

# Generate Python code
generated_code = translate(literal_int16_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_int32() -> None:
"""Test astx.LiteralInt32."""
# Create a LiteralInt32 node
Expand All @@ -274,6 +316,132 @@ def test_literal_int32() -> None:
assert generated_code == expected_code, "generated_code != expected_code"


def test_literal_int64() -> None:
"""Test astx.LiteralInt64."""
# Create a LiteralInt64 node
literal_int64_node = astx.LiteralInt64(value=42)

# Generate Python code
generated_code = translate(literal_int64_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_int128() -> None:
"""Test astx.LiteralInt128."""
# Create a LiteralInt128 node
literal_int128_node = astx.LiteralInt128(value=42)

# Generate Python code
generated_code = translate(literal_int128_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_Uint() -> None:
"""Test astx.LiteralUInt."""
# Create a LiteralUInt node
literal_Uint_node = astx.LiteralUInt(value=42)

# Generate Python code
generated_code = translate(literal_Uint_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_Uint8() -> None:
"""Test astx.LiteralUInt8."""
# Create a LiteralUInt8 node
literal_Uint8_node = astx.LiteralUInt8(value=42)

# Generate Python code
generated_code = translate(literal_Uint8_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_Uint16() -> None:
"""Test astx.LiteralUInt16."""
# Create a LiteralUInt16 node
literal_Uint16_node = astx.LiteralUInt16(value=42)

# Generate Python code
generated_code = translate(literal_Uint16_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_Uint32() -> None:
"""Test astx.LiteralUInt32."""
# Create a LiteralUInt32 node
literal_Uint32_node = astx.LiteralUInt32(value=42)

# Generate Python code
generated_code = translate(literal_Uint32_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_Uint64() -> None:
"""Test astx.LiteralUInt64."""
# Create a LiteralInt64 node
literal_Uint64_node = astx.LiteralUInt64(value=42)

# Generate Python code
generated_code = translate(literal_Uint64_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_Uint128() -> None:
"""Test astx.LiteralUInt128."""
# Create a LiteralUInt128 node
literal_Uint128_node = astx.LiteralUInt128(value=42)

# Generate Python code
generated_code = translate(literal_Uint128_node)
expected_code = "42"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_float() -> None:
"""Test astx.LiteralFloat."""
# Create a LiteralFloat node
literal_float_node = astx.LiteralFloat(value=3.14)

# Generate Python code
generated_code = translate(literal_float_node)
expected_code = "3.14"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_float16() -> None:
"""Test astx.LiteralFloat16."""
# Create a LiteralFloat16 node
Expand Down Expand Up @@ -310,6 +478,20 @@ 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(real=1, imag=2.8)

# Generate Python code
generated_code = translate(literal_complex_node)
expected_code = "complex(1, 2.8)"

assert generated_code == expected_code, (
f"Expected '{expected_code}', but got '{generated_code}'"
)


def test_literal_complex32() -> None:
"""Test astx.LiteralComplex32."""
# Create a LiteralComplex32 node
Expand Down
6 changes: 6 additions & 0 deletions libs/astx/src/astx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@
LiteralDate,
LiteralDateTime,
LiteralDict,
LiteralFloat,
LiteralFloat16,
LiteralFloat32,
LiteralFloat64,
LiteralInt,
LiteralInt8,
LiteralInt16,
LiteralInt32,
Expand All @@ -109,6 +111,7 @@
LiteralTime,
LiteralTimestamp,
LiteralTuple,
LiteralUInt,
LiteralUInt8,
LiteralUInt16,
LiteralUInt32,
Expand Down Expand Up @@ -296,9 +299,11 @@ def get_version() -> str:
"LiteralDate",
"LiteralDateTime",
"LiteralDict",
"LiteralFloat",
"LiteralFloat16",
"LiteralFloat32",
"LiteralFloat64",
"LiteralInt",
"LiteralInt8",
"LiteralInt16",
"LiteralInt32",
Expand All @@ -311,6 +316,7 @@ def get_version() -> str:
"LiteralTime",
"LiteralTimestamp",
"LiteralTuple",
"LiteralUInt",
"LiteralUInt8",
"LiteralUInt16",
"LiteralUInt32",
Expand Down
6 changes: 6 additions & 0 deletions libs/astx/src/astx/literals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
LiteralComplex,
LiteralComplex32,
LiteralComplex64,
LiteralFloat,
LiteralFloat16,
LiteralFloat32,
LiteralFloat64,
LiteralInt,
LiteralInt8,
LiteralInt16,
LiteralInt32,
LiteralInt64,
LiteralInt128,
LiteralUInt,
LiteralUInt8,
LiteralUInt16,
LiteralUInt32,
Expand Down Expand Up @@ -52,9 +55,11 @@
"LiteralDate",
"LiteralDateTime",
"LiteralDict",
"LiteralFloat",
"LiteralFloat16",
"LiteralFloat32",
"LiteralFloat64",
"LiteralInt",
"LiteralInt8",
"LiteralInt16",
"LiteralInt32",
Expand All @@ -67,6 +72,7 @@
"LiteralTime",
"LiteralTimestamp",
"LiteralTuple",
"LiteralUInt",
"LiteralUInt8",
"LiteralUInt16",
"LiteralUInt32",
Expand Down
Loading
Loading