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: create ForRangeExpr class #133

Merged
merged 15 commits into from
Nov 10, 2024
Merged

Conversation

apkrelling
Copy link
Contributor

@apkrelling apkrelling commented Oct 28, 2024

Pull Request description

This PR adds ForRangeExpr and modifies ForRangeLoop.
This PR is an attempt to solve #94 .

  • added new ASTKind
  • created ForRangeExpr class
  • modified ForRangeLoop class to be compatible with ForRangeExpr
  • updated __init__.py to include the new imports and classes in the __all__ list
  • added the new classes visit methods in transpilers/python.py
  • added test in tests/transpilers/test_python.py
  • added test in tests/test_flows.py

How to test these changes

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 6 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,
)

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 October 28, 2024 21:21
@apkrelling
Copy link
Contributor Author

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

are:
for_range_loop_jupyterlab
for_range_loop_ipython
for_range_expr_ipython
for_range_expr_jupyterlab

@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 2, 2024

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: range(0,10,1)

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 2, 2024

Please note that on the issue I put a comment mentioning that the output of the transpiler should be something like result = [x * 2 for x in range(1,6)]. However, this should be the output for the ForRangeLoop (which is a Stmt), and not for the ForRangeExpr. Please let me know if it would be the case to create a new issue to set up the transpiler visit method for ForRangeLoop.

@apkrelling apkrelling marked this pull request as ready for review November 2, 2024 14:55
@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 8, 2024

@xmn, after our discussions, here is the update for this PR:

  • added new ASTKind
  • created ForRangeExpr class
  • got ForRangeLoop back to its original code
  • changed class names: ForRangeLoop -> ForRangeLoopStmt, ForRangeExpr -> ForRangeLoopExpr
  • updated __init__.py to include the new imports and classes in the __all__ list
  • edited tests in tests/test_flows.py

@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 8, 2024

@xmnlab , I noticed that

@apkrelling
Copy link
Contributor Author

ascii representation after changes:
new_astx
png representation after changes:
new_astx_png
Output of the transpiler method:
result = [ (i * 2) for i in range (0,6,1)]

@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 8, 2024

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)

@xmnlab
Copy link
Contributor

xmnlab commented Nov 10, 2024

thank you @apkrelling !
That looks good!

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

@xmnlab xmnlab merged commit 5fb1805 into arxlang:main Nov 10, 2024
3 of 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.

2 participants