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