Skip to content

Commit cb4e943

Browse files
authored
Merge pull request #80 from CoreTrace/feat/frontend-gaps
Support assignment expressions, yield from and starred targets
2 parents 7ee50ab + 3c6ae23 commit cb4e943

11 files changed

Lines changed: 268 additions & 117 deletions

File tree

‎docs/usage.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,10 @@ assignment, list, tuple, set and dict literals with unpacking, f-strings, slices
363363
boolean operators, chained comparisons, keyword and starred arguments, `with`, `assert`,
364364
`try`/`except`/`else`/`finally`, `await`, `yield`, `if`, `while` and `for` with their
365365
`else` clauses, `break`, `continue`, `raise` with `from`, conditional expressions,
366-
comprehensions, lambdas, nested functions and classes, `global`, `nonlocal`, `del` and
367-
`match` with every pattern kind. `finally` is modelled on the normal path only and a
366+
comprehensions, lambdas, nested functions and classes, `global`, `nonlocal`, `del`,
367+
assignment expressions (`(y := f(x))`, laid out as an assignment before the statement),
368+
`yield from`, starred assignment targets (`first, *rest = items`) and `match` with every
369+
pattern kind. `finally` is modelled on the normal path only and a
368370
`nonlocal` write stays inside the nested function. Methods of module-level classes are
369371
analysed like functions.
370372

‎src/coretrace_python/cfg/builder.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def hoist(self, node: Any, pending: list[nodes.Statement]) -> Any:
129129
return result
130130
if isinstance(node, nodes.Comprehension):
131131
return self.comprehension(node, pending)
132+
if isinstance(node, nodes.NamedExpr):
133+
pending.append(nodes.Assign(node.target, self.hoist(node.value, pending), node.span))
134+
return node.target
132135
if isinstance(node, nodes.Lambda):
133136
defaults = tuple(
134137
replace(p, default=self.hoist(p.default, pending)) if p.default is not None else p
@@ -481,7 +484,7 @@ def _may_hold_expressions(value: object) -> bool:
481484

482485

483486
def _has_control_flow(node: object) -> bool:
484-
if isinstance(node, nodes.Conditional | nodes.Comprehension):
487+
if isinstance(node, nodes.Conditional | nodes.Comprehension | nodes.NamedExpr):
485488
return True
486489
if isinstance(node, nodes.Lambda):
487490
return False
@@ -492,9 +495,11 @@ def _has_control_flow(node: object) -> bool:
492495
return False
493496

494497

495-
def _bound_names(target: nodes.Target) -> list[str]:
498+
def _bound_names(target: nodes.Target | nodes.Starred) -> list[str]:
496499
if isinstance(target, nodes.Name):
497500
return [target.identifier]
501+
if isinstance(target, nodes.Starred):
502+
return _bound_names(target.value) # type: ignore[arg-type]
498503
if isinstance(target, nodes.Tuple):
499504
return [name for element in target.elements for name in _bound_names(element)] # type: ignore[arg-type]
500505
return []

‎src/coretrace_python/frontend/ast_adapter.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def expression(self, node: ast.expr) -> nodes.Expression:
146146
yielded = self.expression(node.value) if node.value is not None else None
147147
return nodes.Yield(yielded, span)
148148
if isinstance(node, ast.YieldFrom):
149-
self.fail(node, "yield from is not supported yet")
149+
return nodes.Yield(self.expression(node.value), span, True)
150+
if isinstance(node, ast.NamedExpr):
151+
assert isinstance(node.target, ast.Name)
152+
target = nodes.Name(node.target.id, self.span(node.target))
153+
return nodes.NamedExpr(target, self.expression(node.value), span)
150154
if isinstance(node, (ast.ListComp, ast.SetComp, ast.GeneratorExp)):
151155
generators = tuple(self.generator(generator) for generator in node.generators)
152156
return nodes.Comprehension(
@@ -178,10 +182,13 @@ def target(self, node: ast.expr) -> nodes.Target:
178182
assert isinstance(target, (nodes.Attribute, nodes.Subscript))
179183
return target
180184
if isinstance(node, (ast.Tuple, ast.List)):
185+
elements: list[nodes.Expression] = []
181186
for element in node.elts:
182187
if isinstance(element, ast.Starred):
183-
self.fail(element, "starred assignment targets are not supported yet")
184-
return nodes.Tuple(tuple(self.target(element) for element in node.elts), self.span(node))
188+
elements.append(nodes.Starred(self.target(element.value), self.span(element)))
189+
else:
190+
elements.append(self.target(element))
191+
return nodes.Tuple(tuple(elements), self.span(node))
185192
self.fail(node, f"unsupported assignment target: {type(node).__name__}")
186193

187194
def generator(self, node: ast.comprehension) -> nodes.ComprehensionGenerator:

‎src/coretrace_python/hir/nodes.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,21 @@ class Await:
163163

164164
@dataclass(frozen=True, slots=True)
165165
class Yield:
166+
"""``yield value``; with ``delegate``, ``yield from value``."""
167+
166168
value: Expression | None
167169
span: SourceSpan
170+
delegate: bool = False
171+
172+
173+
@dataclass(frozen=True, slots=True)
174+
class NamedExpr:
175+
"""``(target := value)``: binds ``target`` in the enclosing function and evaluates to
176+
the value. The CFG builder lays it out as an assignment before its statement."""
177+
178+
target: Name
179+
value: Expression
180+
span: SourceSpan
168181

169182

170183
@dataclass(frozen=True, slots=True)
@@ -210,6 +223,7 @@ class Comprehension:
210223
| Lambda
211224
| Await
212225
| Yield
226+
| NamedExpr
213227
| Comprehension
214228
)
215229

‎src/coretrace_python/ir/lowering.py‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,29 @@ def store(self, target: nodes.Target, value: Value) -> None:
240240
key = self.expression(target.key)
241241
self.emit_effect(SetItem(None, target.span, object_value, key, value))
242242
else:
243-
for index, element in enumerate(target.elements):
244-
position = self.emit(Constant(self.new_value(), element.span, index))
243+
self.unpack(target, value)
244+
245+
def unpack(self, target: nodes.Tuple, value: Value) -> None:
246+
"""Store each element of ``value`` into the targets; a starred target receives
247+
the slice the others leave, and the targets after it index from the end."""
248+
249+
count = len(target.elements)
250+
starred = next((i for i, e in enumerate(target.elements) if isinstance(e, nodes.Starred)), None)
251+
for index, element in enumerate(target.elements):
252+
if isinstance(element, nodes.Starred):
253+
lower = self.emit(Constant(self.new_value(), element.span, index))
254+
upper = None
255+
if index < count - 1:
256+
upper = self.emit(Constant(self.new_value(), element.span, index - count + 1))
257+
bounds = self.emit(BuildSlice(self.new_value(), element.span, lower, upper, None))
258+
item = self.emit(GetItem(self.new_value(), element.span, value, bounds))
259+
element = element.value
260+
else:
261+
offset = index if starred is None or index < starred else index - count
262+
position = self.emit(Constant(self.new_value(), element.span, offset))
245263
item = self.emit(GetItem(self.new_value(), element.span, value, position))
246-
assert isinstance(element, nodes.Name | nodes.Attribute | nodes.Subscript | nodes.Tuple)
247-
self.store(element, item)
264+
assert isinstance(element, nodes.Name | nodes.Attribute | nodes.Subscript | nodes.Tuple)
265+
self.store(element, item)
248266

249267
def statement(self, node: nodes.Statement) -> None:
250268
if isinstance(node, nodes.Assign):
@@ -466,9 +484,11 @@ def _reassigned_parameters(function: nodes.Function) -> frozenset[str]:
466484

467485
assigned: set[str] = set()
468486

469-
def names(target: nodes.Target | None) -> None:
487+
def names(target: nodes.Target | nodes.Starred | None) -> None:
470488
if isinstance(target, nodes.Name):
471489
assigned.add(target.identifier)
490+
elif isinstance(target, nodes.Starred):
491+
names(target.value) # type: ignore[arg-type]
472492
elif isinstance(target, nodes.Tuple):
473493
for element in target.elements:
474494
names(element) # type: ignore[arg-type]

‎src/coretrace_python/semantic/scopes.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ def expression(self, node: nodes.Expression, scope: _ScopeBuilder) -> None:
351351
elif isinstance(node, nodes.Yield):
352352
if node.value is not None:
353353
self.expression(node.value, scope)
354+
elif isinstance(node, nodes.NamedExpr):
355+
self.expression(node.value, scope)
356+
# PEP 572: a walrus inside a comprehension binds in the containing scope.
357+
owner = scope
358+
while owner.kind is ScopeKind.COMPREHENSION and owner.parent is not None:
359+
owner = owner.parent
360+
owner.bind(node.target.identifier, BindingKind.LOCAL, node.target.span)
354361
elif isinstance(node, nodes.Tuple | nodes.List):
355362
for element in node.elements:
356363
self.expression(element, scope)
@@ -419,6 +426,8 @@ def target(self, node: nodes.Target, scope: _ScopeBuilder) -> None:
419426
scope.bind(node.identifier, BindingKind.LOCAL, node.span)
420427
elif isinstance(node, nodes.Tuple):
421428
for element in node.elements:
429+
if isinstance(element, nodes.Starred):
430+
element = element.value
422431
assert isinstance(element, nodes.Name | nodes.Attribute | nodes.Subscript | nodes.Tuple)
423432
self.target(element, scope)
424433
else:

0 commit comments

Comments
 (0)