You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**IRPrinter.**By defining an `__ir_print__` method, which converts an IR node to MLC's Python-style AST, MLC's `IRPrinter` handles variable scoping, renaming and syntax highlighting automatically for a text format based on Python syntax.
124
+
**Printer.**MLCconverts an IR node to Python AST by looking up the `__ir_print__` method.
125
125
126
-
<details><summary>Defining Python-based text format on a toy IRusing `__ir_print__`.</summary>
126
+
**[[Example](https://github.com/mlc-ai/mlc-python/blob/main/python/mlc/testing/toy_ir/ir.py)]**. Copy the toy IRdefinition to REPLand then create a `Func` node below:
args=[mlt.Assign(lhs=arg, rhs=None) for arg in args],
182
-
decorators=[],
183
-
return_type=None,
184
-
body=[*stmts, ret_stmt],
185
-
)
186
-
187
-
# An example IR:
188
-
a, b, c, d, e = Var("a"), Var("b"), Var("c"), Var("d"), Var("e")
189
-
f = Func(
190
-
name="f",
191
-
args=[a, b, c],
129
+
>>> a, b, c, d, e = Var("a"), Var("b"), Var("c"), Var("d"), Var("e")
130
+
>>> f = Func("f", [a, b, c],
192
131
stmts=[
193
132
Assign(lhs=d, rhs=Add(a, b)), # d = a + b
194
133
Assign(lhs=e, rhs=Add(d, c)), # e = d + c
195
134
],
196
-
ret=e,
197
-
)
135
+
ret=e)
198
136
```
199
137
200
-
</details>
201
-
202
-
Two printer APIs are provided for Python-based text format:
203
-
-`mlc.printer.to_python` that converts an IR fragment to Python text, and
204
-
-`mlc.printer.print_python` that further renders the text with proper syntax highlighting.
138
+
- Method `mlc.printer.to_python` converts an IR node to Python-based text;
205
139
206
140
```python
207
141
>>>print(mlcp.to_python(f)) # Stringify to Python
208
142
def f(a, b, c):
209
143
d = a + b
210
144
e = d + c
211
145
return e
146
+
```
147
+
148
+
- Method `mlc.printer.print_python` further renders the text with proper syntax highlighting. [[Screenshot](https://raw.githubusercontent.com/gist/potatomashed/5a9b20edbdde1b9a91a360baa6bce9ff/raw/3c68031eaba0620a93add270f8ad7ed2c8724a78/mlc-python-printer.svg)]
149
+
150
+
```python
212
151
>>> mlcp.print_python(f) # Syntax highlighting
213
152
```
214
153
154
+
**AST Parser.**MLC has a concise set of APIs for implementing parser with Python's AST module, including:
155
+
- Inspection API that obtains source code of a Python classor function and the variables they capture;
156
+
- Variable management APIs that helpwith proper scoping;
157
+
-AST fragment evaluation APIs;
158
+
- Error rendering APIs.
159
+
160
+
**[[Example](https://github.com/mlc-ai/mlc-python/blob/main/python/mlc/testing/toy_ir/parser.py)]**. With MLC APIs, a parser can be implemented with100 lines of code for the Python text format above defined by `__ir_printer__`.
161
+
215
162
### :zap: Zero-Copy Interoperability with C++ Plugins
0 commit comments