Skip to content

casting

AST types module.

Classes:

TypeCastExpr

TypeCastExpr(expr: Expr, target_type: DataType, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for type casting expressions.

Methods:

  • get_struct

    Return the AST structure of the TypeCast expression.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/types/casting.py
29
30
31
32
33
34
35
36
37
38
39
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

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the TypeCast expression.

Source code in src/astx/types/casting.py
45
46
47
48
49
50
51
52
53
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the TypeCast expression."""
    key = "TypeCastExpr"
    value: ReprStruct = {
        "expression": self.expr.get_struct(simplified),
        "target_type": self.target_type.get_struct(simplified),
    }

    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
265
266
267
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
259
260
261
262
263
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )