Skip to content
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

TypeCastExpr - Represents a type casting operation. #110

Closed
Tracked by #73
xmnlab opened this issue Sep 17, 2024 · 0 comments
Closed
Tracked by #73

TypeCastExpr - Represents a type casting operation. #110

xmnlab opened this issue Sep 17, 2024 · 0 comments
Assignees
Labels

Comments

@xmnlab
Copy link
Contributor

xmnlab commented Sep 17, 2024

from gpt:

1. Update ASTKind Enum in astx/base.py

Add the following line to your ASTKind enumeration to include the new TypeCastExprKind:

@public
class ASTKind(Enum):
    # ... existing kinds ...

    TypeCastExprKind = -809  # Add this line

2. Define TypeCastExpr in `astx/expressions.py

Add the following class definition to represent type casting expressions:

@public
class TypeCastExpr(Expr):
    """AST class for type casting expressions."""

    expr: Expr
    target_type: DataType  # Assuming 'DataType' is defined in 'astx.datatypes'

    def __init__(
        self,
        expr: Expr,
        target_type: DataType,
        loc: SourceLocation = NO_SOURCE_LOCATION,
        parent: Optional[ASTNodes] = None,
    ) -> None:
        super().__init__(loc=loc, parent=parent)
        self.expr = expr
        self.target_type = target_type
        self.kind = ASTKind.TypeCastExprKind

Notes:

  • Inheritance: TypeCastExpr inherits from Expr since it's an expression.
  • Attributes:
    • expr: The expression being cast to a different type.
    • target_type: The data type to which the expression is being cast.
  • Usage: Represents type casting expressions similar to casting in C/C++ (e.g., (int)expr).

3. Usage Example (Optional)

Here's how you might use the TypeCastExpr class:

# Example usage (do not include in code if not needed)
from astx.expressions import TypeCastExpr
from astx.datatypes import Int32, Float64
from astx.variables import Variable

# Expression to cast
expr = Variable(name="x")

# Target type for casting
target_type = Int32()

# Create the TypeCastExpr
cast_expr = TypeCastExpr(expr=expr, target_type=target_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants