Skip to content

Commit abbacec

Browse files
authored
docs: Implement transpiler documentation improvements (#288)
1 parent 4bc5008 commit abbacec

File tree

6 files changed

+341
-0
lines changed

6 files changed

+341
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ ENV/
9898
# IDE settings
9999
.vscode/
100100
.idea/
101+
102+
# Ignore build artifacts and copied files
103+
dist/
104+
build/
105+
*.egg-info/

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ example:
133133
>>> print(add_function.to_yaml())
134134
```
135135

136+
## Transpilers
137+
138+
ASTx includes a powerful transpiler system to convert your AST structures into
139+
executable Python code. This is great for code generation, prototyping, or
140+
building custom language tools.
141+
142+
```python
143+
from astx_transpilers.python_string import ASTxPythonTranspiler
144+
145+
# Using the 'add_function' ASTx node from the example above
146+
transpiler = ASTxPythonTranspiler()
147+
python_code = transpiler.visit(add_function)
148+
print(python_code)
149+
```
150+
151+
**Output:**
152+
153+
```python
154+
def add(x: int, y: int) -> int:
155+
return (x + y)
156+
```
157+
158+
For a deep dive into the architecture and more hands-on examples, check out the
159+
**[full transpiler tutorial](docs/tutorials/astx_transpiler_refactor_tutorial.md)**.
160+
136161
---
137162

138163
## 📚 Documentation

docs/index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ to translate Abstract Syntax Trees (AST) into LLVM-IR. This showcases the
4747
library's adaptability and potential to serve as a foundational layer in various
4848
applications.
4949

50+
---
51+
52+
## New Transpiler System
53+
54+
ASTx includes a powerful and redesigned transpiler system that allows you to
55+
convert ASTx nodes into Python code. The new architecture separates the process
56+
into two distinct steps:
57+
58+
1. **ASTx → Python AST Objects**: Converts ASTx nodes into Python's built-in AST
59+
objects.
60+
2. **ASTx → Python Source Code**: Converts ASTx nodes directly into executable
61+
Python code.
62+
63+
This separation improves maintainability, eliminates circular dependencies, and
64+
makes the transpiler system more extensible.
65+
66+
For detailed usage instructions and examples, check out the
67+
**[Transpiler Tutorial](tutorials/astx_transpiler_refactor_tutorial.md)**.
68+
69+
---
70+
5071
## Why Choose ASTx?
5172

5273
ASTx is not just a library; it's a robust framework that fosters creativity and
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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`

libs/astx-transpilers/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ example:
133133
>>> print(add_function.to_yaml())
134134
```
135135

136+
## Transpilers
137+
138+
ASTx includes a powerful transpiler system to convert your AST structures into
139+
executable Python code. This is great for code generation, prototyping, or
140+
building custom language tools.
141+
142+
```python
143+
from astx_transpilers.python_string import ASTxPythonTranspiler
144+
145+
# Using the 'add_function' ASTx node from the example above
146+
transpiler = ASTxPythonTranspiler()
147+
python_code = transpiler.visit(add_function)
148+
print(python_code)
149+
```
150+
151+
**Output:**
152+
153+
```python
154+
def add(x: int, y: int) -> int:
155+
return (x + y)
156+
```
157+
158+
For a deep dive into the architecture and more hands-on examples, check out the
159+
**[full transpiler tutorial](docs/tutorials/astx_transpiler_refactor_tutorial.md)**.
160+
136161
---
137162

138163
## 📚 Documentation

libs/astx/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ example:
133133
>>> print(add_function.to_yaml())
134134
```
135135

136+
## Transpilers
137+
138+
ASTx includes a powerful transpiler system to convert your AST structures into
139+
executable Python code. This is great for code generation, prototyping, or
140+
building custom language tools.
141+
142+
```python
143+
from astx_transpilers.python_string import ASTxPythonTranspiler
144+
145+
# Using the 'add_function' ASTx node from the example above
146+
transpiler = ASTxPythonTranspiler()
147+
python_code = transpiler.visit(add_function)
148+
print(python_code)
149+
```
150+
151+
**Output:**
152+
153+
```python
154+
def add(x: int, y: int) -> int:
155+
return (x + y)
156+
```
157+
158+
For a deep dive into the architecture and more hands-on examples, check out the
159+
**[full transpiler tutorial](docs/tutorials/astx_transpiler_refactor_tutorial.md)**.
160+
136161
---
137162

138163
## 📚 Documentation

0 commit comments

Comments
 (0)