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 WhileExpr class #142

Merged
merged 3 commits into from
Nov 18, 2024
Merged

feat: create WhileExpr class #142

merged 3 commits into from
Nov 18, 2024

Conversation

apkrelling
Copy link
Contributor

@apkrelling apkrelling commented Nov 15, 2024

Pull Request description

This PR adds WhileExpr and modifies While -> WhileStmt.
This PR is an attempt to resolve #96 .

  • added new ASTKind
  • created WhileExpr class
  • updated __init__.py to include the new imports and classes in the __all__ list
  • added test for WhileExpr
  • added test for WhileStmt (there weren't any)
  • added transpiler visit methods for WhileStmt and WhileExpr
  • added transpiler tests for WhileStmt and WhileExpr

How to test these changes

from astx.flows import WhileExpr
from astx.blocks import Block
from astx.variables import VariableAssignment, Variable
from astx.operators import BinaryOp
from astx.base import SourceLocation
from astx.datatypes import Int32, LiteralInt32
from astx.transpilers import python as astx2py

# Define a condition: x < 5
x_var = Variable(name="x")
condition = BinaryOp(
    op_code="<",
    lhs=x_var,
    rhs=LiteralInt32(5),
    loc=SourceLocation(line=1, col=0)
)

# Define the loop body: x = x + 1
update_expr = VariableAssignment(
    name="x",
    value=BinaryOp(
        op_code="+",
        lhs=x_var,
        rhs=LiteralInt32(1),
        loc=SourceLocation(line=2, col=4)
    ),
    loc=SourceLocation(line=2, col=4)
)

# Create the body block
body_block = Block(name="while_body")
body_block.append(update_expr)

# Create the WhileExpr
while_expr = WhileExpr(
    condition=condition,
    body=body_block,
    loc=SourceLocation(line=1, col=0)
)

# AST representation
while_expr
while_expr.__str__()

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

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

while_expr_ascii
while_expr_png

Output of while_expr.__str__(): WhileExpr[BinaryOp[<](Variable[x],LiteralInt32(5))]

Output of the transpiler: [ x = (x + 1) for _ in iter(lambda: (x < 5), False)]

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

@apkrelling apkrelling marked this pull request as draft November 15, 2024 22:23
@xmnlab
Copy link
Contributor

xmnlab commented Nov 15, 2024

this is an initial idea for both whileexpr and whilestmt:

@dispatch
def visit(self, node: astx.WhileExpr) -> str:
    """Handle WhileExpr nodes."""
    condition = self.visit(node.condition)
    body = self.visit(node.body)
    return f"[{body} for _ in iter(lambda: {condition}, False)]"

@dispatch
def visit(self, node: astx.WhileStmt) -> str:
    """Handle WhileStmt nodes."""
    condition = self.visit(node.condition)
    body = self._generate_block(node.body)
    return f"while {condition}:\n{body}"

this idea is from gpt .. looks promising

@apkrelling
Copy link
Contributor Author

apkrelling commented Nov 15, 2024

Code for testing the transpiler for WhileStmt:

from astx.flows import WhileStmt
from astx.blocks import Block
from astx.variables import VariableAssignment, Variable
from astx.operators import BinaryOp
from astx.base import SourceLocation
from astx.datatypes import LiteralInt32
from astx.transpilers import python as astx2py

# Define a condition: x < 5
x_var = Variable(name="x")
condition = BinaryOp(
    op_code="<",
    lhs=x_var,
    rhs=LiteralInt32(5),
    loc=SourceLocation(line=1, col=0)
)

# Define the loop body: x = x + 1
update_expr = VariableAssignment(
    name="x",
    value=BinaryOp(
        op_code="+",
        lhs=x_var,
        rhs=LiteralInt32(1),
        loc=SourceLocation(line=2, col=4)
    ),
    loc=SourceLocation(line=2, col=4)
)

# Create the body block
body_block = Block(name="while_body")
body_block.append(update_expr)

while_stmt = WhileStmt(
    condition=condition,
    body=body_block,
    loc=SourceLocation(line=1, col=0)
)

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

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

Output:

while (x < 5):
    x = (x + 1)

@apkrelling apkrelling marked this pull request as ready for review November 16, 2024 13:37
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.

lgtm! thanks for working on that @apkrelling

@xmnlab xmnlab merged commit e94ca1a into arxlang:main Nov 18, 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.

WhileExpr - Represents a while loop.
2 participants