This document describes code transformation through direct manipulation of abstract syntax trees (ASTs). It outlines a manual find-and-replace process for code transformation that is time-consuming and error-prone. Tools are proposed to automate the process by allowing users to create reusable transformation rules through a graphical interface and apply them to scope of code. The Rewrite Tool is presented as providing an intuitive GUI to define left-hand side and right-hand side rules, apply rules in real-time to any scope, and save rules for later reuse.
2. Example of simple code transformation
foo
| result a b |
a := 2.
b := 3.
result := a >= b
ifTrue: [ a ]
ifFalse: [ b ]
foo
| result a b |
a := 2.
b := 3.
result := a max: b
Source code Resulting code
2
transformation
3. Manual code transformation process
(in big system)
Replace all found occurrences by target code.
Search for all occurrences of the code to replace in the source code.
Definition of the desired target code. (RHS)
Definition of the code to change. (LHS)
3
4. Find & Replace
“…code…”
x >= y
ifTrue: [ x ]
ifFalse: [ y ]
“…code…”
“…code…”
x max: y
“…code…”
4
Right-Hand Side partLeft-Hand Side part
16. 16
Download & Info
http://smalltalkhub.com/#!/~MarkRizun/RewriteTool
Configuration Browser (Pharo4)
http://myfuncoding.com
Pharo For The Enterprise (RewriteTool chapter)
https://github.com/SquareBracketAssociates/PharoForTheEnterprise-english/tree/master/RewriteTool
17. Mark Rizun e-mail: mrizun@gmail.com blog: http://myfuncoding.blogspot.com/
17
Notes de l'éditeur
Usually we transform code because we want to:
1 Improve readability and quality of code
2 Make the integration of new features to the code easier
3 To change the API
The transformation is a change of structure of code. We call it LHS and RHS part of transformation rule.
Considering our example, we get the following transformation rule where x and y are some abstract objects.
In case of single method, it is easy to perform transformation. However when it is required to carry out the transformation on big system (with large code base) we have to do the following process.
Obviously, to avoid such problems, it is mandatory to use tools to automate code transformation process, especially in big systems.
Rewrite Engine – an engine that allows defining abstract expressions to match and transform code. However, the language, that is used to define LHS and RHS parts of rule, has unintuitive syntax. Moreover, there is no GUI to apply rules to desired environment scope.
Syntax ` or `@ or ``@ or `# or `@. Etc.
Inattention can cause impactful mistakes like missed symbol, or misspelt name. For example, if one misses a dot symbol in `.InitializationStatement, it will match not a statement, but a single variable. Thus, the whole rule will be completely different than one planned, and it will match different code than was desired.
In Pharo, we have no GUI that would allow user to apply rewrite rule. Clearly, you can apply code transformation programmatically, but not everyone knows how to write that code. The code for applying rules would look similar to what is on slide.
Rewrite Tool. It provides intuitive GUI for implementing rewrite rules, and, what is important, the tool does not require knowledge of Rewrite Engine syntax. Also while creating rule, user is able to see the result of applying current rule on the source code in real time. The Rewrite Tool saves user created rules for later reuse and allows users to apply rules on specific environment scope (packages/classes).
To create transformation rules RT provides contextual menu. For example when user selects an instance of RBBlockNode (the block []), he has an option to create an abstraction that matches any block.