mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Support augmented assignment by desugaring to x = x op v
AugAssign previously raised 'not supported' outright. Desugaring to an ordinary assignment with a BinOp reproduces its Python semantics for Name and Attribute targets and reuses the whole existing assignment path -- locals, struct fields, and the new globals all work, so 'counter += 1' (the canonical global idiom in the kernel selftest corpus) compiles to load/add/store on @counter. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
@ -189,6 +189,33 @@ def handle_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
||||
logger.error(f"Unsupported assignment target: {ast.dump(target)}")
|
||||
|
||||
|
||||
def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
||||
"""Handle `x += v` by desugaring to `x = x op v` and reusing handle_assign.
|
||||
|
||||
That is the statement's Python semantics for the targets we support, and it
|
||||
means globals come along for free: `counter += 1` under `global counter`
|
||||
becomes load/add/store on @counter.
|
||||
"""
|
||||
if isinstance(stmt.target, ast.Name):
|
||||
load_target = ast.Name(id=stmt.target.id, ctx=ast.Load())
|
||||
elif isinstance(stmt.target, ast.Attribute):
|
||||
load_target = ast.Attribute(
|
||||
value=stmt.target.value, attr=stmt.target.attr, ctx=ast.Load()
|
||||
)
|
||||
else:
|
||||
raise SyntaxError(
|
||||
f"Unsupported augmented-assignment target: {ast.dump(stmt.target)}"
|
||||
)
|
||||
|
||||
desugared = ast.Assign(
|
||||
targets=[stmt.target],
|
||||
value=ast.BinOp(left=load_target, op=stmt.op, right=stmt.value),
|
||||
)
|
||||
ast.copy_location(desugared, stmt)
|
||||
ast.fix_missing_locations(desugared)
|
||||
handle_assign(func, compilation_context, builder, desugared, local_sym_tab)
|
||||
|
||||
|
||||
def handle_cond(func, compilation_context, builder, cond, local_sym_tab):
|
||||
val = eval_expr(func, compilation_context, builder, cond, local_sym_tab)[0]
|
||||
return convert_to_bool(builder, val)
|
||||
@ -283,7 +310,7 @@ def process_stmt(
|
||||
elif isinstance(stmt, ast.Assign):
|
||||
handle_assign(func, compilation_context, builder, stmt, local_sym_tab)
|
||||
elif isinstance(stmt, ast.AugAssign):
|
||||
raise SyntaxError("Augmented assignment not supported")
|
||||
handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab)
|
||||
elif isinstance(stmt, ast.Global):
|
||||
# Declarations were collected by process_func_body; nothing to emit.
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user