syntax analysis
play

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)


  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)

  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)

  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)

  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)

  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)

  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)

  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”

  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.)

  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

  10. “Number hierarchy” Object Magnitude Number Integer Fraction Float

  11. “Extended Number hierarchy” Object Magnitude Natural Number Integer Fraction Float LargeInteger SmallInteger LargePositiveInteger LargeNegativeInteger

  12. Instance protocol for Magnitude equality (like Magnitudes) = aMagnitude comparison (ditto) < aMagnitude comparison (ditto) > aMagnitude comparison (ditto) <= aMagnitude comparison (ditto) >= aMagnitude minimum (ditto) min: aMagnitude maximum (ditto) max: aMagnitude Subclasses: Date , Natural • Compare Date with Date , Natural w/ Natural , . . .

  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})) )

  14. Instance protocol for Number negated reciprocal absolute value abs addition + aNumber subtraction - aNumber multiplication * aNumber division (may answer a / aNumber Fraction !) sign check isNegative sign check isNonnegative sign check isStrictlyPositive

  15. More instance protocol for Number class of receiver, value of coerce: aNumber argument conversion asInteger conversion asFraction conversion asFloat

  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

  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)))

  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 ... )

  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))

  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) ...)

  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))

Recommend


More recommend