SlideShare a Scribd company logo
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
| result a b |
a := 2.
b := 3.
result := a max: b
Source code Resulting code
2
transformation
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
Find & Replace
“…code…”
x >= y
ifTrue: [ x ]
ifFalse: [ y ]
“…code…”
“…code…”
x max: y
“…code…”
4
Right-Hand Side partLeft-Hand Side part
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 reuse.
6
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: [
| `@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
Inattention causes impactful mistakes!
`.InitializationStatement `InitializationStatement
9
10
How to apply rules?
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
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
Code Transformation by Direct Transformation of ASTs
15
Live Demo
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
Mark Rizun e-mail: mrizun@gmail.com blog: http://myfuncoding.blogspot.com/
17

More Related Content

DOCX
Cosc 2425 project 2 part 1 implement the following c++ code
PPTX
Cs419 Compiler lec1&2 introduction
PDF
Compiler lec 5
PPTX
if, while and for in Python
PPTX
Presentation1
PPTX
C programming Tutorial Session 2
PPTX
C programming Tutorial Session 3
PPTX
Python Lecture 2
Cosc 2425 project 2 part 1 implement the following c++ code
Cs419 Compiler lec1&2 introduction
Compiler lec 5
if, while and for in Python
Presentation1
C programming Tutorial Session 2
C programming Tutorial Session 3
Python Lecture 2

What's hot (17)

PDF
Code Optimization
PPTX
Exercise6 ch2
PPTX
C programming Tutorial Session 1
PPTX
7.3 daqy 2
PPTX
Debugging Python with Pdb!
PPTX
C programming Tutorial Session 4
PPTX
Hash Function(Grostl) and Contex Hull Research paper
PDF
Write a C function that returns the length of a string. Function name: strinq...
PPTX
NACA Regula Falsi Method
PDF
Optimization in the world of 64-bit errors
PPTX
Recurrence relationships
PPTX
C programming - String
DOCX
vhdl exp-5
PPTX
Process of algorithm evaluation
PDF
Hw5 2017-spring
Code Optimization
Exercise6 ch2
C programming Tutorial Session 1
7.3 daqy 2
Debugging Python with Pdb!
C programming Tutorial Session 4
Hash Function(Grostl) and Contex Hull Research paper
Write a C function that returns the length of a string. Function name: strinq...
NACA Regula Falsi Method
Optimization in the world of 64-bit errors
Recurrence relationships
C programming - String
vhdl exp-5
Process of algorithm evaluation
Hw5 2017-spring
Ad

More from ESUG (20)

PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
PDF
Directing Generative AI for Pharo Documentation
PDF
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
PDF
Composing and Performing Electronic Music on-the-Fly with Pharo and Coypu
PDF
Gamifying Agent-Based Models in Cormas: Towards the Playable Architecture for...
PDF
Analysing Python Machine Learning Notebooks with Moose
PDF
FASTTypeScript metamodel generation using FAST traits and TreeSitter project
PDF
Migrating Katalon Studio Tests to Playwright with Model Driven Engineering
PDF
Package-Aware Approach for Repository-Level Code Completion in Pharo
PDF
Evaluating Benchmark Quality: a Mutation-Testing- Based Methodology
PDF
An Analysis of Inline Method Refactoring
PDF
Identification of unnecessary object allocations using static escape analysis
PDF
Control flow-sensitive optimizations In the Druid Meta-Compiler
PDF
Clean Blocks (IWST 2025, Gdansk, Poland)
PDF
Encoding for Objects Matters (IWST 2025)
PDF
Challenges of Transpiling Smalltalk to JavaScript
PDF
Immersive experiences: what Pharo users do!
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
PDF
Cavrois - an Organic Window Management (ESUG 2025)
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
Micromaid: A simple Mermaid-like chart generator for Pharo
Directing Generative AI for Pharo Documentation
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
Composing and Performing Electronic Music on-the-Fly with Pharo and Coypu
Gamifying Agent-Based Models in Cormas: Towards the Playable Architecture for...
Analysing Python Machine Learning Notebooks with Moose
FASTTypeScript metamodel generation using FAST traits and TreeSitter project
Migrating Katalon Studio Tests to Playwright with Model Driven Engineering
Package-Aware Approach for Repository-Level Code Completion in Pharo
Evaluating Benchmark Quality: a Mutation-Testing- Based Methodology
An Analysis of Inline Method Refactoring
Identification of unnecessary object allocations using static escape analysis
Control flow-sensitive optimizations In the Druid Meta-Compiler
Clean Blocks (IWST 2025, Gdansk, Poland)
Encoding for Objects Matters (IWST 2025)
Challenges of Transpiling Smalltalk to JavaScript
Immersive experiences: what Pharo users do!
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
Cavrois - an Organic Window Management (ESUG 2025)
Ad

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Spectroscopy.pptx food analysis technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
Reach Out and Touch Someone: Haptics and Empathic Computing
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25 Week I

Code Transformation by Direct Transformation of ASTs

  • 1. Code Transformation by Direct Transformation of ASTs M. Rizun, J.-C. Bach, S. Ducasse 1
  • 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
  • 5. Problems Time-consuming and annoying task. Complex and error-prone process. Rules are not reusable. 5
  • 6. Tools to automate process Tool Create transformation rules. Apply rules on desired environment scope. Save rules for later reuse. 6
  • 7. Rewrite Engine Engine for code transformation Unintuitive syntax & no GUI to apply rules Pros Cons 7
  • 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. Inattention causes impactful mistakes! `.InitializationStatement `InitializationStatement 9
  • 10. 10 How to apply rules?
  • 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. Rewrite Tool ´  Intuitive GUI ´  Real time result ´  Apply rules to any scope you want ´  Save rules for later reuse 12
  • 13. A.  Input code B.  Resulting code C.  LHS part of rule D.  RHS part of rule 13
  • 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