Merged
feat(IR): Add normalized PilCode #5470
feat(IR): Add normalized PilCode #5470
Merged
Jie YinAdd on Aug 17
Total: 17 files changed+3611-2347
@@ -0,0 +1,27 @@
1+#!/usr/bin/env python3
2+# coding: utf-8
3+# Copyright (c) 2026 CANN community contributors.
4+# This program is free software; you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# -----------------------------------------------------------------------------------------------------------
11+ 
12+ 
13+from collections import UserDict
14+ 
15+ 
16+class KindDict(UserDict):
17+ 
18+ def __init__(self):
19+ super().__init__()
20+ 
21+ def __call__(self, kind):
22+ def wrapper(cls):
23+ assert kind not in self.data, f'Kind conflict: {kind}'
24+ self.data[kind] = cls
25+ cls.kind = kind
26+ return cls
27+ return wrapper
@@ -73,4 +73,4 @@ For detailed developer documentation, see developer_doc.md in the frontend direc
73 73 
74from ... import error # noqa: F40174from ... import error # noqa: F401
75from .entry import function, jit # noqa: F40175from .entry import function, jit # noqa: F401
76-from .pil import parse_stmts # noqa: F40176+from .pil_parser import parse_stmts # noqa: F401
@@ -0,0 +1,340 @@
1+#!/usr/bin/env python3
2+# coding: utf-8
3+# Copyright (c) 2026 CANN community contributors.
4+# This program is free software; you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# -----------------------------------------------------------------------------------------------------------
11+"""Factory layer building PIL data-model nodes (pil.py) from simplified pieces.
12+ 
13+Mirrors the ``create_pil_XXX`` surface of ``PILBuilder`` in pil_parser.py,
14+but returns PilCode subclasses instead of ast nodes.
15+"""
16+ 
17+import ast
18+from typing import Optional
19+ 
20+from pypto.error import FeError
21+ 
22+from .pil import (
23+ PIL_BOP_TO_CALLEE,
24+ PIL_CALLEE_DICT_OP,
25+ PIL_CALLEE_JOINED_STR,
26+ PIL_CALLEE_LIST,
27+ PIL_CALLEE_SET_OP,
28+ PIL_CALLEE_TUPLE,
29+ PIL_CALLEE_YIELD,
30+ PIL_CALLEE_YIELD_FROM,
31+ PIL_COP_TO_CALLEE,
32+ PIL_UOP_TO_CALLEE,
33+ PilAssert,
34+ PilAssignAttribute,
35+ PilAssignSubscript,
36+ PilAssignSymbolList,
37+ PilAttribute,
38+ PilBreak,
39+ PilCall,
40+ PilClassDef,
41+ PilConstantExpr,
42+ PilContinue,
43+ PilDeleteAttribute,
44+ PilDeleteIdentifier,
45+ PilDeleteSubscript,
46+ PilExpr,
47+ PilExprAssignSymbol,
48+ PilExprIndex,
49+ PilExprListItem,
50+ PilFor,
51+ PilFunctionDef,
52+ PilFunctionParameterDef,
53+ PilGlobal,
54+ PilIdentifierExpr,
55+ PilIf,
56+ PilImport,
57+ PilImportFrom,
58+ PilKeyword,
59+ PilNonlocal,
60+ PilParameter,
61+ PilPass,
62+ PilRaise,
63+ PilReturn,
64+ PilSlice,
65+ PilStarred,
66+ PilStmtList,
67+ PilSubscript,
68+ PilTry,
69+ PilWhile,
70+ PilWith,
71+)
72+ 
73+ 
74+def _checked_expr(value: PilExpr) -> PilExpr:
75+ """Validate a PilExpr; call sites must pass PilIdentifierExpr for identifiers."""
76+ if isinstance(value, PilExpr):
77+ return value
78+ raise FeError(TypeError(f"Expected PilExpr, but got {type(value).__name__}"))
79+ 
80+ 
81+def _checked_expr_or_none(value: Optional[PilExpr]) -> Optional[PilExpr]:
82+ return None if value is None else _checked_expr(value)
83+ 
84+ 
85+def _checked_index(value: PilExprIndex) -> PilExprIndex:
86+ """Validate an index slot (expr or slice)."""
87+ if isinstance(value, PilExprIndex):
88+ return value
89+ raise FeError(TypeError(f"Expected PilExprIndex, but got {type(value).__name__}"))
90+ 
91+ 
92+def _checked_assign_symbol(value: PilExprAssignSymbol) -> PilExprAssignSymbol:
93+ """Validate an assign-symbol slot (expr, attribute, subscript or call)."""
94+ if isinstance(value, PilExprAssignSymbol):
95+ return value
96+ raise FeError(TypeError(f"Expected PilExprAssignSymbol, but got {type(value).__name__}"))
97+ 
98+ 
99+def _checked_list_item(value: PilExprListItem) -> PilExprListItem:
100+ """Validate a list-item slot (expr or starred)."""
101+ if isinstance(value, PilExprListItem):
102+ return value
103+ raise FeError(TypeError(f"Expected PilExprListItem, but got {type(value).__name__}"))
104+ 
105+ 
106+class PilBuilder:
107+ """Builds PIL data-model nodes (PilCode subclasses) from simplified pieces."""
108+ 
109+ # ---- Expression factories ----
110+ 
111+ def create_constant(self, value: object, kind: Optional[str] = None, **_kwargs) -> PilConstantExpr:
112+ """Constant(constant value, string? kind)"""
113+ return PilConstantExpr(value, kind)
114+ 
115+ def create_name(self, identifier: str, **_kwargs) -> PilIdentifierExpr:
116+ """Name(identifier id)"""
117+ return PilIdentifierExpr(identifier)
118+ 
119+ def create_starred(self, value: str, **_kwargs) -> PilStarred:
120+ """Starred(identifier value)"""
121+ return PilStarred(value)
122+ 
123+ def create_slice(self, lower: Optional[PilExpr], upper: Optional[PilExpr],
124+ step: Optional[PilExpr], **_kwargs) -> PilSlice:
125+ """Slice(identifier? lower, identifier? upper, identifier? step)"""
126+ return PilSlice(lower, upper, step)
127+ 
128+ def create_attribute(self, value_expr: PilExpr, attr_name: str, **_kwargs) -> PilAttribute:
129+ """Attribute(expr target, identifier attr)"""
130+ return PilAttribute(_checked_expr(value_expr), attr_name)
131+ 
132+ def create_subscript(self, value_expr: PilExpr, indices: list[PilExprIndex],
133+ **_kwargs) -> PilSubscript:
134+ """Subscript(expr target, PilExprIndex* index)"""
135+ return PilSubscript(_checked_expr(value_expr),
136+ [_checked_index(i) for i in indices])
137+ 
138+ def create_bin_op(self, left_expr: PilExpr, op: ast.operator, right_expr: PilExpr, **_kwargs) -> PilCall:
139+ """BinOp(identifier left, operator op, identifier right) -> ``!bop.<op>`` call"""
140+ return PilCall(PIL_BOP_TO_CALLEE[type(op)], [_checked_expr(left_expr), _checked_expr(right_expr)])
141+ 
142+ def create_unary_op(self, op: ast.unaryop, operand_expr: PilExpr, **_kwargs) -> PilCall:
143+ """UnaryOp(unaryop op, identifier operand) -> ``!uop.<op>`` call"""
144+ return PilCall(PIL_UOP_TO_CALLEE[type(op)], [_checked_expr(operand_expr)])
145+ 
146+ def create_compare(self, left_expr: PilExpr, op: ast.cmpop, comparator_expr: PilExpr, **_kwargs) -> PilCall:
147+ """Compare(identifier left, cmpop op, identifier comparator) -> ``!cop.<op>`` call"""
148+ return PilCall(PIL_COP_TO_CALLEE[type(op)], [_checked_expr(left_expr), _checked_expr(comparator_expr)])
149+ 
150+ def create_dict(self, keys: list[Optional[PilExpr]], values: list[PilExpr],
151+ **_kwargs) -> PilCall:
152+ """Dict(identifier?* keys, identifier* values) -> ``!dict`` call
153+ 
154+ ``!dict`` args alternate ``key0?, value0, key1?, value1, ...``;
155+ a ``None`` key slot means ``**value`` unpacking.
156+ """
157+ if len(keys) != len(values):
158+ raise FeError(ValueError(f"dict keys/values length mismatch: {len(keys)} vs {len(values)}"))
159+ interleaved = []
160+ for key, value in zip(keys, values):
161+ interleaved.append(_checked_expr_or_none(key))
162+ interleaved.append(_checked_expr(value))
163+ return PilCall(PIL_CALLEE_DICT_OP, interleaved)
164+ 
165+ def create_set(self, elts: list[PilExprListItem], **_kwargs) -> PilCall:
166+ """Set(expr* elts) -> ``!set`` call"""
167+ return PilCall(PIL_CALLEE_SET_OP, [_checked_list_item(e) for e in elts])
168+ 
169+ def create_list(self, elts: list[PilExprListItem], **_kwargs) -> PilCall:
170+ """List(expr* elts) -> ``!list`` call"""
171+ return PilCall(PIL_CALLEE_LIST, [_checked_list_item(e) for e in elts])
172+ 
173+ def create_tuple(self, elts: list[PilExprListItem], **_kwargs) -> PilCall:
174+ """Tuple(expr* elts) -> ``!tuple`` call"""
175+ return PilCall(PIL_CALLEE_TUPLE, [_checked_list_item(e) for e in elts])
176+ 
177+ def create_call(self, func_expr: PilIdentifierExpr, args: list[PilExprListItem],
178+ keywords: list[PilKeyword], **_kwargs) -> PilCall:
179+ """Call(identifier func, identifier* args, keyword* keywords)"""
180+ return PilCall(func_expr.id, [_checked_list_item(a) for a in args], list(keywords))
181+ 
182+ def create_yield(self, expr: Optional[PilExpr], **_kwargs) -> PilCall:
183+ """Yield(identifier? value) -> ``!yield`` call"""
184+ return PilCall(PIL_CALLEE_YIELD, [_checked_expr(expr)] if expr is not None else [])
185+ 
186+ def create_yield_from(self, expr: PilExpr, **_kwargs) -> PilCall:
187+ """YieldFrom(identifier value) -> ``!yield_from`` call"""
188+ return PilCall(PIL_CALLEE_YIELD_FROM, [_checked_expr(expr)])
189+ 
190+ def create_joined_str(self, values: list[PilExpr], **_kwargs) -> PilCall:
191+ """JoinedStr(expr* values) -> ``!joined_str`` call"""
192+ return PilCall(PIL_CALLEE_JOINED_STR, [_checked_expr(v) for v in values])
193+ 
194+ # ---- Statement factories ----
195+ 
196+ def create_assign_identifier(self, target_name: str, value: PilExprAssignSymbol,
197+ type_comment: Optional[str] = None, **_kwargs) -> PilAssignSymbolList:
198+ """Assign(identifier target, expr value, string? type_comment) — single-name form"""
199+ return PilAssignSymbolList([PilIdentifierExpr(target_name)], _checked_assign_symbol(value), type_comment)
200+ 
201+ def create_assign_symbol_list(self, targets: list[PilExprListItem], value: PilExprAssignSymbol,
202+ type_comment: Optional[str] = None, **_kwargs) -> PilAssignSymbolList:
203+ """Assign(expr target*, expr value, string? type_comment) for multiple names"""
204+ return PilAssignSymbolList(
205+ [_checked_list_item(t) for t in targets], _checked_assign_symbol(value), type_comment)
206+ 
207+ def create_assign_attribute(self, target_expr: PilExpr, attr_name: str, value: PilExpr,
208+ type_comment: Optional[str] = None, **_kwargs) -> PilAssignAttribute:
209+ """Assign(target.attr = value, string? type_comment)"""
210+ return PilAssignAttribute(_checked_expr(target_expr), attr_name, _checked_expr(value), type_comment)
211+ 
212+ def create_assign_subscript(self, target_expr: PilExpr, indices: list[PilExprIndex],
213+ value: PilExpr, type_comment: Optional[str] = None,
214+ **_kwargs) -> PilAssignSubscript:
215+ """Assign(target[index] = value, string? type_comment)"""
216+ return PilAssignSubscript(_checked_expr(target_expr),
217+ [_checked_index(i) for i in indices],
218+ _checked_expr(value), type_comment)
219+ 
220+ def create_function_def(self, name: str, args, body: PilStmtList,
221+ decorator_list: list[PilExpr] = (),
222+ return_expr: Optional[PilExpr] = None,
223+ type_comment: Optional[str] = None,
224+ **_kwargs) -> PilFunctionDef:
225+ """FunctionDef(identifier name, PilFunctionParameterDef args, stmt* body,
226+ expr* decorator_list, expr? returns, string? type_comment)"""
227+ return PilFunctionDef(name, args, body, [_checked_expr(d) for d in decorator_list], return_expr, type_comment)
228+ 
229+ def create_class_def(self, name: str, bases: list[PilExprListItem],
230+ keywords: list, body: PilStmtList,
231+ decorator_list: list[PilExpr] = (), **_kwargs) -> PilClassDef:
232+ """ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)"""
233+ keywords = [kw if isinstance(kw, PilKeyword) else PilKeyword(kw[0], _checked_expr(kw[1])) for kw in keywords]
234+ return PilClassDef(name, [_checked_list_item(b) for b in bases], keywords, body,
235+ [_checked_expr(d) for d in decorator_list])
236+ 
237+ def create_return(self, expr: Optional[PilExpr], **_kwargs) -> PilReturn:
238+ """Return(identifier? value)"""
239+ return PilReturn(_checked_expr_or_none(expr))
240+ 
241+ def create_delete_identifier(self, target: PilIdentifierExpr, **_kwargs) -> PilDeleteIdentifier:
242+ """Delete(expr target) — identifier form"""
243+ return PilDeleteIdentifier(target)
244+ 
245+ def create_delete_attribute(self, target: PilIdentifierExpr, attr: str, **_kwargs) -> PilDeleteAttribute:
246+ """Delete(expr target, identifier attr)"""
247+ return PilDeleteAttribute(target, attr)
248+ 
249+ def create_delete_subscript(self, target: PilIdentifierExpr, indices: list[PilExprIndex],
250+ **_kwargs) -> PilDeleteSubscript:
251+ """Delete(expr target, PilExprIndex* index)"""
252+ return PilDeleteSubscript(target, [_checked_index(i) for i in indices])
253+ 
254+ def create_for(self, target: PilExpr, iter_expr: PilExpr, body: PilStmtList,
255+ orelse: PilStmtList, type_comment: Optional[str] = None, **_kwargs) -> PilFor:
256+ """For(identifier target, expr iter, stmt* body, stmt* orelse, string? type_comment)
257+ 
258+ A starred tuple target is decomposed into plain symbol + assign symbol list
259+ statements inside the loop body, so target here is always a plain PilExpr.
260+ """
261+ return PilFor(target, _checked_expr(iter_expr), body, orelse, type_comment)
262+ 
263+ def create_while(self, test_expr: PilExpr, body: PilStmtList, orelse: PilStmtList, **_kwargs) -> PilWhile:
264+ """While(identifier test, stmt* body, stmt* orelse)"""
265+ return PilWhile(_checked_expr(test_expr), body, orelse)
266+ 
267+ def create_if(self, test_expr: PilExpr, body: PilStmtList, orelse: PilStmtList, **_kwargs) -> PilIf:
268+ """If(identifier test, stmt* body, stmt* orelse)"""
269+ return PilIf(_checked_expr(test_expr), body, orelse)
270+ 
271+ def create_with(self, context_expr: PilExpr, body: PilStmtList,
272+ optional_vars: Optional[str] = None,
273+ type_comment: Optional[str] = None, **_kwargs) -> PilWith:
274+ """With(expr context_expr, stmt* body, identifier? optional_vars, string? type_comment)
275+ 
276+ One clause per statement; multiple context managers nest via body.
277+ """
278+ return PilWith(_checked_expr(context_expr), body, optional_vars, type_comment)
279+ 
280+ def create_raise(self, exc: Optional[PilExpr], cause: Optional[PilExpr], **_kwargs) -> PilRaise:
281+ """Raise(identifier? exc, identifier? cause)"""
282+ return PilRaise(_checked_expr_or_none(exc), _checked_expr_or_none(cause))
283+ 
284+ def create_try(self, body: PilStmtList, handler: tuple[Optional[str], PilStmtList],
285+ orelse: PilStmtList, finalbody: PilStmtList, **_kwargs) -> PilTry:
286+ """Try(stmt* body, (identifier? exc_var, stmt* handler_body), stmt* orelse, stmt* finalbody)"""
287+ return PilTry(body, handler, orelse, finalbody)
288+ 
289+ def create_assert(self, test_expr: PilExpr, msg: Optional[PilExpr], **_kwargs) -> PilAssert:
290+ """Assert(identifier test, identifier? msg)"""
291+ return PilAssert(_checked_expr(test_expr), _checked_expr_or_none(msg))
292+ 
293+ def create_import(self, name: str, asname: Optional[str] = None, **_kwargs) -> PilImport:
294+ """Import(identifier name, identifier? asname)"""
295+ return PilImport(name, asname)
296+ 
297+ def create_import_from(self, module: Optional[str], name: str, asname: Optional[str] = None,
298+ level: Optional[int] = None, **_kwargs) -> PilImportFrom:
299+ """ImportFrom(identifier? module, identifier name, identifier? asname, int? level)"""
300+ return PilImportFrom(module, name, asname, level)
301+ 
302+ def create_global(self, name: str, **_kwargs) -> PilGlobal:
303+ """Global(identifier name); ``global a, b`` is two statements."""
304+ return PilGlobal(name)
305+ 
306+ def create_nonlocal(self, name: str, **_kwargs) -> PilNonlocal:
307+ """Nonlocal(identifier name); ``nonlocal a, b`` is two statements."""
308+ return PilNonlocal(name)
309+ 
310+ def create_pass(self) -> PilPass:
311+ """Pass"""
312+ return PilPass()
313+ 
314+ def create_break(self) -> PilBreak:
315+ """Break"""
316+ return PilBreak()
317+ 
318+ def create_continue(self) -> PilContinue:
319+ """Continue"""
320+ return PilContinue()
321+ 
322+ # ---- Parameter factories ----
323+ 
324+ def create_keyword(self, arg: Optional[str], value: PilExpr, **_kwargs) -> PilKeyword:
325+ """keyword(arg?, value)"""
326+ return PilKeyword(arg, value)
327+ 
328+ def create_parameter(self, name: str, default: Optional[PilExpr] = None,
329+ annotation: Optional[PilExpr] = None,
330+ type_comment: Optional[str] = None, **_kwargs) -> PilParameter:
331+ """arg(identifier arg, expr? annotation, expr? default, string? type_comment)"""
332+ return PilParameter(name, default, annotation, type_comment)
333+ 
334+ def create_function_parameter_def(self, posonlyargs: list[PilParameter] = (),
335+ args: list[PilParameter] = (),
336+ vararg: Optional[PilParameter] = None,
337+ kwonlyargs: list[PilParameter] = (),
338+ kwarg: Optional[PilParameter] = None) -> PilFunctionParameterDef:
339+ """arguments(arg* posonlyargs, arg* args, arg? vararg, arg* kwonlyargs, arg? kwarg)"""
340+ return PilFunctionParameterDef(posonlyargs, args, vararg, kwonlyargs, kwarg)
@@ -0,0 +1,571 @@
1+#!/usr/bin/env python3
2+# coding: utf-8
3+# Copyright (c) 2026 CANN community contributors.
4+# This program is free software; you can redistribute it and/or modify it under the terms and conditions of
5+# CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# -----------------------------------------------------------------------------------------------------------
11+ 
12+"""Lower PilCode nodes back to Python ast nodes and unparse them to legal Python code.
13+ 
14+Every PilCode class carries a ``kind`` tag (set by ``pil_code_dict`` in pil.py);
15+conversion is dispatched through the ``ast_builder_dict`` registry keyed by
16+that tag.
17+ 
18+Registered builders use uniform signatures:
19+ expr builders: (node) -> ast.expr (always Load ctx)
20+ stmt builders: (node) -> ast.stmt
21+ parameter builder: (node) -> ast.arguments
22+ 
23+Statement builders build Store/Del contexts themselves for their target
24+slots; nested expression visiting never changes context.
25+"""
26+ 
27+import ast
28+from typing import Union
29+ 
30+from pypto.error import FeError
31+from pypto.frontend.kind_dict import KindDict
32+ 
33+from .pil import (
34+ PIL_CALLEE_BOP_ADD,
35+ PIL_CALLEE_BOP_BITAND,
36+ PIL_CALLEE_BOP_BITOR,
37+ PIL_CALLEE_BOP_BITXOR,
38+ PIL_CALLEE_BOP_DIV,
39+ PIL_CALLEE_BOP_FLOORDIV,
40+ PIL_CALLEE_BOP_LSHIFT,
41+ PIL_CALLEE_BOP_MATMULT,
42+ PIL_CALLEE_BOP_MOD,
43+ PIL_CALLEE_BOP_MULT,
44+ PIL_CALLEE_BOP_POW,
45+ PIL_CALLEE_BOP_RSHIFT,
46+ PIL_CALLEE_BOP_SUB,
47+ PIL_CALLEE_COP_EQ,
48+ PIL_CALLEE_COP_GT,
49+ PIL_CALLEE_COP_GTE,
50+ PIL_CALLEE_COP_IN,
51+ PIL_CALLEE_COP_IS,
52+ PIL_CALLEE_COP_ISNOT,
53+ PIL_CALLEE_COP_LT,
54+ PIL_CALLEE_COP_LTE,
55+ PIL_CALLEE_COP_NOTEQ,
56+ PIL_CALLEE_COP_NOTIN,
57+ PIL_CALLEE_DICT_OP,
58+ PIL_CALLEE_JOINED_STR,
59+ PIL_CALLEE_LIST,
60+ PIL_CALLEE_SET_OP,
61+ PIL_CALLEE_TUPLE,
62+ PIL_CALLEE_UOP_INVERT,
63+ PIL_CALLEE_UOP_NOT,
64+ PIL_CALLEE_UOP_UADD,
65+ PIL_CALLEE_UOP_USUB,
66+ PIL_CALLEE_YIELD,
67+ PIL_CALLEE_YIELD_FROM,
68+ PilAssert,
69+ PilAssignAttribute,
70+ PilAssignSubscript,
71+ PilAssignSymbolList,
72+ PilAttribute,
73+ PilBreak,
74+ PilCall,
75+ PilClassDef,
76+ PilCode,
77+ PilConstantExpr,
78+ PilContinue,
79+ PilDeleteAttribute,
80+ PilDeleteIdentifier,
81+ PilDeleteSubscript,
82+ PilExprListItem,
83+ PilFor,
84+ PilFunctionDef,
85+ PilFunctionParameterDef,
86+ PilGlobal,
87+ PilIdentifierExpr,
88+ PilIf,
89+ PilImport,
90+ PilImportFrom,
91+ PilKeyword,
92+ PilNonlocal,
93+ PilParameter,
94+ PilPass,
95+ PilRaise,
96+ PilReturn,
97+ PilSlice,
98+ PilStarred,
99+ PilStmt,
100+ PilStmtList,
@@ -11,7 +11,7 @@
11"""Check every ``test_*.py.pil`` is a faithful render of its ``test_*.py``.11"""Check every ``test_*.py.pil`` is a faithful render of its ``test_*.py``.
12 12 
13``render.py`` produces a ``.py.pil`` from a ``.py`` (raw) via13``render.py`` produces a ``.py.pil`` from a ``.py`` (raw) via
14-``ast.parse`` -> ``pil.parse_stmts`` -> ``ast.unparse`` (prefixed with a license14+``ast.parse`` -> ``pil_parser.parse_stmts`` -> ``ast.unparse`` (prefixed with a license
15header). This test re-applies the same transform to the raw file and compares it15header). This test re-applies the same transform to the raw file and compares it
16against the committed ``.py.pil`` after normalising both sides through16against the committed ``.py.pil`` after normalising both sides through
17``ast.parse`` -> ``ast.unparse``. The round-trip strips comments (the license17``ast.parse`` -> ``ast.unparse``. The round-trip strips comments (the license
@@ -30,7 +30,7 @@ import sys
30import types30import types
31import unittest31import unittest
32 32 
33-import pypto.frontend.parser.pil as pil33+import pypto.frontend.parser.pil_parser as pil_parser
34 34 
35DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_pil_builder_data")35DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_pil_builder_data")
36 36 
@@ -38,9 +38,9 @@ _NON_DATA_FILES = {"test_pil_builder_utils.py"}
38 38 
39 39 
40def _render(src):40def _render(src):
41- """Apply the pil transform directly: ast.parse -> pil.parse_stmts -> ast.unparse."""41+ """Apply the pil transform directly: ast.parse -> pil_parser.parse_stmts -> ast.unparse."""
42 tree = ast.parse(src)42 tree = ast.parse(src)
43- transformed = pil.parse_stmts(tree.body)43+ transformed = pil_parser.parse_stmts(tree.body)
44 return ast.unparse(ast.Module(body=transformed, type_ignores=[]))44 return ast.unparse(ast.Module(body=transformed, type_ignores=[]))
45 45 
46 46 
@@ -11,7 +11,7 @@
11"""Render pil-transformed test sources.11"""Render pil-transformed test sources.
12 12 
13Reads every ``test_*.raw.py`` in this directory, transforms it through the pil13Reads every ``test_*.raw.py`` in this directory, transforms it through the pil
14-frontend (``ast.parse`` -> ``pil.parse_stmts`` -> ``ast.unparse``) and writes the14+frontend (``ast.parse`` -> ``pil_parser.parse_stmts`` -> ``ast.unparse``) and writes the
15result to the matching ``test_*.pil.py``. The pil form is semantically15result to the matching ``test_*.pil.py``. The pil form is semantically
16equivalent to the raw form (it is a 3-address normalisation), so both should16equivalent to the raw form (it is a 3-address normalisation), so both should
17behave identically when executed.17behave identically when executed.
@@ -21,7 +21,7 @@ import ast
21import glob21import glob
22import os22import os
23 23 
24-import pypto.frontend.parser.pil as pil24+import pypto.frontend.parser.pil_parser as pil_parser
25 25 
26_DATA_DIR = os.path.dirname(os.path.abspath(__file__))26_DATA_DIR = os.path.dirname(os.path.abspath(__file__))
27 27 
@@ -36,7 +36,7 @@ def render(src):
36 ``render_data.txt``.36 ``render_data.txt``.
37 """37 """
38 tree = ast.parse(src)38 tree = ast.parse(src)
39- transformed = pil.parse_stmts(tree.body)39+ transformed = pil_parser.parse_stmts(tree.body)
40 code = ast.unparse(ast.Module(body=transformed, type_ignores=[]))40 code = ast.unparse(ast.Module(body=transformed, type_ignores=[]))
41 return _LICENSE + "\n" + code + "\n"41 return _LICENSE + "\n" + code + "\n"
42 42 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class ExprTest(PILTestCase):15class ExprTest(PILTestCase):
15 16 
@@ -1276,7 +1277,7 @@ class AssignTest(PILTestCase):
1276 _pil_1067 = Expr.int1277 _pil_1067 = Expr.int
1277 _pil_1068 = _pil_1067(1)1278 _pil_1068 = _pil_1067(1)
1278 _pil_1064 = [_pil_1066, _pil_1068]1279 _pil_1064 = [_pil_1066, _pil_1068]
1279- [_pil_1069, _pil_1070] = _pil_10641280+ _pil_1069, _pil_1070 = _pil_1064
1280 _var_x = _pil_10691281 _var_x = _pil_1069
1281 _var_y = _pil_10701282 _var_y = _pil_1070
1282 _pil_1071 = self.assertEqual1283 _pil_1071 = self.assertEqual
@@ -1607,10 +1608,10 @@ class AssignTest(PILTestCase):
1607 _pil_1341 = Expr.int1608 _pil_1341 = Expr.int
1608 _pil_1342 = _pil_1341(1)1609 _pil_1342 = _pil_1341(1)
1609 _pil_1338 = [_pil_1340, _pil_1342]1610 _pil_1338 = [_pil_1340, _pil_1342]
1610- [_pil_1343, _pil_1344] = _pil_13381611+ _pil_1343, _pil_1344 = _pil_1338
1611 _var_a = _pil_13431612 _var_a = _pil_1343
1612 _var_b = _pil_13441613 _var_b = _pil_1344
1613- [_pil_1345, _pil_1346] = _pil_13381614+ _pil_1345, _pil_1346 = _pil_1338
1614 _var_c = _pil_13451615 _var_c = _pil_1345
1615 _var_d = _pil_13461616 _var_d = _pil_1346
1616 _pil_1347 = self.assertEqual1617 _pil_1347 = self.assertEqual
@@ -1703,11 +1704,11 @@ class AssignTest(PILTestCase):
1703 _pil_1418 = [_pil_1420, _pil_1421]1704 _pil_1418 = [_pil_1420, _pil_1421]
1704 _pil_1415 = [_pil_1417, _pil_1418]1705 _pil_1415 = [_pil_1417, _pil_1418]
1705 _var_x = _pil_14151706 _var_x = _pil_1415
1706- [_pil_1426, _pil_1427] = _pil_14151707+ _pil_1426, _pil_1427 = _pil_1415
1707 var_a = _pil_14261708 var_a = _pil_1426
1708- [_pil_1428, _pil_1429] = _pil_14271709+ _pil_1428, _pil_1429 = _pil_1427
1709 var_b = _pil_14281710 var_b = _pil_1428
1710- [_pil_1430, _pil_1431] = _pil_14291711+ _pil_1430, _pil_1431 = _pil_1429
1711 var_c = _pil_14301712 var_c = _pil_1430
1712 var_d = _pil_14311713 var_d = _pil_1431
1713 _pil_1432 = self.assertEqual1714 _pil_1432 = self.assertEqual
@@ -1740,7 +1741,7 @@ class AssignTest(PILTestCase):
1740 _var_x = _pil_14461741 _var_x = _pil_1446
1741 _pil_1457, _pil_1458 = _pil_14461742 _pil_1457, _pil_1458 = _pil_1446
1742 var_a = _pil_14571743 var_a = _pil_1457
1743- [_pil_1459, _pil_1460] = _pil_14581744+ _pil_1459, _pil_1460 = _pil_1458
1744 var_b = _pil_14591745 var_b = _pil_1459
1745 _pil_1461, _pil_1462 = _pil_14601746 _pil_1461, _pil_1462 = _pil_1460
1746 var_c = _pil_14611747 var_c = _pil_1461
@@ -1771,7 +1772,7 @@ class AssignTest(PILTestCase):
1771 _pil_1485 = _pil_1484(3)1772 _pil_1485 = _pil_1484(3)
1772 _pil_1477 = [_pil_1479, _pil_1481, _pil_1483, _pil_1485]1773 _pil_1477 = [_pil_1479, _pil_1481, _pil_1483, _pil_1485]
1773 _var_x = _pil_14771774 _var_x = _pil_1477
1774- [_pil_1486, _pil_1487, _pil_1488, _pil_1489] = _pil_14771775+ _pil_1486, _pil_1487, _pil_1488, _pil_1489 = _pil_1477
1775 var_a = _pil_14861776 var_a = _pil_1486
1776 var_b = _pil_14871777 var_b = _pil_1487
1777 var_c = _pil_14881778 var_c = _pil_1488
@@ -1817,7 +1818,7 @@ class AssignTest(PILTestCase):
1817 _pil_1525 = _pil_1524(1)1818 _pil_1525 = _pil_1524(1)
1818 _pil_1521 = [_pil_1523, _pil_1525]1819 _pil_1521 = [_pil_1523, _pil_1525]
1819 _var_x = _pil_15211820 _var_x = _pil_1521
1820- [_pil_1526, _pil_1527] = _pil_15211821+ _pil_1526, _pil_1527 = _pil_1521
1821 var_c = _pil_15261822 var_c = _pil_1526
1822 var_d = _pil_15271823 var_d = _pil_1527
1823 _pil_1528, _pil_1529 = _pil_15211824 _pil_1528, _pil_1529 = _pil_1521
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class ClassTest(PILTestCase):15class ClassTest(PILTestCase):
15 16 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class BoolopTest(PILTestCase):15class BoolopTest(PILTestCase):
15 16 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class ForTest(PILTestCase):15class ForTest(PILTestCase):
15 16 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class ConstantTest(PILTestCase):15class ConstantTest(PILTestCase):
15 16 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class RaiseTest(PILTestCase):15class RaiseTest(PILTestCase):
15 16 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class FunctionDefTest(PILTestCase):15class FunctionDefTest(PILTestCase):
15 16 
@@ -9,7 +9,8 @@
9# See LICENSE in the root of the software repository for the full text of the License.9# See LICENSE in the root of the software repository for the full text of the License.
10# -----------------------------------------------------------------------------------------------------------10# -----------------------------------------------------------------------------------------------------------
11 11 
12-from .test_pil_builder_utils import Expr, PILTestCase12+from .test_pil_builder_utils import Expr
13+from .test_pil_builder_utils import PILTestCase
13 14 
14class ListCompTest(PILTestCase):15class ListCompTest(PILTestCase):
15 16 
@@ -283,7 +284,7 @@ class ListCompTest(PILTestCase):
283 _pil_220 = [_pil_222, _pil_224]284 _pil_220 = [_pil_222, _pil_224]
284 _pil_214 = [_pil_215, _pil_220]285 _pil_214 = [_pil_215, _pil_220]
285 for _pil_225 in _pil_214:286 for _pil_225 in _pil_214:
286- [_pil_226, _pil_227] = _pil_225287+ _pil_226, _pil_227 = _pil_225
287 var_a = _pil_226288 var_a = _pil_226
288 var_b = _pil_227289 var_b = _pil_227
289 _pil_228 = Expr.str290 _pil_228 = Expr.str
@@ -784,7 +785,7 @@ class SetCompTest(PILTestCase):
784 _pil_645 = [_pil_647, _pil_649]785 _pil_645 = [_pil_647, _pil_649]
785 _pil_639 = [_pil_640, _pil_645]786 _pil_639 = [_pil_640, _pil_645]
786 for _pil_650 in _pil_639:787 for _pil_650 in _pil_639:
787- [_pil_651, _pil_652] = _pil_650788+ _pil_651, _pil_652 = _pil_650
788 var_a = _pil_651789 var_a = _pil_651
789 var_b = _pil_652790 var_b = _pil_652
790 _pil_653 = Expr.str791 _pil_653 = Expr.str
@@ -1245,7 +1246,7 @@ class DictCompTest(PILTestCase):
1245 _pil_1038 = [_pil_1040, _pil_1042]1246 _pil_1038 = [_pil_1040, _pil_1042]
1246 _pil_1032 = [_pil_1033, _pil_1038]1247 _pil_1032 = [_pil_1033, _pil_1038]
1247 for _pil_1043 in _pil_1032:1248 for _pil_1043 in _pil_1032:
1248- [_pil_1044, _pil_1045] = _pil_10431249+ _pil_1044, _pil_1045 = _pil_1043
1249 var_a = _pil_10441250 var_a = _pil_1044
1250 var_b = _pil_10451251 var_b = _pil_1045
1251 _pil_1047 = Expr.int1252 _pil_1047 = Expr.int
@@ -1734,7 +1735,7 @@ class GeneratorExpTest(PILTestCase):
1734 _pil_1454 = [_pil_1456, _pil_1458]1735 _pil_1454 = [_pil_1456, _pil_1458]
1735 _pil_1448 = [_pil_1449, _pil_1454]1736 _pil_1448 = [_pil_1449, _pil_1454]
1736 for _pil_1459 in _pil_1448:1737 for _pil_1459 in _pil_1448:
1737- [_pil_1460, _pil_1461] = _pil_14591738+ _pil_1460, _pil_1461 = _pil_1459
1738 var_a = _pil_14601739 var_a = _pil_1460
1739 var_b = _pil_14611740 var_b = _pil_1461
1740 _pil_1462 = Expr.str1741 _pil_1462 = Expr.str
@@ -15,7 +15,7 @@ import logging
15import textwrap15import textwrap
16import unittest16import unittest
17 17 
18-import pypto.frontend.parser.pil as pil18+import pypto.frontend.parser.pil_parser as pil_parser
19 19 
20LOGGER = logging.getLogger(__name__)20LOGGER = logging.getLogger(__name__)
21 21 
@@ -231,7 +231,7 @@ class TestParser:
231 stmt_list = ast.parse(source).body[0].body231 stmt_list = ast.parse(source).body[0].body
232 232 
233 python_trace, python_vardict = self.run_ast(stmt_list)233 python_trace, python_vardict = self.run_ast(stmt_list)
234- pil_trace, pil_vardict = self.run_ast(pil.parse_stmts(stmt_list))234+ pil_trace, pil_vardict = self.run_ast(pil_parser.parse_stmts(stmt_list))
235 235 
236 assert python_trace == pil_trace, f'{target.__name__}: {python_trace=} {pil_trace=}'236 assert python_trace == pil_trace, f'{target.__name__}: {python_trace=} {pil_trace=}'
237 assert python_vardict == pil_vardict, f'{target.__name__}: {python_vardict=} {pil_vardict=}'237 assert python_vardict == pil_vardict, f'{target.__name__}: {python_vardict=} {pil_vardict=}'