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 IfExpr class #143

Merged
merged 5 commits into from
Nov 19, 2024
Merged

feat: add IfExpr class #143

merged 5 commits into from
Nov 19, 2024

Conversation

apkrelling
Copy link
Contributor

@apkrelling apkrelling commented Nov 16, 2024

Pull Request description

This PR adds IfExpr and modifies If -> IfStmt.
This PR is an attempt to resolve #93 .

  • added new ASTKind
  • created IfExpr class
  • updated __init__.py to include the new imports and classes in the __all__ list
  • added test for IfExpr
  • added transpiler visit method for IfExpr
  • added transpiler test for IfExpr
  • added transpiler visit method for IfStmt
  • added transpiler tests for IfStmt

How to test these changes

from astx.operators import BinaryOp
from astx.blocks import Block
from astx.datatypes import LiteralInt32
from astx.flows import IfExpr
from astx.transpilers import python as astx2py

# determine condition
cond = BinaryOp(op_code=">", lhs=LiteralInt32(1), rhs=LiteralInt32(2))

# create then and else blocks
then_block = Block()
else_block = Block()

# define literals
lit_2 = LiteralInt32(2)
lit_3 = LiteralInt32(3)

# define operations
op1 = lit_2 + lit_3
op2 = lit_2 - lit_3

# Add statements to the then and else blocks
then_block.append(op1)
else_block.append(op2)

# define if Expr
if_expr = IfExpr(condition=cond, then=then_block, else_=else_block)

if_expr
if_expr.__str__()

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

# Generate Python code
generated_code = generator.visit(if_expr)
generated_code

if_expr_ascii
if_expr_png

Output of if_expr.__str__(): 'IfExpr[BinaryOp[>](LiteralInt32(1),LiteralInt32(2))]'

Output of generated_code: 'result = (2 + 3) if (1 > 2) else (2 - 3)'

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.

Additional information

Reviewer's checklist

Copy and paste this template for your review's note:

## Reviewer's Checklist

- [ ] I managed to reproduce the problem locally from the `main` branch
- [ ] I managed to test the new changes locally
- [ ] I confirm that the issues mentioned were fixed/resolved .

@apkrelling apkrelling marked this pull request as draft November 16, 2024 15:54
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@apkrelling apkrelling marked this pull request as ready for review November 16, 2024 16:01
@apkrelling apkrelling marked this pull request as draft November 16, 2024 16:03
@xmnlab
Copy link
Contributor

xmnlab commented Nov 18, 2024

hi @apkrelling,

about the output from the transpiler for the if expression, python has already this syntax, although it is for an inline expression.

maybe you can use this as a reference:

>>> import ast
>>> ast_text = "a = 1 if mycond == 1 else 2 if mycond == 2 else 3"
>>> pprint(ast.dump(ast.parse(ast_text)))
("Module(body=[Assign(targets=[Name(id='a', ctx=Store())], "
 "value=IfExp(test=Compare(left=Name(id='mycond', ctx=Load()), ops=[Eq()], "
 'comparators=[Constant(value=1)]), body=Constant(value=1), '
 "orelse=IfExp(test=Compare(left=Name(id='mycond', ctx=Load()), ops=[Eq()], "
 'comparators=[Constant(value=2)]), body=Constant(value=2), '
 'orelse=Constant(value=3))))], type_ignores=[])')
>>> 

@xmnlab
Copy link
Contributor

xmnlab commented Nov 18, 2024

for if stmt, the result from the transpiler would look like this:

if  (1 > 2):
    x = (2 + 3)
else:
    x = (2 - 3)'

src/astx/transpilers/python.py Outdated Show resolved Hide resolved
src/astx/transpilers/python.py Outdated Show resolved Hide resolved
@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 18, 2024

Transpiler outputs:

from astx.operators import BinaryOp
from astx.blocks import Block
from astx.datatypes import LiteralInt32
from astx.flows import IfExpr, IfStmt
from astx.transpilers import python as astx2py

# determine condition
cond = BinaryOp(op_code=">", lhs=LiteralInt32(1), rhs=LiteralInt32(2))

# create then and else blocks
then_block = Block()
else_block = Block()

# define literals
lit_2 = LiteralInt32(2)
lit_3 = LiteralInt32(3)

# define operations
op1 = lit_2 + lit_3
op2 = lit_2 - lit_3

# Add statements to the then and else blocks
then_block.append(op1)
else_block.append(op2)

# define if Stmt/Expr
if_stmt1 = IfStmt(condition=cond, then=then_block, else_=else_block)
if_stmt2 = IfStmt(condition=cond, then=then_block)
if_expr1 = IfExpr(condition=cond, then=then_block, else_=else_block)
if_expr2 = IfExpr(condition=cond, then=then_block)

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

# Generate Python code
generated_code_stmt1 = generator.visit(if_stmt1)
generated_code_stmt2 = generator.visit(if_stmt2)
generated_code_expr1 = generator.visit(if_expr1)
generated_code_expr2 = generator.visit(if_expr2)

print(generated_code_stmt1)
print(generated_code_stmt2)
print(generated_code_expr1)
print(generated_code_expr2)

Output of print(generated_code_stmt1):

if (1 > 2):
    (2 + 3)
else:
    (2 - 3)

Output of print(generated_code_stmt2):

if (1 > 2):
    (2 + 3)

Output of print(generated_code_expr1):

    (2 + 3) if  (1 > 2) else     (2 - 3)

Output of print(generated_code_expr2):

    (2 + 3) if  (1 > 2)

@apkrelling apkrelling marked this pull request as ready for review November 18, 2024 16:25
@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 19, 2024

I updated the transpiler method for IfExpr, and now the outputs are:

Output of print(generated_code_expr1):

    (2 + 3) if  (1 > 2) else     (2 - 3)

Output of print(generated_code_expr2):

    (2 + 3) if  (1 > 2) else None

Copy link
Contributor

@xmnlab xmnlab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT, thanks, @apkrelling !

@xmnlab xmnlab merged commit 4301e2a into arxlang:main Nov 19, 2024
12 checks passed
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.

IfExpr - Represents an if expression (e.g., in Rust).
2 participants