Hone logo
Hone
Problems

AST Transformer for Code Modification

This challenge asks you to implement a simple Abstract Syntax Tree (AST) transformer in Python. ASTs represent the structure of code, and transformers allow you to programmatically modify that structure, enabling tasks like code refactoring, linting, and automated code generation. You'll be working with the ast module to parse, traverse, and modify a given AST.

Problem Description

You are tasked with creating a transformer that modifies an AST to replace all occurrences of the x + 1 pattern with x + 10. The transformer should traverse the AST, identify nodes representing binary operations where the left operand is a variable (represented by a simple name) and the right operand is an addition operation with a constant 1, and replace the entire expression with the equivalent expression x + 10. The variable name x should be preserved.

What needs to be achieved:

  • Parse a Python code snippet into an AST.
  • Traverse the AST and identify specific binary operation patterns.
  • Replace those patterns with a modified expression (x + 10).
  • Convert the modified AST back into Python code.

Key Requirements:

  • The transformer must correctly identify and replace only the x + 1 pattern. Other additions should not be modified.
  • The variable name x must be preserved. The transformer should not change the variable name.
  • The code generated from the modified AST must be valid Python code.

Expected Behavior:

Given an AST representing a Python expression, the transformer should return a new AST representing the modified expression. When this new AST is converted back to Python code, it should reflect the replacements made.

Edge Cases to Consider:

  • The variable x might appear multiple times in the code.
  • The expression x + 1 might be nested within other expressions.
  • The code might contain other addition operations that are not x + 1.
  • The input code might be syntactically invalid (though your transformer shouldn't need to handle this, the ast module will raise an exception if parsing fails).

Examples

Example 1:

Input: ast.parse("x + 1")
Output: ast.parse("x + 10")
Explanation: The input AST represents the expression "x + 1". The transformer identifies this pattern and replaces it with "x + 10".

Example 2:

Input: ast.parse("y + 1")
Output: ast.parse("y + 1")
Explanation: The input AST represents the expression "y + 1". The transformer does not modify this expression because the variable is not 'x'.

Example 3:

Input: ast.parse("x + 1 + 2")
Output: ast.parse("x + 10 + 2")
Explanation: The transformer replaces the first "x + 1" with "x + 10", leaving the rest of the expression unchanged.

Example 4:

Input: ast.parse("(x + 1) * 2")
Output: ast.parse("(x + 10) * 2")
Explanation: The transformer replaces "x + 1" within the parentheses.

Constraints

  • The input code will be a valid Python expression.
  • The variable to be replaced will always be named "x".
  • The constant to be added will always be 1.
  • The transformer should be reasonably efficient; avoid unnecessary iterations or complex logic.
  • The output must be a valid AST that can be converted back to Python code without errors.

Notes

  • You'll need to use the ast module for parsing and manipulating the AST.
  • The ast.NodeTransformer class provides a base class for creating AST transformers. You'll need to subclass this class and override the visit_* methods to handle different node types.
  • Consider using the ast.BinOp, ast.Name, and ast.Constant node types.
  • Remember to return the modified node from your visit_* methods. Failing to do so will effectively remove the node from the AST.
  • The ast.unparse function (available in Python 3.9+) can be used to convert the modified AST back into Python code. If using an older version of Python, you'll need to use a different method to convert the AST back to code (e.g., astor).
Loading editor...
python