-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b8f52d
commit 3a18439
Showing
9 changed files
with
329 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,53 @@ | ||
"""AST types module.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from public import public | ||
from typeguard import typechecked | ||
|
||
from astx.base import ( | ||
NO_SOURCE_LOCATION, | ||
ASTKind, | ||
ASTNodes, | ||
DataType, | ||
Expr, | ||
ReprStruct, | ||
SourceLocation, | ||
) | ||
|
||
|
||
@public | ||
@typechecked | ||
class TypeCastExpr(Expr): | ||
"""AST class for type casting expressions.""" | ||
|
||
expr: Expr | ||
target_type: DataType | ||
|
||
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 | ||
|
||
def __str__(self) -> str: | ||
"""Return a string representation of the TypeCast expression.""" | ||
return f"TypeCastExpr ({self.expr}, {self.target_type})" | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Tests for classes in types.py.""" | ||
|
||
from __future__ import annotations | ||
|
||
from astx.datatypes import Int32 | ||
from astx.types import TypeCastExpr | ||
from astx.variables import Variable | ||
from astx.viz import visualize | ||
|
||
|
||
def test_typecastexpr() -> None: | ||
"""Test TypeCastExpr.""" | ||
# 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) | ||
|
||
assert str(cast_expr) | ||
assert cast_expr.get_struct() | ||
assert cast_expr.get_struct(simplified=True) | ||
|
||
visualize(cast_expr.get_struct()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters