PiranhaAgent uses a static inference algorithm and OpenAI's GPT-4 model to generate human-like piranha rules from code examples. It generates these rules in TOML format, which can be applied to refactor other parts of the codebase.
To get started with PiranhaAgent, follow these instructions:
pip install polyglot-piranha-playground
To run the playground:
piranha-playground
To define your transformation rules, you will need to provide pairs of code snippets: 'before' and 'after' transformation. Each piece of code that needs transformation should be marked by unique identifiers surrounded by comments (like // 1
, // 2
, etc.). These identifiers serve two purposes:
- They denote which part of the code should be transformed.
- They define the transformation sequence, or cascading, i.e., which transformations should follow which.
Consider the following code snippet:
class SomeClass {
// 1 -> 2
// 1
void someMethod(String arg) {
}
// end
void otherMethod() {
String x;
// 2
someMethod(x);
// end
}
}
class SomeClass {
// 1 -> 2
// 1
public String someMethod() {
}
// end
void otherMethod() {
String x;
// 2
x = someMethod();
// end
}
}
In this example, there are two transformation points, marked by the identifiers // 1
and // 2
.
// 1
shows the transformation fromvoid someMethod(String arg)
topublic String someMethod()
.// 2
shows the transformation fromsomeMethod(x)
tox = someMethod()
.
The arrow notation // 1 -> 2
indicates the transformation cascade, i.e., transformation // 2
should be applied after transformation // 1
.
Each transformation snippet begins with the identifier (like // 1
) and ends with a // end
comment.
This way of representing transformations helps to create clear, concise, and human-friendly refactoring rules, making it easier to manage and understand your transformations.
Make sure to follow these conventions when inputting your code for refactoring. Happy coding!
Note: The code before and after must be syntactically correct, and it should parse. Moreover, after applying the rules to code before, the refactored code should match the code after. (spaces are ignored).