Syntax analysis Definition keywords: (method select: (aBlock) - - PowerPoint PPT Presentation

syntax analysis
SMART_READER_LITE
LIVE PREVIEW

Syntax analysis Definition keywords: (method select: (aBlock) - - PowerPoint PPT Presentation

Syntax analysis Definition keywords: (method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp) Syntax analysis Method defined: (method select: (aBlock)


slide-1
SLIDE 1

Syntax analysis

Definition keywords:

(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)

slide-2
SLIDE 2

Syntax analysis

Method defined:

(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)

slide-3
SLIDE 3

Syntax analysis

Variables defined:

(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)

slide-4
SLIDE 4

Syntax analysis

Expression form—set:

(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)

slide-5
SLIDE 5

Syntax analysis

Expression form—block:

(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)

slide-6
SLIDE 6

Syntax analysis

Expression form—messages sent:

(method select: (aBlock) [locals temp] (set temp ((self class) new)) (self do: [block (x) ((aBlock value: x) ifTrue: (temp add: x))]) temp)

slide-7
SLIDE 7

Method dispatch

To answer a message:

  • 1. Consider the class of the receiver
  • 2. Is the method with that name defined?
  • 3. If so, use it
  • 4. If not, repeat with the superclass

Run out of superclasses? “Message not understood”

slide-8
SLIDE 8

Class mechanisms

Superclass

  • Its methods can answer messages sent to me

Instance variables

  • Parts from which I’m formed
  • Visible only to me (“private”)

Methods

  • Activated whenever I receive a message
  • By mechanism, “public” (anyone can send)
  • By convention, some are private

– Sent to myself, or to an instance I create (Simplest thing that could possibly work.)

slide-9
SLIDE 9

Each class has one of two roles

Abstract class

  • Meant to be inherited from
  • Some (
> 0) subclassResponsibility methods
  • Examples: Boolean, Shape, Collection

Regular (“concrete”) class

  • Meant to be instantiated
  • No subclassResponsibility methods
  • Examples: True, Triangle, List
slide-10
SLIDE 10

“Number hierarchy”

Object Magnitude Number Fraction Float Integer

slide-11
SLIDE 11

“Extended Number hierarchy”

Object Magnitude Natural Number Fraction Float Integer SmallInteger LargeInteger LargePositiveInteger LargeNegativeInteger

slide-12
SLIDE 12

Instance protocol for Magnitude

= aMagnitude equality (like Magnitudes) < aMagnitude comparison (ditto) > aMagnitude comparison (ditto) <= aMagnitude comparison (ditto) >= aMagnitude comparison (ditto) min: aMagnitude minimum (ditto) max: aMagnitude maximum (ditto) Subclasses: Date, Natural

  • Compare Date with Date, Natural w/Natural, . . .
slide-13
SLIDE 13

Implementation of Magnitude: Reuse

(class Magnitude ; abstract class [subclass-of Object] (method = (x) (self subclassResponsibility)) ; may not inherit = from Object (method < (x) (self subclassResponsibility)) (method > (y) (y < self)) (method <= (x) ((self > x) not)) (method >= (x) ((self < x) not)) (method min: (aMag) ((self < aMag) ifTrue:ifFalse: {self} {aMag})) (method max: (aMag) ((self > aMag) ifTrue:ifFalse: {self} {aMag})) )

slide-14
SLIDE 14

Instance protocol for Number

negated reciprocal abs absolute value + aNumber addition

  • aNumber

subtraction * aNumber multiplication / aNumber division (may answer a Fraction!) isNegative sign check isNonnegative sign check isStrictlyPositive sign check

slide-15
SLIDE 15

More instance protocol for Number

coerce: aNumber class of receiver, value of argument asInteger conversion asFraction conversion asFloat conversion

slide-16
SLIDE 16

Your turn: Object-oriented design

Given Magnitude, which of these methods can be implemented in terms of others? (poll) negated * coerce: reciprocal / asInteger abs isNegative asFraction + isNonnegative asFloat

  • isStrictlyPositive
slide-17
SLIDE 17

Number methods

(method - (y) (self + (y negated))) (method abs () ((self isNegative) ifTrue:ifFalse: {(self negated)} {self})) (method / (y) (self * (y reciprocal))) (method isNegative () (self < (self coerce: 0))) (method isNonnegative () (self >= (self coerce: 0))) (method isStrictlyPositive () (self > (self coerce: 0)))

slide-18
SLIDE 18

Example class Fraction: initialization

(class Fraction [subclass-of Number] [ivars num den] ;; representation (concrete!) ;; invariants by signReduce, divReduce (class-method num:den: (a b) ((self new) initNum:den: a b)) (method initNum:den: (a b) ; private (self setNum:den: a b) (self signReduce) (self divReduce)) (method setNum:den: (a b) (set num a) (set den b) self) ; private .. other methods of class Fraction ... )

slide-19
SLIDE 19

Information revealed to self

“Instance variables” num and den

  • Directly available
  • Always and only go with self

Object knows its own representation, invariants, private methods:

(method asFraction () self) (method print () (num print) (’/ print) (den print) self) (method reciprocal () (((Fraction new) setNum:den: den num) signReduce))

slide-20
SLIDE 20

Information revealed to self: your turn

How would you implement coerce:? (Value of argument, representation of receiver)

(method asFraction () self) (method print () (num print) (’/ print) (den print) self) (method reciprocal () (((Fraction new) setNum:den: den num) signReduce)) (method coerce: (aNumber) ...)

slide-21
SLIDE 21

Information revealed to self: your turn

How would you implement coerce:? (Value of argument, representation of receiver)

(method asFraction () self) (method print () (num print) (’/ print) (den print) self) (method reciprocal () (((Fraction new) setNum:den: den num) signReduce)) (method coerce: (aNumber) (aNumber asFraction))