Skip to content

added basic ASTX to python transpiler #278

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nagajaideep
Copy link
Contributor

Pull Request Description

This pull request introduces a new feature: an ASTx to Python AST transpiler. The ASTxPythonASTTranspiler class enables the conversion of ASTx nodes into Python AST nodes using Python's built-in ast module. This allows for programmatic manipulation of ASTx nodes and seamless integration with Python's AST ecosystem.

Changes Made

  • Added ASTxPythonASTTranspiler class in libs/astx-transpilers/src/astx_transpilers/python_ast.py.
  • Implemented visit_* methods for several ASTx node types:
    • LiteralInt32, Variable
    • BinaryOp
    • Assign, If, While, and more
  • Added fallback support using ast.unparse for unsupported or complex cases.

Issue This PR Aims to Resolve

This PR introduces a new feature and does not resolve an existing issue directly.


How to Test These Changes

You can run the following Python script to test the transpiler functionality:

from astx_transpilers.python_ast import ASTxPythonASTTranspiler
import astx
import ast

# Example ASTx node
astx_node = astx.BinaryOp(
    lhs=astx.LiteralInt32(value=5),
    op_code="+",
    rhs=astx.Variable(name="x")
)

# Transpile to Python AST
transpiler = ASTxPythonASTTranspiler()
python_ast_node = transpiler.visit(astx_node)

# Print the Python AST node
print(ast.dump(python_ast_node))

# Convert to Python code
python_code = ast.unparse(python_ast_node)
print(python_code)

Expected Output:

BinOp(left=Constant(value=5), op=Add(), right=Name(id='x', ctx=Load()))
5 + x

Additionally, run any included unit tests to validate correctness.


Pull Request Checklists

This PR is a:

  • new feature
  • bug-fix
  • 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.

Optional:

  • Share updated images of the graph representation of the new ASTx node proposed in this PR, in both image and ASCII formats. For more information, refer to this Colab notebook

Additional Information

Example Output:

BinOp(left=Constant(value=5), op=Add(), right=Name(id='x', ctx=Load()))
5 + x

Reviewer's Checklist

Copy and paste this template when reviewing:

## 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.

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.BinaryOp) -> ast.BinOp:
"""Handle BinaryOp nodes."""
op_map = {
Copy link
Contributor

@xmnlab xmnlab Jun 4, 2025

Choose a reason for hiding this comment

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

this variable should be moved to outside of the function nd class, otherwise it will be created every time it is executed.

it could be called OP_MAP

@dispatch # type: ignore[no-redef]
def visit(self, node: astx.AugAssign) -> ast.AugAssign:
"""Handle AugAssign nodes."""
op_map = {
Copy link
Contributor

Choose a reason for hiding this comment

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

this variable should be moved to outside of the function nd class, otherwise it will be created every time it is executed.

it could be called OP_MAP

value = self.visit(node.value)
return ast.YieldFrom(value=value)

# For nodes that aren't directly implemented, use the unparse approach
Copy link
Contributor

Choose a reason for hiding this comment

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

not really sure about this .. but we can keep it for now

@xmnlab
Copy link
Contributor

xmnlab commented Jun 4, 2025

just a few comments, in general looks good to me.
you can proceed with tests and docs.

appreciate it!

Copy link

This pull request has been marked as stale because it has been
inactive for more than 5 days. Please update this pull request
or it will be automatically closed in 5 days.

@github-actions github-actions bot added the Stale label Jun 11, 2025
@nagajaideep
Copy link
Contributor Author

TASKS TO BE DONE:
-NEED TO ADD COMPLETE NODE TRANSLATIONS
-WRITE APPROPRIATE TESTS FOR NODES (WITH SYMMETRY)
-DOCUMENT THE NEW TRANSPILER

@github-actions github-actions bot removed the Stale label Jun 12, 2025
Copy link

This pull request has been marked as stale because it has been
inactive for more than 5 days. Please update this pull request
or it will be automatically closed in 5 days.

@github-actions github-actions bot added the Stale label Jun 17, 2025
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