SlideShare a Scribd company logo
First Class Variables


as


AST Annotations
Marcus Denker

Inria RMoD
Part I: The AST
• AST = Abstract Syntax Tree

• Tree Representation of the Method

• Based on the RB AST

• Used by all tools (refactoring, syntax-highlighting,…)
Smalltalk compiler parse: 'test ^(1+2)'
AST
• RBMethodNode Root

• RBVariableNode Variable (read and write)

• RBAssignmentNode Assignment

• RBMessageNode A Message (most of them)

• RBReturnNode Return
Inspect a simple AST
• A very simple Example
Smalltalk compiler parse: 'test ^(1+2)'
User: Tools
• Refactoring

• Breakpoints / Watchers

• Syntax Highlight / Code Completion

• AST based Menu in the Code Browser
User: The Compiler
Source AST
Annotated
AST
IR
Annotated
AST
Bytecode
RBParser OCSemanticAnalyzer
OCASTTranslator/
IRBuilder
IRBytecodeGenerator
Variables in the AST
• Example: (Point>>#x)

•
Problem: Kind of Variable?
• Example: SHRBTextStyler

• Syntax highlighting needs to know which kind
Variables in the AST
• Every de
fi
nition, read and write gets one new instance
of RBVariableNode (as we have to encode the parent
for each di
ff
erently)

• We just know the name

• SYNTAX, but no SEMANTICs

• Kind? (temp or ivar)

• Variables with same name can be di
ff
erent
variables
To the Rescue: Name
Analysis
• We have to annotate the AST with information about
Variables

• Block/Method: de
fi
ned Variables are put in a Scope

• Scopes know the parent Scope

• When we see a use, we loop up the variable in the Scope
Semantic Variables
• Every RBVariableNode gets a semantic variable
annotation

• Both the de
fi
nition and all uses

• There is one instance for each variable that models

• name

• scope it was de
fi
ned
Variables in the AST
• Example Again: (Point>>#x)

•
Variables and Compilation
• Compiler just delegates to the Variable, e.g for instance
Variables:

• emitStore/emitValue: de
fi
ned for each kind of Variables
(global/temp/ivar)
emitStore: methodBuilder
"generate store bytecode"
methodBuilder storeInstVar: index
Repeat:The AST
• AST = Abstract Syntax Tree

• Tree Representation of the Method

• Produced by the Parser (part of the Compiler)

• Used by all tools and the Compiler

• We need to model Variables semantically to make it useful
Now Step Back
Forget Part I


(for now)
Look at it from Re
fl
ective
Point of View
PartII


First Class Variables
First: Variables in ST80
Instance Variables
• De
fi
ned by the Class (list of variable names)

• Can be read via the object:

• instVarNamed:(put:), #instVarAt:(put:)
• Instance Variables have an o
ff
set in the Object

• De
fi
ned by the order of the de
fi
ned vars in the Hierarchy
1@2 instVarNamed: 'x'
Temporary Variable
• De
fi
ned by a method or Block

• Arguments are temps, too

• Can be read via the context

• #tempNamed:, tempNamed:put:
• With Closures this is more complex than you ever want to
know!
[| temp | temp := 1. thisContext tempNamed: 'temp' ] value
Globals
• Entries in the “Smalltalk globals” Dictionary

• Contain the value

• Can be read via the global Dictionary

• Access via #value / value: on the Association

• Class Vars and Pool Vars are just Associations from other
Dictionaries
Smalltalk globals at: #Object.
Object binding value.
“Everything is an Object”
For Variables… not really
Globals/Class Vars
• Here we have at least the Association (#binding):

• But there is no “GlobalVariable” class

• No API other than #value:/#value

• Classes de
fi
ne just names of variables
Object binding
Instance Variables
• The class just knows the names

• There is no Object representing instance variables

• Classes de
fi
ne just names of variables

• Bytecode accesses by o
ff
set
Point allInstVarNames
Temporary Variables
• The methods know nothing. Even to know the variable
name we need the compiler (and the source)

• There is no object representing temp Variables

• Re
fl
ective read and write is *hard* -> compiler needs to
create extensive meta-data
Why Not Do Better?
• Every de
fi
ned Variable is described a meta object

• Class Hierarchy: Variable
The Hierarchy
• Variable

• LiteralVariable

• ClassVariable

• GlobalVariable

• UndeclaredVariable

• WorkspaceVariable
• LocalVariable

• ArgumentVariable

• TemporaryVariable

• ReservedVariable

• SelfVariable

• SuperVariable

• ThisContextVariable

• Slot
Example: vars of a class
• Get all Variables of a class

• Inspect it

• #usingMethods
Point instanceVariables
Instance Variable
• Read x in a Point

• Write

• read/write without sending a message to the object!
(Point instanceVariables
fi
rst) read: (5@4)
point := 5@4.
(Point instanceVariables
fi
rst) write: 100 to: point.
Globals
• Object binding class

• Object binding read

• We keep the Association API so the Global Variables can
play the role of associations in the global dictionary.
Object binding usingMethods
Temporary Variables
• There are too many to allocate them all

• They are created on demand (with the AST)
((LinkedList>>#do:) temporaryVariableNamed: 'aLink')
#lookupVar:
• Every variable knows the scope is was de
fi
ned in

• Every scope know the outer scope

• #lookupVar: looks up names along the scope
[ | temp |thisContext lookupVar: 'temp' ] value.
[ | temp |thisContext lookupVar: ‘Object' ] value
(Point slotNamed: #x ) scope outerScope
Debugger: Read Vars
• In the Debugger we to be able to read Variables from a
DoIt.

• lookupVar, then readInContext works for all Variables!

• If you know the context, you can read any variable

• DoItVariable: Nice names in DoIts (—> Show Us)
[ | temp | temp :=1 . (thisContext lookupVar: 'temp')
readInContext: thisContext] value
Part III: Putting it Together
• We have seen how Semantic Variables are needed to
make the AST useful

• We have seen First Class Variables as part of the
Re
fl
ective Model

• Do we really need the two?
Solution: Scope
• What is needed? Add the concept of Scope 

• Scope of a global is Smalltalk globals

• Scope of an instance variable is the class

• Scope of temp: method and block scope
Example: Point x
(Point slotNamed: #x) scope == Point
(Point lookupVar: #x) == (Point slotNamed: #x)
(Point>>#x) ast variableNodes
fi
rst variable == (Point slotNamed: #x)
What do we get?
• Simpli
fi
ed Name Analysis in the Compiler

• Open Compiler: De
fi
ne your own kinds of Variables

• While fully integrated in the Re
fl
ective Model

• Re
fl
ective Reading/Writing 

• All tools work for you own kinds of Variables
What we did not see…
• De
fi
ne your own kinds of Variables (e.g. subclasses of
Slot / ClassVariable)

• Fluid Class De
fi
nitions: How to create classes that use
these variables

• How this enables DoIts with nice variable names

• Re
fl
ection: MetaLinks on Variables
Thanks…
• This is the work on *many* contributors from the Pharo
Community

• Thanks for lots of interesting discussions, ideas, and
code!
Questions?
• We have seen how the AST needs semantic variables to
be useful

• We have seen First Class Variables as part of the
Re
fl
ective model

• First Class Variables, with just adding the concept of a
Scope, can serve as semantic annotations on the AST

More Related Content

PDF
Variables in Pharo
PDF
First class Variables in Pharo
PDF
Variables in Pharo5
PPT
Stoop 305-reflective programming5
PPT
Java-Variables_about_different_Scope.ppt
PPT
Stoop 422-naming idioms
PPTX
Computer programming 2 Lesson 6
PDF
Advanced Reflection in Pharo
Variables in Pharo
First class Variables in Pharo
Variables in Pharo5
Stoop 305-reflective programming5
Java-Variables_about_different_Scope.ppt
Stoop 422-naming idioms
Computer programming 2 Lesson 6
Advanced Reflection in Pharo

Similar to First Class Variables as AST Annotations (13)

PDF
Subprogram
PPTX
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
PPT
Theory of programming language chapter 5
PPT
8 - OOP - Syntax & Messages
PDF
Inside AOStA
PDF
Java unit 7
KEY
Ruby Internals
KEY
Talk: The Present and Future of Pharo
PDF
Denker - Pharo: Present and Future - 2009-07-14
PDF
#Pharo Days 2016 Reflectivity
PPT
Chapter 5.ppt
PPTX
Chapter 3
PDF
NaBL: A Meta-Language for Declarative Name Binding and Scope Rules
Subprogram
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
Theory of programming language chapter 5
8 - OOP - Syntax & Messages
Inside AOStA
Java unit 7
Ruby Internals
Talk: The Present and Future of Pharo
Denker - Pharo: Present and Future - 2009-07-14
#Pharo Days 2016 Reflectivity
Chapter 5.ppt
Chapter 3
NaBL: A Meta-Language for Declarative Name Binding and Scope Rules
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)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Spectroscopy.pptx food analysis technology
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Approach and Philosophy of On baking technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectroscopy.pptx food analysis technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
A comparative analysis of optical character recognition models for extracting...
Approach and Philosophy of On baking technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars

First Class Variables as AST Annotations

  • 1. First Class Variables as AST Annotations Marcus Denker Inria RMoD
  • 2. Part I: The AST • AST = Abstract Syntax Tree • Tree Representation of the Method • Based on the RB AST • Used by all tools (refactoring, syntax-highlighting,…) Smalltalk compiler parse: 'test ^(1+2)'
  • 3. AST • RBMethodNode Root • RBVariableNode Variable (read and write) • RBAssignmentNode Assignment • RBMessageNode A Message (most of them) • RBReturnNode Return
  • 4. Inspect a simple AST • A very simple Example Smalltalk compiler parse: 'test ^(1+2)'
  • 5. User: Tools • Refactoring • Breakpoints / Watchers • Syntax Highlight / Code Completion • AST based Menu in the Code Browser
  • 6. User: The Compiler Source AST Annotated AST IR Annotated AST Bytecode RBParser OCSemanticAnalyzer OCASTTranslator/ IRBuilder IRBytecodeGenerator
  • 7. Variables in the AST • Example: (Point>>#x) •
  • 8. Problem: Kind of Variable? • Example: SHRBTextStyler • Syntax highlighting needs to know which kind
  • 9. Variables in the AST • Every de fi nition, read and write gets one new instance of RBVariableNode (as we have to encode the parent for each di ff erently) • We just know the name • SYNTAX, but no SEMANTICs • Kind? (temp or ivar) • Variables with same name can be di ff erent variables
  • 10. To the Rescue: Name Analysis • We have to annotate the AST with information about Variables • Block/Method: de fi ned Variables are put in a Scope • Scopes know the parent Scope • When we see a use, we loop up the variable in the Scope
  • 11. Semantic Variables • Every RBVariableNode gets a semantic variable annotation • Both the de fi nition and all uses • There is one instance for each variable that models • name • scope it was de fi ned
  • 12. Variables in the AST • Example Again: (Point>>#x) •
  • 13. Variables and Compilation • Compiler just delegates to the Variable, e.g for instance Variables: • emitStore/emitValue: de fi ned for each kind of Variables (global/temp/ivar) emitStore: methodBuilder "generate store bytecode" methodBuilder storeInstVar: index
  • 14. Repeat:The AST • AST = Abstract Syntax Tree • Tree Representation of the Method • Produced by the Parser (part of the Compiler) • Used by all tools and the Compiler • We need to model Variables semantically to make it useful
  • 17. Look at it from Re fl ective Point of View
  • 20. Instance Variables • De fi ned by the Class (list of variable names) • Can be read via the object: • instVarNamed:(put:), #instVarAt:(put:) • Instance Variables have an o ff set in the Object • De fi ned by the order of the de fi ned vars in the Hierarchy 1@2 instVarNamed: 'x'
  • 21. Temporary Variable • De fi ned by a method or Block • Arguments are temps, too • Can be read via the context • #tempNamed:, tempNamed:put: • With Closures this is more complex than you ever want to know! [| temp | temp := 1. thisContext tempNamed: 'temp' ] value
  • 22. Globals • Entries in the “Smalltalk globals” Dictionary • Contain the value • Can be read via the global Dictionary • Access via #value / value: on the Association • Class Vars and Pool Vars are just Associations from other Dictionaries Smalltalk globals at: #Object. Object binding value.
  • 23. “Everything is an Object”
  • 25. Globals/Class Vars • Here we have at least the Association (#binding): • But there is no “GlobalVariable” class • No API other than #value:/#value • Classes de fi ne just names of variables Object binding
  • 26. Instance Variables • The class just knows the names • There is no Object representing instance variables • Classes de fi ne just names of variables • Bytecode accesses by o ff set Point allInstVarNames
  • 27. Temporary Variables • The methods know nothing. Even to know the variable name we need the compiler (and the source) • There is no object representing temp Variables • Re fl ective read and write is *hard* -> compiler needs to create extensive meta-data
  • 28. Why Not Do Better? • Every de fi ned Variable is described a meta object • Class Hierarchy: Variable
  • 29. The Hierarchy • Variable • LiteralVariable • ClassVariable • GlobalVariable • UndeclaredVariable • WorkspaceVariable • LocalVariable • ArgumentVariable • TemporaryVariable • ReservedVariable • SelfVariable • SuperVariable • ThisContextVariable • Slot
  • 30. Example: vars of a class • Get all Variables of a class • Inspect it • #usingMethods Point instanceVariables
  • 31. Instance Variable • Read x in a Point • Write • read/write without sending a message to the object! (Point instanceVariables fi rst) read: (5@4) point := 5@4. (Point instanceVariables fi rst) write: 100 to: point.
  • 32. Globals • Object binding class • Object binding read • We keep the Association API so the Global Variables can play the role of associations in the global dictionary. Object binding usingMethods
  • 33. Temporary Variables • There are too many to allocate them all • They are created on demand (with the AST) ((LinkedList>>#do:) temporaryVariableNamed: 'aLink')
  • 34. #lookupVar: • Every variable knows the scope is was de fi ned in • Every scope know the outer scope • #lookupVar: looks up names along the scope [ | temp |thisContext lookupVar: 'temp' ] value. [ | temp |thisContext lookupVar: ‘Object' ] value (Point slotNamed: #x ) scope outerScope
  • 35. Debugger: Read Vars • In the Debugger we to be able to read Variables from a DoIt. • lookupVar, then readInContext works for all Variables! • If you know the context, you can read any variable • DoItVariable: Nice names in DoIts (—> Show Us) [ | temp | temp :=1 . (thisContext lookupVar: 'temp') readInContext: thisContext] value
  • 36. Part III: Putting it Together • We have seen how Semantic Variables are needed to make the AST useful • We have seen First Class Variables as part of the Re fl ective Model • Do we really need the two?
  • 37. Solution: Scope • What is needed? Add the concept of Scope • Scope of a global is Smalltalk globals • Scope of an instance variable is the class • Scope of temp: method and block scope
  • 38. Example: Point x (Point slotNamed: #x) scope == Point (Point lookupVar: #x) == (Point slotNamed: #x) (Point>>#x) ast variableNodes fi rst variable == (Point slotNamed: #x)
  • 39. What do we get? • Simpli fi ed Name Analysis in the Compiler • Open Compiler: De fi ne your own kinds of Variables • While fully integrated in the Re fl ective Model • Re fl ective Reading/Writing • All tools work for you own kinds of Variables
  • 40. What we did not see… • De fi ne your own kinds of Variables (e.g. subclasses of Slot / ClassVariable) • Fluid Class De fi nitions: How to create classes that use these variables • How this enables DoIts with nice variable names • Re fl ection: MetaLinks on Variables
  • 41. Thanks… • This is the work on *many* contributors from the Pharo Community • Thanks for lots of interesting discussions, ideas, and code!
  • 42. Questions? • We have seen how the AST needs semantic variables to be useful • We have seen First Class Variables as part of the Re fl ective model • First Class Variables, with just adding the concept of a Scope, can serve as semantic annotations on the AST