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

Add support for transpiler from ASTx to Python built-in AST #172

Open
1 task done
xmnlab opened this issue Feb 7, 2025 · 0 comments
Open
1 task done

Add support for transpiler from ASTx to Python built-in AST #172

xmnlab opened this issue Feb 7, 2025 · 0 comments

Comments

@xmnlab
Copy link
Contributor

xmnlab commented Feb 7, 2025

Summary

Add support for transpiler from ASTx to Python built-in AST.

Example for how to use Python ast:

import ast

# Create an if-else structure programmatically
if_stmt = ast.If(
    test=ast.Compare(
        left=ast.Name(id="x", ctx=ast.Load()),  # x
        ops=[ast.Gt()],                         # >
        comparators=[ast.Constant(value=10)],   # 10
    ),
    body=[ast.Expr(
        value=ast.Call(
            func=ast.Name(id="print", ctx=ast.Load()), 
            args=[ast.Constant(value="x is greater than 10")],
            keywords=[]
        )
    )],
    orelse=[ast.Expr(
        value=ast.Call(
            func=ast.Name(id="print", ctx=ast.Load()), 
            args=[ast.Constant(value="x is 10 or less")],
            keywords=[]
        )
    )]
)

# Wrap in a Module node
module = ast.Module(body=[if_stmt], type_ignores=[])

# Convert to source code
new_code = ast.unparse(module)
print(new_code)

The current transpiler from ASTx to Python Source can be use as a reference: https://github.com/arxlang/astx/blob/main/src/astx/tools/transpilers/python.py

Additional Information

Code of Conduct

  • I agree to follow the Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant