31
Variables in Pharo5 ESUG 2015 Marcus Denker http://www.pharo.org

Variables in Pharo5

Embed Size (px)

Citation preview

Page 1: Variables in Pharo5

Variables in Pharo5 ESUG 2015Marcus Denker http://www.pharo.org

Page 2: Variables in Pharo5

Everything is an Object

Page 3: Variables in Pharo5

Everything?

Page 4: Variables in Pharo5

Classes, yes.

Page 5: Variables in Pharo5

Methods, yes

Page 6: Variables in Pharo5

But Variables?

Page 7: Variables in Pharo5

Everything is an object?

SmalltalkImage classVarNamed: #CompilerClass ==> returns value

Object binding class ==> Association

Page 8: Variables in Pharo5

Why not an Object?

Page 9: Variables in Pharo5

Globals/ClassVariables

• We are close: bindings are associations

• Add sublass “LiteralVariable”

• Sublasses GlobalVariable, ClassVariable

• Enhance API

Page 10: Variables in Pharo5

Globals/ClassVariables

SmalltalkImage classVariableNamed: #CompilerClass

Object binding class

Page 11: Variables in Pharo5

Globals: Reflective APiglobal := SmalltalkImage classVariableNamed: #CompilerClass

global read global write: someObject

+ helper methods + compatibility methods

Page 12: Variables in Pharo5

Everything is an object?

• Point instanceVariables

• 5@3 instVarNamed: ‘x’

• 5@3 instVarNamed: ‘y’ put: 6

Page 13: Variables in Pharo5

Why not an Object?

Page 14: Variables in Pharo5

Slots

Point slots

(Point slotNamed: #x) read: (3@4)

(Point slotNamed: #x) write: 7 to: (3@4)

Page 15: Variables in Pharo5

Variables+MetaLink

• Helper methods

• But: can’t we annotate variables directly?

Point assignmentNodes

Page 16: Variables in Pharo5

Variables + Links

• Object binding link: myMetaLink

• (Point slotNamed: #x) link: myMetaLink

(not yet in Pharo5)

Page 17: Variables in Pharo5

Opal Compiler

• Uses RB AST

• Based on Visitors

Text AST AST+vars IR CM

Parser Semantic Analysis

AST Translator+ IRBuilder

BytecodeBuilder+ Encoder

Page 18: Variables in Pharo5

Opal Compiler

• Name analysis finds the variables

• Code generator can delegate to them

Text AST AST+vars IR CM

Parser Semantic Analysis

AST Translator+ IRBuilder

BytecodeBuilder+ Encoder

Page 19: Variables in Pharo5

Gobals: code read

• By default compile reflective read

emitValue: aMethodBuilder aMethodBuilder pushLiteralVariable: #slot->self; send: #read

Page 20: Variables in Pharo5

Gobals: code write

• By default compile reflective write

emitStore: aMethodBuilder | tempName | tempName := Object new. aMethodBuilder addTemp: tempName; storeTemp: tempName; popTop; pushLiteralVariable: #global -> self; pushTemp: tempName; send: #write:

Page 21: Variables in Pharo5

Gobals: code write

• ClassVariable and GlobalVariable override

emitStore: methodBuilder

methodBuilder storeIntoLiteralVariable: self.

Page 22: Variables in Pharo5

Same for Slots

• Slot generates reflective read/write

• InstanceVariableSlot overrides for fast instance access via byte code

Page 23: Variables in Pharo5

What does that mean?

• Slots and Globals are instances of a class

• The compiler delegates code generation to the variable meta object

• Which means: We can define our own variables!

Page 24: Variables in Pharo5

Class Template

Object subclass: #Point slots: { #x. #y } classVariables: { } category: 'Kernel-BasicObjects'

Page 25: Variables in Pharo5

Class Template

Object subclass: #MyClass slots: { #x => WeakSlot } classVariables: { } category: 'Example'

Page 26: Variables in Pharo5

Examples: DEMO

• Simple Slot

• WeakSlot

• Property Slot

• Boolean

Page 27: Variables in Pharo5

RoadMap• Pharo3:

• Layout+Slots (hidden), Opal

• Pharo4

• Slots: Monticello support, class template

• Pharo5

• Remove old Compiler/AST

• Slots + Reflectivity: First finished version

Page 28: Variables in Pharo5

RoadMap

• Pharo6:

• Library of useful Slots

• Use e.g. Property Slots in Bloc/Morphic

Page 29: Variables in Pharo5

Future

• Can’t we model bit patterns and bind them to named virtual slots?

• How to model Array-like layouts better?

Page 30: Variables in Pharo5

Thanks!

• Work of many people…

• special thanks to… Toon Verwaest, Camillo Bruni, Martin Dias, Stephane Ducasse, Max Mattone and Camille Teruel

Page 31: Variables in Pharo5

Questions ?