Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Code Transformation by
Direct Transformation of ASTs
M. Rizun, J.-C. Bach, S. Ducasse
1
Example of simple code transformation
foo
| result a b |
a := 2.
b := 3.
result := a >= b
ifTrue: [ a ]
ifFalse: [ b ]
foo...
Manual code transformation process
(in big system)
Replace all found occurrences by target code.
Search for all occurrence...
Find & Replace
“…code…”
x >= y
ifTrue: [ x ]
ifFalse: [ y ]
“…code…”
“…code…”
x max: y
“…code…”
4
Right-Hand Side partLeft...
Problems
Time-consuming and annoying task.
Complex and error-prone process.
Rules are not reusable.
5
Tools to automate process
Tool Create transformation rules.
Apply rules on desired environment scope.
Save rules for later...
Rewrite Engine
Engine for code
transformation
Unintuitive
syntax & no GUI
to apply rules
Pros
Cons
7
“Scary” transformation rule
Left-Hand Side part
| `@temporaries |
`.InitializationStatement.
`@condition1
ifTrue: [
| `@ot...
Inattention causes impactful mistakes!
`.InitializationStatement `InitializationStatement
9
10
How to apply rules?
OR
| rule result aCollection anEnvironment |
aCollection := self environment.
anEnvironment := RBClassEnvironment
classes:...
Rewrite Tool
´  Intuitive GUI
´  Real time result
´  Apply rules to any scope you want
´  Save rules for later reuse
12
A.  Input code
B.  Resulting code
C.  LHS part of rule
D.  RHS part of rule
13
15
Live Demo
16
Download & Info
´ http://smalltalkhub.com/#!/~MarkRizun/RewriteTool
´ Configuration Browser (Pharo4)
´ http://myfuncodi...
Mark Rizun e-mail: mrizun@gmail.com blog: http://myfuncoding.blogspot.com/
17
Code Transformation by Direct Transformation of ASTs
Upcoming SlideShare
Loading in …5
×

Code Transformation by Direct Transformation of ASTs

567 views

Published on

IWST 2105
Video: https://www.youtube.com/watch?v=bHAhiV8VajQ

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Code Transformation by Direct Transformation of ASTs

  1. 1. Code Transformation by Direct Transformation of ASTs M. Rizun, J.-C. Bach, S. Ducasse 1
  2. 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. 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. 4. Find & Replace “…code…” x >= y ifTrue: [ x ] ifFalse: [ y ] “…code…” “…code…” x max: y “…code…” 4 Right-Hand Side partLeft-Hand Side part
  5. 5. Problems Time-consuming and annoying task. Complex and error-prone process. Rules are not reusable. 5
  6. 6. Tools to automate process Tool Create transformation rules. Apply rules on desired environment scope. Save rules for later reuse. 6
  7. 7. Rewrite Engine Engine for code transformation Unintuitive syntax & no GUI to apply rules Pros Cons 7
  8. 8. “Scary” transformation rule Left-Hand Side part | `@temporaries | `.InitializationStatement. `@condition1 ifTrue: [ | `@otherTemporaries | `@.Statements. `@condition2 ifTrue: [ ``@value := ``@calculate ] ifFalse: [ ``@value := `#wrongLiteral ] ] Right-Hand Side part | `@temporaries `@otherTemporaries | `.InitializationStatement. `@condition1 ifFalse: [ ^self ]. `@.Statements. ``@value := `@condition2 ifTrue: [ ``@calculate ] ifFalse: [ `#wrongLiteral ] 8
  9. 9. Inattention causes impactful mistakes! `.InitializationStatement `InitializationStatement 9
  10. 10. 10 How to apply rules?
  11. 11. OR | rule result aCollection anEnvironment | aCollection := self environment. anEnvironment := RBClassEnvironment classes: aCollection. rule := (Smalltalk globals at: self rules selectedItem) new. result := RBSmalllintChecker runRule: rule onEnvironment: anEnvironment. (ChangesBrowser changes: result builder changes) open 11
  12. 12. Rewrite Tool ´  Intuitive GUI ´  Real time result ´  Apply rules to any scope you want ´  Save rules for later reuse 12
  13. 13. A.  Input code B.  Resulting code C.  LHS part of rule D.  RHS part of rule 13
  14. 14. 15 Live Demo
  15. 15. 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
  16. 16. Mark Rizun e-mail: mrizun@gmail.com blog: http://myfuncoding.blogspot.com/ 17

×