|
| 1 | +# ASTx Transpiler Tutorial |
| 2 | + |
| 3 | +This guide walks you through using ASTx's transpiler system to convert your AST |
| 4 | +structures into Python code. We've redesigned the transpiler with a cleaner |
| 5 | +architecture that's easier to use and maintain. |
| 6 | + |
| 7 | +## How the Transpiler Works |
| 8 | + |
| 9 | +The transpiler follows a straightforward two-step approach: |
| 10 | + |
| 11 | +### Step 1: ASTx → Python AST Objects |
| 12 | + |
| 13 | +The `ASTxPythonASTTranspiler` converts your ASTx nodes into Python's built-in |
| 14 | +AST objects. This is useful when you need to work with the AST programmatically. |
| 15 | + |
| 16 | +```python |
| 17 | +from astx_transpilers.python_to_ast import ASTxPythonASTTranspiler |
| 18 | + |
| 19 | +# Get a Python AST object |
| 20 | +ast_transpiler = ASTxPythonASTTranspiler() |
| 21 | +python_ast = ast_transpiler.visit(your_astx_node) |
| 22 | +``` |
| 23 | + |
| 24 | +### Step 2: ASTx → Python Source Code |
| 25 | + |
| 26 | +The `ASTxPythonTranspiler` takes your ASTx nodes and produces clean, readable |
| 27 | +Python source code. Behind the scenes, it uses the AST transpiler and then |
| 28 | +converts to string format. |
| 29 | + |
| 30 | +```python |
| 31 | +from astx_transpilers.python_string import ASTxPythonTranspiler |
| 32 | + |
| 33 | +# Get Python source code as a string |
| 34 | +string_transpiler = ASTxPythonTranspiler() |
| 35 | +python_code = string_transpiler.visit(your_astx_node) |
| 36 | +``` |
| 37 | + |
| 38 | +## What's Better About This Approach |
| 39 | + |
| 40 | +- **No more circular imports**: Each component has a single, clear job |
| 41 | +- **Easier debugging**: Problems in AST generation don't affect string |
| 42 | + formatting |
| 43 | +- **More flexible**: Use AST objects directly or get formatted strings |
| 44 | +- **Cleaner code**: Each module focuses on one thing and does it well |
| 45 | + |
| 46 | +## Testing Your Setup |
| 47 | + |
| 48 | +Make sure everything works by running the test suite: |
| 49 | + |
| 50 | +```bash |
| 51 | +python -m pytest libs/astx-transpilers/tests/ -v |
| 52 | +``` |
| 53 | + |
| 54 | +## Usage Examples |
| 55 | + |
| 56 | +### Example 1: Simple Integer Literal |
| 57 | + |
| 58 | +**Code:** |
| 59 | + |
| 60 | +```python |
| 61 | +import astx |
| 62 | +from astx_transpilers.python_to_ast import ASTxPythonASTTranspiler |
| 63 | +from astx_transpilers.python_string import ASTxPythonTranspiler |
| 64 | + |
| 65 | +# Create an ASTx integer literal |
| 66 | +astx_node = astx.LiteralInt32(value=42) |
| 67 | + |
| 68 | +# Convert to Python AST |
| 69 | +python_ast = ASTxPythonASTTranspiler().visit(astx_node) |
| 70 | +print(python_ast) |
| 71 | + |
| 72 | +# Convert to Python source code string |
| 73 | +code_str = ASTxPythonTranspiler().visit(astx_node) |
| 74 | +print(code_str) |
| 75 | +``` |
| 76 | + |
| 77 | +**Output:** |
| 78 | + |
| 79 | +``` |
| 80 | +<_ast.Constant value=42> |
| 81 | +42 |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### Example 2: Lambda Expression |
| 87 | + |
| 88 | +**Code:** |
| 89 | + |
| 90 | +```python |
| 91 | +import astx |
| 92 | +from astx_transpilers.python_string import ASTxPythonTranspiler |
| 93 | + |
| 94 | +# Create lambda: lambda x: x + 1 |
| 95 | +params = astx.Arguments(astx.Argument(name="x", type_=astx.Int32())) |
| 96 | +body = astx.BinaryOp(op_code="+", lhs=astx.Variable(name="x"), rhs=astx.LiteralInt32(1)) |
| 97 | +lambda_node = astx.LambdaExpr(params=params, body=body) |
| 98 | + |
| 99 | +# Convert to Python source code |
| 100 | +transpiler = ASTxPythonTranspiler() |
| 101 | +code_str = transpiler.visit(lambda_node) |
| 102 | +print(code_str) |
| 103 | +``` |
| 104 | + |
| 105 | +**Output:** |
| 106 | + |
| 107 | +```python |
| 108 | +lambda x: x + 1 |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +### Example 3: Mathematical Expression |
| 114 | + |
| 115 | +**Code:** |
| 116 | + |
| 117 | +```python |
| 118 | +import astx |
| 119 | +from astx_transpilers.python_string import ASTxPythonTranspiler |
| 120 | + |
| 121 | +# Create: (5 + 3) * 2 |
| 122 | +left_expr = astx.BinaryOp( |
| 123 | + op_code="+", |
| 124 | + lhs=astx.LiteralInt32(5), |
| 125 | + rhs=astx.LiteralInt32(3) |
| 126 | +) |
| 127 | +math_expr = astx.BinaryOp( |
| 128 | + op_code="*", |
| 129 | + lhs=left_expr, |
| 130 | + rhs=astx.LiteralInt32(2) |
| 131 | +) |
| 132 | + |
| 133 | +transpiler = ASTxPythonTranspiler() |
| 134 | +result = transpiler.visit(math_expr) |
| 135 | +print(result) |
| 136 | +``` |
| 137 | + |
| 138 | +**Output:** |
| 139 | + |
| 140 | +``` |
| 141 | +((5 + 3) * 2) |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +### Example 4: Function Definition |
| 147 | + |
| 148 | +**Code:** |
| 149 | + |
| 150 | +```python |
| 151 | +import astx |
| 152 | +from astx_transpilers.python_string import ASTxPythonTranspiler |
| 153 | + |
| 154 | +# Create function: def add(x, y): return x + y |
| 155 | +args = astx.Arguments( |
| 156 | + astx.Argument(name="x", type_=astx.Int32()), |
| 157 | + astx.Argument(name="y", type_=astx.Int32()), |
| 158 | +) |
| 159 | +fn_body = astx.Block() |
| 160 | +fn_body.append( |
| 161 | + astx.FunctionReturn( |
| 162 | + value=astx.BinaryOp( |
| 163 | + op_code="+", |
| 164 | + lhs=astx.Variable("x"), |
| 165 | + rhs=astx.Variable("y") |
| 166 | + ) |
| 167 | + ) |
| 168 | +) |
| 169 | +add_function = astx.FunctionDef( |
| 170 | + prototype=astx.FunctionPrototype( |
| 171 | + name="add", |
| 172 | + args=args, |
| 173 | + return_type=astx.Int32() |
| 174 | + ), |
| 175 | + body=fn_body, |
| 176 | +) |
| 177 | + |
| 178 | +transpiler = ASTxPythonTranspiler() |
| 179 | +code_str = transpiler.visit(add_function) |
| 180 | +print(code_str) |
| 181 | +``` |
| 182 | + |
| 183 | +**Output:** |
| 184 | + |
| 185 | +```python |
| 186 | +def add(x: int, y: int) -> int: |
| 187 | + return (x + y) |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +### Example 5: Import Statement |
| 193 | + |
| 194 | +**Code:** |
| 195 | + |
| 196 | +```python |
| 197 | +import astx |
| 198 | +from astx_transpilers.python_string import ASTxPythonTranspiler |
| 199 | + |
| 200 | +# Create: import os, sys |
| 201 | +import_stmt = astx.ImportStmt( |
| 202 | + names=[ |
| 203 | + astx.AliasExpr(name="os"), |
| 204 | + astx.AliasExpr(name="sys") |
| 205 | + ] |
| 206 | +) |
| 207 | + |
| 208 | +transpiler = ASTxPythonTranspiler() |
| 209 | +code_str = transpiler.visit(import_stmt) |
| 210 | +print(code_str) |
| 211 | +``` |
| 212 | + |
| 213 | +**Output:** |
| 214 | + |
| 215 | +```python |
| 216 | +import os, sys |
| 217 | +``` |
| 218 | + |
| 219 | +## What You Get |
| 220 | + |
| 221 | +- **AST Output:** Valid Python AST objects that can be further processed |
| 222 | +- **String Output:** Clean Python source code ready to execute or save to files |
| 223 | + |
| 224 | +## Implementation Details |
| 225 | + |
| 226 | +The refactored transpiler provides better code organization and eliminates |
| 227 | +previous circular dependency issues. Each component now has a single, clear |
| 228 | +purpose. |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | +**Source Files:** |
| 233 | + |
| 234 | +- `libs/astx-transpilers/src/astx_transpilers/python_to_ast.py` |
| 235 | +- `libs/astx-transpilers/src/astx_transpilers/python_string.py` |
| 236 | + |
| 237 | +**Tests:** |
| 238 | + |
| 239 | +- `libs/astx-transpilers/tests/test_python_to_ast.py` |
| 240 | +- `libs/astx-transpilers/tests/test_python_string.py` |
0 commit comments