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

feat: add TypeCastExpr class #130

Merged
merged 17 commits into from
Nov 1, 2024
Merged

feat: add TypeCastExpr class #130

merged 17 commits into from
Nov 1, 2024

Conversation

apkrelling
Copy link
Contributor

@apkrelling apkrelling commented Oct 24, 2024

Pull Request description

This PR adds TypeCastExpr class.
This PR is an attempt to solve #110 .

  • added new ASTKind's
  • created TypeCastExpr classes
  • updated __init__.py to include the new imports and classes in the__all__ list
  • added the new class visit method in transpilers/python.py
  • added test in tests/transpilers/test_python.py
  • added test in tests/test_types.py

How to test these changes

from astx.datatypes import Int32
from astx.types import TypeCastExpr
from astx.variables import Variable
from astx.transpilers import python as astx2py 

# 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)

cast_expr.get_struct()
cast_expr

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

# Generate Python code
generated_code = generator.visit(cast_expr)
print(generated_code)

Pull Request checklists

This PR is a:

  • bug-fix
  • new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more
    complexity.
  • New and old tests passed locally.

@apkrelling apkrelling marked this pull request as draft October 24, 2024 22:24
@apkrelling
Copy link
Contributor Author

TO-DO:

  • maybe add more tests
  • maybe add more transpiler tests
  • verify the AST representation in png and ascii (below)

@apkrelling
Copy link
Contributor Author

apkrelling commented Oct 24, 2024

Below are the ascii and png AST reprs for

from astx.types import TypeCastExpr
from astx.datatypes import Int32
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)
cast_expr

The difference is due to modifications in the get_struct() method.

Case 1:

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=True),
        "target_type": self.target_type.get_struct(simplified=True),
    }

    return self._prepare_struct(key, value, simplified)

asts

Case 2:

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(),
        "target_type": self.target_type.get_struct(),
    }

    return self._prepare_struct(key, value, simplified)

ASTs2

@apkrelling
Copy link
Contributor Author

apkrelling commented Oct 28, 2024

new AST repr, for both png and ascii:

from astx.types import TypeCastExpr
from astx.datatypes import Int32
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)
cast_expr

new_asts_typecastexpr

@apkrelling apkrelling marked this pull request as ready for review October 28, 2024 14:14
@apkrelling apkrelling changed the title feat: TypeCastExpr feat: add TypeCastExpr class Oct 28, 2024
@xmnlab
Copy link
Contributor

xmnlab commented Nov 1, 2024

I will fix the linter issue in a follow up PR

@xmnlab xmnlab merged commit 3a18439 into arxlang:main Nov 1, 2024
11 of 12 checks passed
@xmnlab
Copy link
Contributor

xmnlab commented Nov 1, 2024

thanks for working on that @apkrelling !

Copy link

🎉 This PR is included in version 0.16.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants