-
Notifications
You must be signed in to change notification settings - Fork 4
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
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
hi @apkrelling, about the output from the transpiler for the maybe you can use this as a reference:
|
for if stmt, the result from the transpiler would look like this: if (1 > 2):
x = (2 + 3)
else:
x = (2 - 3)' |
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
Output of
Output of
Output of
|
I updated the transpiler method for Output of
Output of
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGMT, thanks, @apkrelling !
🎉 This PR is included in version 0.16.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Pull Request description
This PR adds
IfExpr
and modifiesIf
->IfStmt
.This PR is an attempt to resolve #93 .
IfExpr
class__init__.py
to include the new imports and classes in the__all__
listIfExpr
IfExpr
IfExpr
IfStmt
IfStmt
How to test these changes
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:
About this PR:
Author's checklist:
complexity.
Additional information
Reviewer's checklist
Copy and paste this template for your review's note: