Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Code transformation by direct transformation of ASTs

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Wird geladen in …3
×

Hier ansehen

1 von 17 Anzeige
Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (18)

Andere mochten auch (20)

Anzeige

Ähnlich wie Code transformation by direct transformation of ASTs (20)

Aktuellste (20)

Anzeige

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

Hinweis der Redaktion

  • 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.

×