"simple test case" |x| x := 1. x printNl. "prints 1" ! " free form input: " |x| x := 1. x printNl . ! "prints 1" " other primitive types: " $m printNl . ! "prints $m" 'hello world' printNl . ! "prints 'hello world' " #(1 $a 'xyz') printNl . ! "prints (1 $a 'xyz') " (#(1 $a 'xyz') at: 3) printNl . ! "prints 'xyz' " " expressions: " (1+2) printNl . ! "prints 3 " (1+'a') printNl . ! "prints 'a' error: did not understand #generality" " unary message selector: " 3 printNl . ! "prints 3" " binary message selector: " (4 + 7) printNl . ! "prints 11" " precedence: " 3 + 4 printNl ! " keyword selectors: " |anArray| anArray := Array new: 4. (anArray at: 3 put: 'xyz') printNl . anArray printNl . "prints (nil nil 'xyz' nil ) " ! " blocks: " |index anArray aBlock| anArray := Array new: 4 . index := 1 . aBlock := [index := index + 1. anArray at: index put: index] . anArray printNl . "prints (nil nil nil nil ) " aBlock value . anArray printNl . "prints (nil 2 nil nil ) " aBlock value . anArray printNl . "prints (nil 2 3 nil ) " ! " Booleans control conditionals: " |a b| a := 10. (a < 0) ifTrue: [b := 0] ifFalse: [b := a sqrt] . b printNl . "prints 3.16227766016837 " ! " integers know how to repeat blocks: " |x| x := 49 . 4 timesRepeat: [x := x squared] . x printNl . "prints 1104427674243920646305299201, or 49^16 " ! " blocks know how to control while loops: " |a b| a := 1 . b := 10 . [a <= b] whileTrue: [ a printNl . "prints 1, then 2, ... 10" a := a + 1 . ] . ! " blocks can take parameters: " ([:elem| elem sqrt] value: 4) printNl . ! "prints 2.0 " ([:elem| elem sqrt] value) printNl . ! "erroneous" ([:a :b | a + b] value: 3 value: 7) printNl . ! " defining a new class: Stack " Object subclass: #Stack instanceVariableNames: 'count elements' classVariableNames: 'MaxDepth' poolDictionaries: '' category: 'Example' . ! Stack class methodsFor: 'creation'! initialize " sets default depth " MaxDepth := 100 ! new "builds a new stack of default depth" ^ super new init: MaxDepth ! new: desiredDepth "builds new stack of given depth" ^ super new init: desiredDepth ! ! Stack methodsFor: 'initialization'! init: depth count := 0. elements := Array new: depth ! ! Stack methodsFor: 'access'! empty ^ (count = 0) . ! push: elem elements size <= count ifTrue: [self error: 'Stack overflow'] ifFalse: [ count := count + 1. elements at: count put: elem . ] . ! pop |top| self empty ifTrue: [self error: 'Stack is empty'] ifFalse: [ top := elements at: count. count := count - 1. ^ top ] ! ! " initial tests " |aStack| aStack := Stack new: 2 . aStack empty printNl . (aStack push: 3) empty printNl . (aStack push: 4) . (aStack pop) printNl . ! " using stacks " |aStack| Stack initialize . " you must initialize or plain Stack new fails." aStack := Stack new: 4 . aStack push: 4 . (aStack pop) printNl . "prints 4 " ! " reloading this file " Object methodsFor: 'redo'! redo (FileStream open: 'examples.st' mode: 'r') fileIn . ! ! " subclass of Stack: IntegerStack " Stack subclass: #IntegerStack instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Example' ! IntegerStack methodsFor: 'access'! push: elem count >= elements size ifTrue: [self error: 'Stack overflow'] ifFalse: [ elem class = Integer ifTrue: [ count := count + 1. elements at: count put: elem ] ifFalse: [self error: 'Can only push integers.'] ] ! + aStack |answer i| (self currentDepth) = (aStack currentDepth) ifFalse: [self error: 'Incompatible stacks for addition'] ifTrue: [ answer := IntegerStack new: (elements size). i := 1. self currentDepth timesRepeat: [ answer push: (elements at: i) + (aStack pos: i). i := i + 1 ]. ^ answer ] ! pos: i "a private method " ^ elements at: i ! currentDepth "a private method " ^ count ! ! " using Integer stacks " |aStack bStack| aStack := IntegerStack new: 4 . bStack := IntegerStack new: 4 . aStack push: 4 . bStack push: 5 . (aStack + bStack) pop printNl . "prints 9 " ! " deep binding " |innerBlock outerBlock aVariable| outerBlock := [ :aVariable | innerBlock := [ aVariable printNl . "print the value" ] ] . innerBlock := [ aVariable printNl . "print the value" ] . outerBlock value: 4 . aVariable := 6 . "Try to confuse the issue" innerBlock value "writes 4" ! " vim:nospell: "