csc 520 principles of programming languages

27
520—Spring 2005—48 CSc 520 Principles of Programming Languages 48: OO Languages — SmallTalk Christian Collberg [email protected] Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

Upload: others

Post on 09-Apr-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSc 520 Principles of Programming Languages

520—Spring 2005—48

CSc 520

Principles of ProgrammingLanguages

48: OO Languages — SmallTalk

Christian Collberg

[email protected]

Department of Computer Science

University of Arizona

Copyright c© 2005 Christian Collberg

[1]

Page 2: CSc 520 Principles of Programming Languages

520—Spring 2005—48

History

During the 70’s Alan Kay worked on the Dynabook atXerox Parc. A lot like today’s laptops, with integratedtouch screen, sound, networking.

Users would need some sort of programming skills tofully utilize the system. A language for non-experts wasneeded.

Smalltalk borrows from Simula, Logo (a language forchildren), and Sketchpad (a constraint-based interactivedrawing system).

Smalltalk was the first “pure” object-oriented language.Every interaction is through sending a message to anobject.

[2]

Page 3: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Running Smalltalk

On lectura, do the following:

> cp /usr/local/lib/squeak/3.2-5/Squeak3.2-4956.* .

> setenv SQUEAK_IMAGE $PWD/Squeak3.2-4956.image

> /usr/local/lib/squeak/3.2-5/squeak

[3]

Page 4: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Squeak’s start screen:

[4]

Page 5: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Get rid of the crud:

[5]

Page 6: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Open the class browser:

[6]

Page 7: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Workspace lets you enter commands interactively.Transcript is “standard output.” do it executeshighlighted code.

[7]

Page 8: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Create a new category cc.

[8]

Page 9: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Create a new class MyClass. Select accept to add it.

[9]

Page 10: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Click on no messages to get a message template.

[10]

Page 11: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Create a method square. Select accept to add it.

[11]

Page 12: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Execute square.

[12]

Page 13: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax

Square brackets [...] contain code.

Global items (variables, classes) begin with a capitalletter. Other items start with lowercase.

Temporary variables: | x y z |.

Assignment: ← or :=, or type as .

Return value: ↑, type as ˆ.

The dot (.) is the statement terminator.

[13]

Page 14: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax — Unary Messages

A message M is sent to an object (receiver) R using thesyntax

R M

A unary message has the syntax

R M

For example:

D ← Dictionary new.

[14]

Page 15: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax — Binary Messages

A binary message M to receiver R with argument A hasthe syntax

R M A

For example:

8 + 9

This sends the message + to the object 8 with theargument 9.

[15]

Page 16: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax — Keyword Messages

A keyword message M to receiver R with argumentsA1, A2, A3, . . . has the syntax

R M1: A1 M2: A2 M3: A3 ...

For example:

DeannaTroi kiss: cheek how: tenderly

This sends the message kiss:how: to the objectDeannaTroi with the arguments cheek andtenderly. In Java we would have written:

DeannaTroi.kisshow(cheek,tenderly)

[16]

Page 17: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax — Order of Evaluation

Messages are executed from left to right.

Parentheses can be used to force a particular order ofevaluation.

Expressions are executed in the order1. unary messages,2. binary messages,3. keyword messages

Binary messages are executed left to right.

[17]

Page 18: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax — Cascading messages

Often we want to send several messages M1, M2, . . . tothe same receiver. We can use the syntax

R M1:A1.R M2:A2.R M3:A3.

Or, we can cascade the messages using a semicolon(;):

R M1:A1; M2:A2; M3:A3...

For example:

Transcript show:5; cr; show:9; cr.

[18]

Page 19: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Syntax — Blocks

A block is similar to a lambda expression. It’s syntax is:

[arguments | code]

Arguments are prefixed by a colon (:):

[:x :y | ↑ x+y ]

[19]

Page 20: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Collections: Dictionary.

[20]

Page 21: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Collections: Bag. 3 timesRepeat (from classInteger) sends the value: message to the blockargument 3 times.

[21]

Page 22: CSc 520 Principles of Programming Languages

520—Spring 2005—48

expression timesRepeat block sends thevalue: message to block as long as expression istrue.

[22]

Page 23: CSc 520 Principles of Programming Languages

520—Spring 2005—48

do: aBlock enumerates all the receivers elements.

[23]

Page 24: CSc 520 Principles of Programming Languages

520—Spring 2005—48

ifTrue:ifFalse evaluates one of its blocksdepending on the value of the receiver.

[24]

Page 25: CSc 520 Principles of Programming Languages

520—Spring 2005—48 [25]

Page 26: CSc 520 Principles of Programming Languages

520—Spring 2005—48 [26]

Page 27: CSc 520 Principles of Programming Languages

520—Spring 2005—48

Readings and References

Squeak download: http://www.squeak.org/download/

Squeak documentation:http://www.squeak.org/documentation/index.html

Squeak manual:http://www.phaidros.com/DIGITALIS/englisch/sqk/sqk00002.htm

http://www.cosc.canterbury.ac.nz/˜wolfgang/cosc205/smalltalk1.html#link

http://www.cosc.canterbury.ac.nz/˜wolfgang/cosc205/labs/labs98.html

Read Scott: pp. 536,577–580.

[27]