-
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: create ForRangeExpr class #133
Conversation
The AST png and ascii representations for the code: from astx.variables import Variable, InlineVariableDeclaration
from astx.blocks import Block
from astx.flows import ForRangeLoop, ForRangeExpr
from astx.datatypes import LiteralInt32
from astx.datatypes import Int32
from astx.modifiers import MutabilityKind
# Create a range expression from 0 to 10 with step 1
range_expr = ForRangeExpr(
start=LiteralInt32(0),
end=LiteralInt32(6),
step=LiteralInt32(1)
)
# Variable declaration for the loop variable
loop_var = InlineVariableDeclaration(
name="i",
type_=Int32,
mutability=MutabilityKind.mutable,
value=LiteralInt32(1),
)
loop_var2 = Variable(name="i")
# Loop body
loop_body = Block(name="loop_body")
# Add statements to the loop body as needed
lit_2 = LiteralInt32(2)
basic_op = loop_var2 * lit_2
loop_body.append(basic_op)
# Create the ForRangeLoop using the range expression
for_loop = ForRangeLoop(
variable=loop_var,
range_expr=range_expr,
body=loop_body,
) |
Example usage for the transpiler: # Create a ForRangeExpr node
range_expr = astx.ForRangeExpr(
start=astx.LiteralInt32(0),
end=astx.LiteralInt32(10),
step=astx.LiteralInt32(1)
)
# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()
# Generate Python code
generated_code = generator.visit(range_expr)
generated_code output: |
99b1ab3
to
89336e4
Compare
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Please note that on the issue I put a comment mentioning that the output of the transpiler should be something like |
@xmn, after our discussions, here is the update for this PR:
|
@xmnlab , I noticed that
|
Code used to test these changes: from astx.variables import Variable, InlineVariableDeclaration
from astx.blocks import Block
from astx.flows import ForRangeLoopExpr
from astx.datatypes import LiteralInt32
from astx.datatypes import Int32
from astx.modifiers import MutabilityKind
import astx
from astx.transpilers import python as astx2py
# Variable declaration for the loop variable
loop_var = InlineVariableDeclaration(
name="i",
type_=Int32,
mutability=MutabilityKind.mutable,
value=LiteralInt32(1),
)
loop_var2 = Variable(name="i")
# Loop body
loop_body = Block(name="loop_body")
# Add statements to the loop body as needed
lit_2 = LiteralInt32(2)
basic_op = loop_var2 * lit_2
loop_body.append(basic_op)
# Create a range expression from 0 to 6 with step 1
for_expr = ForRangeLoopExpr(
variable=loop_var,
start=LiteralInt32(0),
end=LiteralInt32(6),
step=LiteralInt32(1),
body=loop_body
)
for_expr
generator = astx2py.ASTxPythonTranspiler()
generated_code = generator.visit(for_expr)
print(generated_code) |
thank you @apkrelling ! I am merging this PR, and I will play a bit with that and maybe open a follow-up PR with some small changes and I will fix the notebook as well. thanks, and sorry for messing with the issue XD |
🎉 This PR is included in version 0.16.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Pull Request description
This PR adds ForRangeExpr and modifies ForRangeLoop.
This PR is an attempt to solve #94 .
__init__.py
to include the new imports and classes in the__all__
listtranspilers/python.py
tests/transpilers/test_python.py
tests/test_flows.py
How to test these changes
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: