CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 Smalltalk, Fall 20071...

40
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall 2007 1 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

description

The language is together with its interactive runtime system Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Transcript of CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 Smalltalk, Fall 20071...

Page 1: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

CSE 3302 Programming Languages

Chengkai LiFall 2007

Smalltalk

Lecture 14 – Smalltalk, Fall 2007 1CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Page 2: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Readings:

• Smalltalk by Example, Chapter 1-6

• (Chapter 7-9 are also recommended, especially sections related to Array)

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

2

Page 3: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

The language is together with its interactive runtime system

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

3

Page 4: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Smalltalk Runtime

• Runtime written in the language itself• Can change the system on-the-fly• Debug system state (image), object, class

hierarchy

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

4

Page 5: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Squeak Demo• The files (.exe .changes, .img, .sources)• Menu and Mouse

(red-button (usually left button), yellow-button (usually right button), blue-button(Alt+red, “morphic halo”) )

• Save changes into .img and .sources• Transcript: system console, log• Workspace: Run code snippet, text documents, …• do it, print it, inspect it, explore it• System Browser (Class, Object, Instance variable, Method)• Hierarchy Browser• Method Finder

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

5

Page 6: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Race Car

Based on http://www.youtube.com/watch?v=y_3l08tI5wQ

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

6

Page 7: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

Squeak by Example, Chapter 2

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

7

Page 8: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

1. Create new class category: SBE-Quinto2. Define new class: SBECell

Object subclass: #NameOfSubclassinstanceVariableNames: ''classVariableNames: ''poolDictionaries: ''category: ‘SBE-Quinto‘

SimpleSwitchMorph subclass: #SBECellinstanceVariableNames: 'mouseAction'classVariableNames: ''poolDictionaries: ''category: ‘SBE-Quinto'

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

8

Message: class compiled

Page 9: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

3. Add methods: initialize

initializesuper initialize.self label: ''.self borderWidth: 2.bounds := 0@0 corner: [email protected] := Color paleYellow.onColor := Color paleBlue darker.self useSquareCorners.self turnOff

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

9

newCell := SBECell new.

Message initialize will be automatically sent to the newly created object when the class has an initialize method.

newCell initialize.

Page 10: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

4. Inspect an objectSBECell new (inspect it)self openInWorld (do it)

5. Add new class: SBEGameBorderedMorph subclass: #SBEGame

instanceVariableNames: ''classVariableNames: ''poolDictionaries: ''category: 'SBE--Quinto'

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

10

Page 11: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game6. Add methods: SBEGame>>initialize initialize

| sampleCell width height n |super initialize.n := self cellsPerSide.sampleCell := SBECell new.width := sampleCell width.height := sampleCell height.self bounds: (5@5 extent: ((width*n) @(height*n)) + (2 * self borderWidth)).cells := Matrix new: n tabulate: [ :i :j | self newCellAt: i at: j ].

7. Put methods into category (protocol)

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

11

Page 12: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

8. Two more methods for SBEGame

SBEGame»cellsPerSide"The number of cells along each side of the game"^10

SBEGame»toggleNeighboursOfCellAt: i at: j(i > 1) ifTrue: [ (cells at: i -- 1 at: j ) toggleState].(i < self cellsPerSide) ifTrue: [ (cells at: i + 1 at: j) toggleState].(j > 1) ifTrue: [ (cells at: i at: j -- 1) toggleState].(j < self cellsPerSide) ifTrue: [ (cells at: i at: j + 1) toggleState].

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

12

For annotation only,

not the language syntax.

Page 13: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

9. Two more methods for SBECellSBECell»mouseAction: aBlock

mouseAction := aBlock

SBECell»mouseUp: anEventmouseAction value

10 . Try and debugSBEGame>>initialize…^cells

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

13

Page 14: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Demo: Quinto Game

• File Out and File In

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

14

Page 15: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Everything is object. Objects communicate by messages.

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

15

Page 16: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Object Hierarchy

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

16

Object

UndefinedObject Boolean

Fraction

False

Magnitude

Char Number …True

Integer Float

Collection

Set …

Page 17: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

No Data Type.There is only Class.

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

17

Page 18: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Smalltalk Syntax is Simple.

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

18

Page 19: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Syntax

• Smalltalk is really “small”– Only 6 keywords (pseudo variables)– Class, object, variable, method names are self

explanatory – Only syntax for calling method (messages) and

defining method. • No syntax for control structure• No syntax for creating class

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

19

Page 20: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Expressions

• Literals• Pseudo Variables• Variables• Assignments• Blocks• Messages

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

20

Page 21: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Literals• Number: 3 3.5• Character: $a• String: ‘ ’ (‘Hel’,’lo!’ and ‘Hello!’ are two objects)• Symbol: # (#foo and #foo are the same object)• Compile-time (literal) array: #(1 $a ‘abc’)• Run-time (dynamic) array: {1. $a. SBEGame new }• Comment: “This is a comment.”

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

21

Page 22: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Pseudo Variables

• true: singleton instance of True• false: singleton instance of Falsee• nil: singleton instance of UndefinedObject• self: the object itself• super: the object itself (but using the selector

defined for the superclass)• thisContext: activation of method. (inspect the

state of system)

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

22

Page 23: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Variables• Instance variables.

• Local variables (method, blocks)| sampleCell width height n |

• Arguments (method argument, block argument)SBEGame»toggleNeighboursOfCellAt: i at: j[ :i :j | self newCellAt: i at: j ]

• Shared Variables:– Global variables, e.g., Transcript– Class variables, e.g., Epsilon in Float

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

23

Page 24: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Conventions• Class name, class variable, global variable:

(Capital letter for the first character of every word)TableHashTable

• Local variables, arguments, instance variable:(Capital letter for the first character of every word, except the first word)sampleCell

• Object (instance of a class, especially arguments)aTableaHashTable

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

24

Page 25: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Assignments

• bounds := 0@0 corner: 16@16or• bounds _ 0@0 corner: 16@16

• Assignment returns value, which is the object to the left of :=.

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

25

Page 26: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Defining a Methodselector (method name)

| local variable |statement (expression). (. Is used to end a statement)statement(expression). ^ return-value (^ returns value from a method)

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

26

Page 27: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

object selector (raiseTo:)

message

Methods and Messages• Method Name: Selector• Method Invocation: Message

– Unary selector3 factorial

– Keyword selector3 raisedTo: 2

‘Programming Language’ indexOf: $a startingAt: 3

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

27

object selector

message

object selector ( indexOf:startingAt: )

message

Page 28: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Keyword Selector: more readable

• table insert: anItem at: anIndex

table insert: 3 at: 5

vs.• table.insert(anItem, anIndex)

table.insert(3,5)

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

28

Page 29: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

object selector parameter

Binary selector• 2 + 3

• 2 + 3 + 4 ?

• aTable / 3 (what it means depends on the class)• 1+2*3 ( * does not have higher precedence than -, because they are

messages that can be sent to any object. No mathematical meaning is assumed.)

• Examples:– Integer>>#+– Complex>>#+– Fraction>>#+3/5(1/3) + (1/2)

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

29

Page 30: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Binary selector

• + - * /• = (equality) ~= >= <= > <• == (identity, the two objects are the same unique object), ~~• & | Boolean• , (string concatenation)

‘Hel’,’lo’ = ‘Hello’ ‘Hel’,’lo’ == ‘Hello’#Hello == #Hello

• Assignment := is not a method

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

30

Page 31: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Expression• Associativity for Binary selector : left to right• Precedence rules:

Unary selector, then Binary selector, then Keyword selector

1+2/42 raisedTo: 1 + 3 factorial

• ( ) for changing the order of evaluation

• “-object” was not there originally. So “3 - - 4" generated syntax errors in previous versions.

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

31

Page 32: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Message Cascading

• i.e., Sequence OperatorTranscript cr.Transcript show: 'hello world'.Transcript cr

Transcript cr; show: 'hello world‘; cr

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

32

Page 33: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

A block is an anonymous function.

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

33

Page 34: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Block

• Evaluate a block: valueThe evaluation result is the object from the last statement.

[ 1+2 ] value[ 1+2.‘abc’, ‘def’] value[ 1+2. SBEGame new] value

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

34

Page 35: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Block Parameters• [:x :y | x+y ] value:2 value:3• [ :x :y |

| z | z := x+ y. z:=z*z. z

] value: 2 value: 3

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

35

Page 36: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Block Closure

• Block can access variables declared in enclosing scope.

| x |x := 1.[ :y | x + y ] value: 2[ :y | self x: + y ] value: 2

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

36

Page 37: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

Block is Object!z := [:x :y | x+y ].z value:2 value:3

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

37

Page 38: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

“Control Structures” by Messages• Conditions: Messages to Boolean objects, with blocks

as arguments

class True (subclass of Boolean, False is similar)Selectors:

– ifTrue: alternativeBlock^ alternativeBlock value

– ifFalse: alternativeBlock^nil

– ifTrue:ifFalse:– ifFalse:ifTrue:

• Example– (a < b) ifTrue: [max:=b] ifFalse: [max:=a]

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

38

Page 39: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

“Control Structures” by Messages

• While Loops : Messages to blocks• Example

– n := 1.[ n < 10 ] whileTrue: [ n := n*2 ]

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

39

Page 40: CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14  Smalltalk, Fall 20071 CSE3302 Programming Languages, UT-Arlington Chengkai.

“Control Structures” by Messages• Counting Loops : Blocks as both message receivers and

parameters• Example

– n := 1.10 timesRepeat: [ n := n*2 ]

– n := 1.1 to: 10 do: [ n := n*2 ]

– n := 0.1 to: 10 do: [ :i | n := n + i ]

– n := 0.1 to: 10 by: 2 do: [ :i | n := n + i ]

– n := 0.10 to: 1 by: -2 do: [ :i | n := n + i ]

• Let’s see how Number>>to:do: is implemented

Lecture 14 – Smalltalk, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

40