[pe.cust.sources] Little Smalltalk Source, *New* Part 20 of 20

earlw@pesnta.UUCP (Earl Wallace) (06/13/85)

#! /bin/sh 
#
# This is an another posting of the Little Smalltalk source, the last posting
# of this source went out in 5 parts and they were too big (>200k) for most
# sites so I redid the whole mess to keep the files around the 50k range.
#
# The complete set is now 20 parts.
#
# P.S. - If you don't receive all 20 parts within 5 days, drop me a line.
#	 Also, I have the Rand sources of May 1984, if someone has a more
#	 updated copy, I'll be happy to post them (or YOU can post them :-))
# 
# -earlw@pesnta
#
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	symbols
#	tests/4queen.out
#	tests/4queen.st
#	tests/8queen.st
#	tests/Makefile
#	tests/basic.out
#	tests/basic.st
#	tests/blocks.out
#	tests/blocks.st
#	tests/check.st
#	tests/collect.out
#	tests/collect.st
#	tests/cond.st
#	tests/control.st
#	tests/copy.out
#	tests/copy.st
#	tests/fib.st
#	tests/file.out
#	tests/file.st
#	tests/foo
#	tests/fork.out
#	tests/fork.st
#	tests/generator.st
#	tests/in
#	tests/new.out
#	tests/new.st
#	tests/num.out
#	tests/num.st
#	tests/phil.out
#	tests/phil.st
#	tests/prime.st
#	tests/prime3.st
#	tests/prime4.st
#	tests/primes.out
#	tests/primes.st
#	tests/prob.st
#	tests/sim1.out
#	tests/sim1.st
#	tests/sim2.out
#	tests/sim2.st
#	tests/sim3.out
#	tests/sim3.st
#	tests/super.out
#	tests/super.st
#	tests/temp.st
#	tests/turing.st
#	tests/visitor.st
# This archive created: Thu Jun 13 11:33:20 1985
# By:	Earl Wallace (Perkin-Elmer Data Systems Group / Customer Service)
export PATH; PATH=/bin:$PATH
if test -f 'symbols'
then
	echo shar: will not over-write existing file "'symbols'"
else
cat << \SHAR_EOF > 'symbols'
!
&
(
)
*
+
,
-
/
//
<
<=
=
==
>
>=
@
Array
ArrayedCollection
BLOCKED
Bag
Block
Boolean
ByteArray
Char
Class
Collection
Complex
Dictionary
False
File
Float
Integer
Interpreter
Interval
KeyedCollection
List
Little Smalltalk
Magnitude
Main
Number
Object
OrderedCollection
Point
Process
READY
Radian
Random
SUSPENDED
Semaphore
SequenceableCollection
Set
Smalltalk
String
Symbol
TERMINATED
True
UndefinedObject
[
\\
\\\\
]
^
abs
add:
add:after:
add:before:
add:withOccurrences:
addAll:
addAllFirst:
addAllLast:
addFirst:
addLast:
after:
allMask:
and:
anyMask:
arcCos
arcSin
arcTan
argerror
asArray
asBag
asCharacter
asDictionary
asFloat
asFraction
asInteger
asList
asLowercase
asOrderedCollection
asSet
asString
asSymbol
asUppercase
asciiValue
at:
at:ifAbsent:
at:put:
atAll:put:
atAllPut:
before:
between:and:
binaryDo:
bitAnd:
bitAt:
bitInvert
bitOr:
bitShift:
bitXor:
block
blockedProcessQueue
ceiling
checkBucket:
class
cleanUp
coerce:
collect:
commands:
compareError
copy
copyArguments:
copyArguments:to:
copyFrom:
copyFrom:length:
copyFrom:to:
copyWith:
copyWithout:
cos
count
currAssoc
currBucket
current
currentBucket
currentKey
currentList
date
debug:
deepCopy
deepCopy:
detect:
detect:ifAbsent:
detect:ifNone:
dict
dictionary
digitValue
digitValue:
display
displayAssign
dist:
do:
doPrimitive:
doPrimitive:withArguments:
edit
equals:startingAt:
eqv:
error:
even
excessSignals
executeWith:
exp
factorial
findAssociation:inList:
findFirst:
findFirst:ifAbsent:
findLast
findLast:
findLast:ifAbsent:
first
firstKey
floor
floorLog:
fork
forkWith:
fractionPart
free:
from:
from:to:
from:to:by:
gamma
gcd:
getList:
grid:
hashNumber:
hashTab
hashTable
highBit
i
ifFalse:
ifFalse:ifTrue:
ifTrue:
ifTrue:ifFalse:
inRange:
includes:
includesKey:
indexOf:
indexOf:ifAbsent:
indexOfSubCollection:startingAt:
indexOfSubCollection:startingAt:ifAbsent:
init:
init:super:
init:super:numVars:
inject:into:
integerPart
isAlphaNumeric
isDigit
isEmpty
isKindOf:
isLetter
isLowercase
isMemberOf:
isNil
isSeparator
isUppercase
isVowel
keys
keysDo:
keysSelect:
last
lastKey
lcm:
list
ln
log:
lower
main
max:
maxContext:
maxtype:
methods:
min:
modeCharacter
modeInteger
modeString
name:
negated
negative
new
new:
newProcess
newProcessWith:
next
next:
noDisplay
noMask:
not
notNil
nothing
occurrencesOf:
odd
opError
open:
open:for:
or:
perform:
perform:withArguments:
pi
positive
print
printString
put:
quo:
radians
radix:
raisedTo:
raisedToInteger:
randInteger:
randomize
read
reciprocal
reject:
rem:
remove:
remove:ifAbsent:
removeAll:
removeError
removeFirst
removeKey:
removeKey:ifAbsent:
removeLast
removed
replaceFrom:to:with:
replaceFrom:to:with:startingAt:
respondsTo
respondsTo:
resume
reverseDo:
reversed
roundTo:
rounded
sameAs:
seed
select:
setCurrentLocation:
sh:
shallowCopy
shallowCopy:
sign
signal
sin
size
smalltalk
sort
sort:
sqrt
squared
state
step
strictlyPositive
superClass
superClass:
suspend
tan
temp
termErr:
terminate
time:
timesRepeat:
to:
to:by:
transpose
truncateTo:
truncated
truncatedGrid:
unblock
upper
value
value:
value:value:
value:value:value:
value:value:value:value:
value:value:value:value:value:
values
variables
variables:
view
wait
whileFalse:
whileTrue:
with:do:
withArguments:
write:
x
x:
xor:
xvalue
y
y:
yield
yvalue
|
~
~=
~~
SHAR_EOF
if test 3253 -ne "`wc -c < 'symbols'`"
then
	echo shar: error transmitting "'symbols'" '(should have been 3253 characters)'
fi
fi # end of overwriting check
if test -f 'tests/4queen.out'
then
	echo shar: will not over-write existing file "'tests/4queen.out'"
else
cat << \SHAR_EOF > 'tests/4queen.out'
Little Smalltalk
	Col 1 Row 2
Col 2 Row 4
Col 3 Row 1
Col 4 Row 3
Main
	

SHAR_EOF
if test 74 -ne "`wc -c < 'tests/4queen.out'`"
then
	echo shar: error transmitting "'tests/4queen.out'" '(should have been 74 characters)'
fi
fi # end of overwriting check
if test -f 'tests/4queen.st'
then
	echo shar: will not over-write existing file "'tests/4queen.st'"
else
cat << \SHAR_EOF > 'tests/4queen.st'
Class Queen
| myrow mycolumn neighbor boardsize |
[
        build: aQueen col: aNumber size: brdmax

                neighbor <- aQueen.
                mycolumn <- aNumber.
                myrow <- 1.
                boardsize <- brdmax.
                neighbor first.
                ^ self

|       checkCol: colNumber row: rowNumber      | cd |
                (rowNumber = myrow) ifTrue: [ ^ false ].
                cd <- colNumber - mycolumn.
                ((myrow + cd) = rowNumber) ifTrue: [ ^ false ].
                ((myrow - cd) = rowNumber) ifTrue: [ ^ false ].
                (neighbor isNil) ifFalse:
                        [ ^ neighbor checkCol: colNumber row: rowNumber ].
                ^ true

|       first
                myrow <- 1.
                ^ self checkrow

|       next
                myrow <- myrow + 1.
                ^ self checkrow

|       checkrow
                (neighbor isNil) ifTrue: [^ myrow].
                [myrow <= boardsize] whileTrue:
                        [(neighbor checkCol: mycolumn row: myrow)
                                ifTrue: [^ myrow]
                                ifFalse: [myrow <- myrow + 1] ].
                ((neighbor next) isNil) ifTrue: [^ nil].
                ^ self first

|       printboard
                (neighbor isNil) ifFalse: [ neighbor printboard].
                ('Col ', mycolumn asString , ' Row ' ,
                    myrow asString) print
]

Class Main
| lastq |
[
        main | size |

                size <- 4.
                lastq <- nil.
                (1 to: size) do: [:x |
                         lastq <- Queen new build: lastq col: x size: size ].
                lastq first.
                lastq printboard
]
SHAR_EOF
if test 1731 -ne "`wc -c < 'tests/4queen.st'`"
then
	echo shar: error transmitting "'tests/4queen.st'" '(should have been 1731 characters)'
fi
fi # end of overwriting check
if test -f 'tests/8queen.st'
then
	echo shar: will not over-write existing file "'tests/8queen.st'"
else
cat << \SHAR_EOF > 'tests/8queen.st'
Class Queen
| myrow mycolumn neighbor boardsize |
[
        build: aQueen col: aNumber size: brdmax

                neighbor <- aQueen.
                mycolumn <- aNumber.
                myrow <- 1.
                boardsize <- brdmax.
                neighbor first.
                ^ self

|       checkCol: colNumber row: rowNumber      | cd |
                (rowNumber = myrow) ifTrue: [ ^ false ].
                cd <- colNumber - mycolumn.
                ((myrow + cd) = rowNumber) ifTrue: [ ^ false ].
                ((myrow - cd) = rowNumber) ifTrue: [ ^ false ].
                (neighbor isNil) ifFalse:
                        [ ^ neighbor checkCol: colNumber row: rowNumber ].
                ^ true

|       first
                myrow <- 1.
                ^ self checkrow

|       next
                myrow <- myrow + 1.
                ^ self checkrow

|       checkrow
                (neighbor isNil) ifTrue: [^ myrow].
                [myrow <= boardsize] whileTrue:
                        [(neighbor checkCol: mycolumn row: myrow)
                                ifTrue: [^ myrow]
                                ifFalse: [myrow <- myrow + 1] ].
                ((neighbor next) isNil) ifTrue: [^ nil].
                ^ self first

|       printboard
                (neighbor isNil) ifFalse: [ neighbor printboard].
                ('Col ', mycolumn asString , ' Row ' ,
                    myrow asString) print
]

Class Main
| lastq |
[
        main | size |

                size <- 8.
                lastq <- nil.
                (1 to: size) do: [:x |
                         lastq <- Queen new build: lastq col: x size: size ].
                lastq first.
                lastq printboard
]
SHAR_EOF
if test 1731 -ne "`wc -c < 'tests/8queen.st'`"
then
	echo shar: error transmitting "'tests/8queen.st'" '(should have been 1731 characters)'
fi
fi # end of overwriting check
if test -f 'tests/Makefile'
then
	echo shar: will not over-write existing file "'tests/Makefile'"
else
cat << \SHAR_EOF > 'tests/Makefile'
.SUFFIXES : .st .test

BINDIR = ../bin

FILES = Makefile in *.st *.out

.st.test:
	$(BINDIR)/st -m $*.st <in | diff - $*.out

install:
	@echo Performing Self Checking Tests
	-make basic.test
	-make blocks.test
	-make fork.test
	-make new.test
	-make super.test
	-make copy.test
	-make num.test
	-make file.test
	-make primes.test
	-make collect.test
	-make 4queen.test
	@echo The following produce cycles, thus have nonzero differences
	-make phil.test
	@echo Differences in random numbers may change results in following
	-make sim1.test
	-make sim2.test
	@echo Finished Self Checking Tests
	
bundle:
	bundle $(FILES) >../tests.bundle
SHAR_EOF
if test 636 -ne "`wc -c < 'tests/Makefile'`"
then
	echo shar: error transmitting "'tests/Makefile'" '(should have been 636 characters)'
fi
fi # end of overwriting check
if test -f 'tests/basic.out'
then
	echo shar: will not over-write existing file "'tests/basic.out'"
else
cat << \SHAR_EOF > 'tests/basic.out'
Little Smalltalk
	88
3.14159
this is it
#( #this #is #also #it )
True
shallowCopy
respondsTo:
printString
print
notNil
next
isNil
isMemberOf:
isKindOf:
first
error:
do:
deepCopy
copy
class
asSymbol
asString
~=
=
~~
==
#( 22 17 )
time:
sh:
perform:withArguments:
noDisplay
doPrimitive:withArguments:
displayAssign
display
debug:
date
Main
	

SHAR_EOF
if test 341 -ne "`wc -c < 'tests/basic.out'`"
then
	echo shar: error transmitting "'tests/basic.out'" '(should have been 341 characters)'
fi
fi # end of overwriting check
if test -f 'tests/basic.st'
then
	echo shar: will not over-write existing file "'tests/basic.st'"
else
cat << \SHAR_EOF > 'tests/basic.st'
Class Main
[
	main
		88 print.
		3.14159 print.
		'this is it' print.
		#(this is also it) print.
		88 respondsTo: #+ ; print.
		Object respondsTo.
		smalltalk at: 3 put: #(22 17).
		(smalltalk at: 3) print.
		Smalltalk respondsTo.
]
SHAR_EOF
if test 234 -ne "`wc -c < 'tests/basic.st'`"
then
	echo shar: error transmitting "'tests/basic.st'" '(should have been 234 characters)'
fi
fi # end of overwriting check
if test -f 'tests/blocks.out'
then
	echo shar: will not over-write existing file "'tests/blocks.out'"
else
cat << \SHAR_EOF > 'tests/blocks.out'
Little Smalltalk
	correct-1
correct-2
correct-3
correct-4
correct-5
correct-6
correct-7
correct-8
Main
	

SHAR_EOF
if test 106 -ne "`wc -c < 'tests/blocks.out'`"
then
	echo shar: error transmitting "'tests/blocks.out'" '(should have been 106 characters)'
fi
fi # end of overwriting check
if test -f 'tests/blocks.st'
then
	echo shar: will not over-write existing file "'tests/blocks.st'"
else
cat << \SHAR_EOF > 'tests/blocks.st'
Class Main
[
	main
		(2 < 3) ifTrue: ['correct-1' print].
		((2 < 3) ifTrue: ['correct-2']) print.
		[:x | x print] value: 'correct-3' .
		((2 < 3) or: [3 < 4]) ifTrue: ['correct-4' print].
		((2 > 3) or: [3 < 4]) ifTrue: ['correct-5' print].
		((2 < 3) and: [3 < 4]) ifTrue: ['correct-6' print].
		((2 > 3) and: [3 < 4]) ifFalse: ['correct-7' print].
		self test1 print
|
	test1
		self test2: [^ 'correct-8'].
		'should not print' print
|
	test2: aBlock
		self test3: aBlock.
		'should not print' print
|
	test3: bBlock
		bBlock value.
		'should not print' print
]
SHAR_EOF
if test 566 -ne "`wc -c < 'tests/blocks.st'`"
then
	echo shar: error transmitting "'tests/blocks.st'" '(should have been 566 characters)'
fi
fi # end of overwriting check
if test -f 'tests/check.st'
then
	echo shar: will not over-write existing file "'tests/check.st'"
else
cat << \SHAR_EOF > 'tests/check.st'
Class CheckBook
| balance |
[
	new
		balance <- 0
|
	+ amount
		balance <- balance + amount.
		^ balance
|
	- amount
		balance <- balance - amount.
		^ balance
]

SHAR_EOF
if test 163 -ne "`wc -c < 'tests/check.st'`"
then
	echo shar: error transmitting "'tests/check.st'" '(should have been 163 characters)'
fi
fi # end of overwriting check
if test -f 'tests/collect.out'
then
	echo shar: will not over-write existing file "'tests/collect.out'"
else
cat << \SHAR_EOF > 'tests/collect.out'
Little Smalltalk
	example
7
#( $e $x $a $m $p $l $e )
2
Bag ( $x $l $m $p $a $e $e )
Set ( $l $p $m $a $x $e )
exampl
Dictionary ( 1 @ $e 2 @ $x 3 @ $a 4 @ $m 5 @ $p 6 @ $l 7 @ $e )
List ( $e $x $a $m $p $l $e )
List ( 0.666667 $e $x $a $m $p $l $e 12.5 13.25 14 14.75 )
14.75
List ( 0.666667 $e $x $a $m $p $l $e 12.5 13.25 14 $a 7 )
xmpl
xampl
aeelmpx
xpmleea
Set ( 7 6 5 4 3 2 1 )
Bag ( $x $l $m $p $a $e $e )
xxxmxlx
Bag ( 1 2 3 4 5 6 )
6
Bag ( 1 3 5 )
Set ( 2 1 0 )
3
read
Main
	

SHAR_EOF
if test 486 -ne "`wc -c < 'tests/collect.out'`"
then
	echo shar: error transmitting "'tests/collect.out'" '(should have been 486 characters)'
fi
fi # end of overwriting check
if test -f 'tests/collect.st'
then
	echo shar: will not over-write existing file "'tests/collect.st'"
else
cat << \SHAR_EOF > 'tests/collect.st'
Class Main
| i |
[
	main
		self test1.
		self test2.
		self test3
|
	test1		| j |
		(i <- 'example') print.
		i size print.
		i asArray print.
		(i occurrencesOf: $e) print.
		i asBag print.
		(j <- i asSet) print.
		j asString reversed print.
		i asDictionary print.
		(j <- i asList) print.
		j addFirst: 2 / 3.
		j addAllLast: (12.5 to: 15 by: 0.75).
		j print.
		j removeLast print.
		(j , #($a 7) ) print.
		(i reject: [:x | x isVowel] ) print.
		(i copyWithout: $e) print.
		i sort print.
		(i sort: [:x :y | y < x]) print.
		i keys print.
		i values print.
		(i atAll: (1 to: 7 by: 2) put: $x) print
|
	test2			| j |
		i <- (1 to: 6) asBag print.
		i size print.
		(i select: [:x | (x \\ 2) strictlyPositive] ) print.
		(j <- (i collect: [:x | x \\ 3]) asSet ) print.
		j size print
|
	test3
		('bead' at: 1 put: $r) print
]
SHAR_EOF
if test 832 -ne "`wc -c < 'tests/collect.st'`"
then
	echo shar: error transmitting "'tests/collect.st'" '(should have been 832 characters)'
fi
fi # end of overwriting check
if test -f 'tests/cond.st'
then
	echo shar: will not over-write existing file "'tests/cond.st'"
else
cat << \SHAR_EOF > 'tests/cond.st'
Class Main
[
	main			| i |
		((2 < 3) ifTrue: ['correct']) print.
		(2 < 3) ifTrue: ['correct' print ].
		i <- 1.
		[i < 3] whileTrue: [i <- i + 1].
		(i >= 3) ifTrue: ['correct' print]
]

SHAR_EOF
if test 189 -ne "`wc -c < 'tests/cond.st'`"
then
	echo shar: error transmitting "'tests/cond.st'" '(should have been 189 characters)'
fi
fi # end of overwriting check
if test -f 'tests/control.st'
then
	echo shar: will not over-write existing file "'tests/control.st'"
else
cat << \SHAR_EOF > 'tests/control.st'
"
     control the values produced by a generator
"
Class ControlGenerator :Generator
| firstGenerator secondGenerator
  currentFirst currentSecond
  controlBlock computeBlock |
[
        initA: fGen b: sGen control: aBlock compute: anotherBlock
                firstGenerator <- fGen.
                secondGenerator <- sGen.
                controlBlock <- aBlock.
                computeBlock <- anotherBlock

|       first
                currentFirst <- firstGenerator first.
                currentSecond <- secondGenerator first.
                (currentFirst isNil & currentSecond isNil) ifTrue: [^ nil].
                ^ self controlGeneratorNext

|       next
                ^ self controlGeneratorNext

|       controlGeneratorNext    | control returnedValue |
                control <- 0.
                [ control anyMask: 12] whileFalse: [
                  control <- controlBlock value: currentFirst
                                          value: currentSecond.
                   (control allMask: 64) ifTrue: [^nil].
                   (control allMask: 32) ifTrue:
                                [currentFirst <- firstGenerator first].
                   (control allMask: 16) ifTrue:
                                [currentSecond <- secondGenerator first].
                   (control allMask: 12)
                      ifTrue:
                          [returnedValue <- computeBlock
                               value: currentFirst value: currentSecond]
                      ifFalse: [
                         (control allMask: 8) ifTrue:
                           [returnedValue <- computeBlock value: currentFirst].
                         (control allMask: 4) ifTrue:
                           [returnedValue <- computeBlock value: currentSecond].
                         ].
                   (control allMask: 2) ifTrue:
                           [currentFirst <- firstGenerator next].
                   (control allMask: 1) ifTrue:
                           [currentSecond <- secondGenerator next].
                  ].
                ^ returnedValue
]
SHAR_EOF
if test 2100 -ne "`wc -c < 'tests/control.st'`"
then
	echo shar: error transmitting "'tests/control.st'" '(should have been 2100 characters)'
fi
fi # end of overwriting check
if test -f 'tests/copy.out'
then
	echo shar: will not over-write existing file "'tests/copy.out'"
else
cat << \SHAR_EOF > 'tests/copy.out'
Little Smalltalk
	test value test value 17
test value test value 12
#( test value 17 )
#( test value 12 )
Main
	

SHAR_EOF
if test 114 -ne "`wc -c < 'tests/copy.out'`"
then
	echo shar: error transmitting "'tests/copy.out'" '(should have been 114 characters)'
fi
fi # end of overwriting check
if test -f 'tests/copy.st'
then
	echo shar: will not over-write existing file "'tests/copy.st'"
else
cat << \SHAR_EOF > 'tests/copy.st'
Class Main
| i j k l |
[
	main
		i <- Test new.
		i set: 17.
		j <- Test new.
		j set: i.
		k <- j deepCopy.
		l <- j shallowCopy.
		i set: 12.
		k print.
		l print.
		i <- Test new.
		i set: 17.
		j <- #(2).
		j at: 1 put: i.
		k <- j deepCopy.
		l <- j shallowCopy.
		i set: 12.
		k print.
		l print.
]
Class Test
| var |
[
	printString
		^ 'test value ', var printString
|
	set: aVal
		var <- aVal
]

SHAR_EOF
if test 404 -ne "`wc -c < 'tests/copy.st'`"
then
	echo shar: error transmitting "'tests/copy.st'" '(should have been 404 characters)'
fi
fi # end of overwriting check
if test -f 'tests/fib.st'
then
	echo shar: will not over-write existing file "'tests/fib.st'"
else
cat << \SHAR_EOF > 'tests/fib.st'
Class Fib :Generator
| lastNumber  nextToLastNumber |
[
	first
    		nextToLastNumber <- 0.
    		^ lastNumber <- 1
|
  	next			| sum |
    		sum <- nextToLastNumber + lastNumber.
    		nextToLastNumber <- lastNumber.
    		^ lastNumber <- sum
]
SHAR_EOF
if test 246 -ne "`wc -c < 'tests/fib.st'`"
then
	echo shar: error transmitting "'tests/fib.st'" '(should have been 246 characters)'
fi
fi # end of overwriting check
if test -f 'tests/file.out'
then
	echo shar: will not over-write existing file "'tests/file.out'"
else
cat << \SHAR_EOF > 'tests/file.out'
Little Smalltalk
	niaM ssalC
[
| g f |		niam	
.'ts.elif' :nepo ; wen eliF -< f		
.'w' :rof 'oof' :nepo ; wen eliF -< g		
.]desrever x :etirw g | x:[ :od f		
.'r' :rof 'oof' :nepo ; wen eliF -< g		
.]tnirp x | x:[ :od g		
.retcarahCedom f		
.tnirp tsrif f		
.] tnirp txen f [ :taepeRsemit 01		
.tnirp )2 :ta f(		
.tnirp yeKtnerruc f		
.tnirp ezis f		
]

$C
$l
$a
$s
$s
$ 
$M
$a
$i
$n
$

$a
3
335
Main
	

SHAR_EOF
if test 403 -ne "`wc -c < 'tests/file.out'`"
then
	echo shar: error transmitting "'tests/file.out'" '(should have been 403 characters)'
fi
fi # end of overwriting check
if test -f 'tests/file.st'
then
	echo shar: will not over-write existing file "'tests/file.st'"
else
cat << \SHAR_EOF > 'tests/file.st'
Class Main
[
	main		| f g |
		f <- File new ; open: 'file.st'.
		g <- File new ; open: 'foo' for: 'w'.
		f do: [:x | g write: x reversed].
		g <- File new ; open: 'foo' for: 'r'.
		g do: [:x | x print].
		f modeCharacter.
		f first print.
		10 timesRepeat: [ f next print ].
		(f at: 2) print.
		f currentKey print.
		f size print.
]

SHAR_EOF
if test 335 -ne "`wc -c < 'tests/file.st'`"
then
	echo shar: error transmitting "'tests/file.st'" '(should have been 335 characters)'
fi
fi # end of overwriting check
if test -f 'tests/foo'
then
	echo shar: will not over-write existing file "'tests/foo'"
else
cat << \SHAR_EOF > 'tests/foo'
niaM ssalC
[
| g f |		niam	
.'ts.elif' :nepo ; wen eliF -< f		
.'w' :rof 'oof' :nepo ; wen eliF -< g		
.]desrever x :etirw g | x:[ :od f		
.'r' :rof 'oof' :nepo ; wen eliF -< g		
.]tnirp x | x:[ :od g		
.retcarahCedom f		
.tnirp tsrif f		
.] tnirp txen f [ :taepeRsemit 01		
.tnirp )2 :ta f(		
.tnirp yeKtnerruc f		
.tnirp ezis f		
]

SHAR_EOF
if test 335 -ne "`wc -c < 'tests/foo'`"
then
	echo shar: error transmitting "'tests/foo'" '(should have been 335 characters)'
fi
fi # end of overwriting check
if test -f 'tests/fork.out'
then
	echo shar: will not over-write existing file "'tests/fork.out'"
else
cat << \SHAR_EOF > 'tests/fork.out'
Little Smalltalk
	17
23
17
23
17
23
17
23
17
23
17
23
17
23
17
23
17
23
17
23
Main
	

SHAR_EOF
if test 86 -ne "`wc -c < 'tests/fork.out'`"
then
	echo shar: error transmitting "'tests/fork.out'" '(should have been 86 characters)'
fi
fi # end of overwriting check
if test -f 'tests/fork.st'
then
	echo shar: will not over-write existing file "'tests/fork.st'"
else
cat << \SHAR_EOF > 'tests/fork.st'
Class Main
[
	loop1
		10 timesRepeat: [17 print]
|
	loop2
		10 timesRepeat: [23 print]
|
	main
		[self loop1] fork.
		self loop2
]

SHAR_EOF
if test 132 -ne "`wc -c < 'tests/fork.st'`"
then
	echo shar: error transmitting "'tests/fork.st'" '(should have been 132 characters)'
fi
fi # end of overwriting check
if test -f 'tests/generator.st'
then
	echo shar: will not over-write existing file "'tests/generator.st'"
else
cat << \SHAR_EOF > 'tests/generator.st'
Class Generator :Collection
[
	, aGenerator
		^ DyadicControlGenerator new;
			firstGen: self
			secondGen: aGenerator
			control: [:x :y |
				(x isNil)
					ifTrue:
						[(y isNil)
							ifTrue:  [2r01000000]
							ifFalse: [2r00000101]
						]
					ifFalse: [2r00001010] ]
			compute: [:x | x ]
|
  	collect: xformBlock
    		^ MonadicControlGenerator new;
       			initGen: self deepCopy
       			control: [ :x | 
				(x isNil) 
					ifTrue:  [2r1000] 
					ifFalse: [2r0101] 
				 ]
       			init: []
       			compute: [:x | xformBlock value: x]
|
	first: limit     | count |
    		count <- 0.
		^ MonadicControlGenerator new;
       			initGen: self deepCopy
       			control: [ :x |
                 			(x isNil)
                  			ifTrue:  [2r1000]
                  			ifFalse: [((count <- count + 1) > limit)
                             				ifTrue:  [2r1000]
                             				ifFalse: [2r0101]
                           			 ]
                		 ]
       			init: [count <- 0]
       			compute: [:x | x]
|
  	select: condBlock
    		^ MonadicControlGenerator new;
       			initGen: self deepCopy
       			control: [ :x |
                 		(x isNil)
                  			ifTrue:  [2r1000]
                  			ifFalse: [(condBlock value: x)
                             				ifTrue:  [2r0101]
                             				ifFalse: [2r0001]
                           			 ]
                		 ]
       			init: []
       			compute: [:x | x]
|
  	until: condBlock
    		^ MonadicControlGenerator new;
       			initGen: self deepCopy
       			control: [ :x |
                 		(x isNil)
                  			ifTrue:  [2r1000]
                  			ifFalse: [(condBlock value: x)
                             				ifTrue:  [2r1000]
                             				ifFalse: [2r0101]
                           			 ]
                		 ]
       			init: []
       			compute: [:x | x]
|
	with: aGenerator when: conditionBlock
		^ DyadicControlGenerator new ;
			firstGen: self
			secondGen: aGenerator
			control: [:x :y |
				(x isNil)
					ifTrue: [(y isNil)
						ifTrue:  [2r01000000]
						ifFalse: [2r00000101] ]
					ifFalse: [(y isNil)
						ifTrue:  [2r00001010]
						ifFalse: [(conditionBlock
							value: x value: y)
							ifTrue:  [2r00001010]
							ifFalse: [2r00000101]
							] ] ]
			compute: [:x | x ]
]

Class MonadicControlGenerator :Generator
| subGenerator  currentValue  controlBlock  initBlock  computeBlock |
[
  	initGen: aGenerator 
	control: conBlk 
	init: iniBlk 
	compute: cmpBlk
    		subGenerator <- aGenerator.
    		controlBlock <- conBlk.
    		initBlock <- iniBlk.
    		computeBlock <- cmpBlk.
    		currentValue <- nil
|
  	first
    		(currentValue <- subGenerator first) isNil
      			ifTrue: [^ nil].
    		initBlock value.
    		^ self next
|
  	next     | control  returnedValue |
    		control <- 0.
    		[control anyMask: 2r0100] whileFalse:
      			[
        			control <- controlBlock value: currentValue.

        			(control anyMask: 2r1000) ifTrue:
          				[^ nil].
        			(control anyMask: 2r0100) ifTrue:
          				[returnedValue <- 
						computeBlock value: currentValue].
        			(control anyMask: 2r0010) ifTrue:
          				[currentValue <- subGenerator first].
        			(control anyMask: 2r0001) ifTrue:
				    [currentValue <- subGenerator next]
  			].
    		^ returnedValue
]
Class DyadicControlGenerator :Generator
| firstGenerator secondGenerator
  currentFirst currentSecond
  controlBlock computeBlock |
[
	firstGen: firstGen
	secondGen: secondGen
	control: contBlock
	compute: compBlock

                firstGenerator <- firstGen.
                secondGenerator <- secondGen.
                controlBlock <- contBlock.
                computeBlock <- compBlock

|       first
                currentFirst <- firstGenerator first.
                currentSecond <- secondGenerator first.
                (currentFirst isNil & currentSecond isNil) ifTrue: [^ nil].
                ^ self next

|       next    	| control returnedValue |
                control <- 0.
                [ control anyMask: 2r00001100] whileFalse: [
                  control <- controlBlock value: currentFirst
                                          value: currentSecond.
                   (control allMask: 2r01000000) ifTrue: [^nil].
                   (control allMask: 2r00100000) ifTrue:
                                [currentFirst <- firstGenerator first].
                   (control allMask: 2r00010000) ifTrue:
                                [currentSecond <- secondGenerator first].
                   (control allMask: 2r00001100)
                      ifTrue:
                          [returnedValue <- computeBlock
                               value: currentFirst value: currentSecond]
                      ifFalse: [
                         (control allMask: 2r00001000) ifTrue:
                           [returnedValue <- computeBlock value: currentFirst].
                         (control allMask: 2r00000100) ifTrue:
                           [returnedValue <- computeBlock value: currentSecond].
                         ].
                   (control allMask: 2r00000010) ifTrue:
                           [currentFirst <- firstGenerator next].
                   (control allMask: 2r00000001) ifTrue:
                           [currentSecond <- secondGenerator next].
                  ].
                ^ returnedValue
]
SHAR_EOF
if test 5472 -ne "`wc -c < 'tests/generator.st'`"
then
	echo shar: error transmitting "'tests/generator.st'" '(should have been 5472 characters)'
fi
fi # end of overwriting check
if test -f 'tests/in'
then
	echo shar: will not over-write existing file "'tests/in'"
else
cat << \SHAR_EOF > 'tests/in'
Main new main
SHAR_EOF
if test 14 -ne "`wc -c < 'tests/in'`"
then
	echo shar: error transmitting "'tests/in'" '(should have been 14 characters)'
fi
fi # end of overwriting check
if test -f 'tests/new.out'
then
	echo shar: will not over-write existing file "'tests/new.out'"
else
cat << \SHAR_EOF > 'tests/new.out'
Little Smalltalk
	correct
correct
Main
	

SHAR_EOF
if test 42 -ne "`wc -c < 'tests/new.out'`"
then
	echo shar: error transmitting "'tests/new.out'" '(should have been 42 characters)'
fi
fi # end of overwriting check
if test -f 'tests/new.st'
then
	echo shar: will not over-write existing file "'tests/new.st'"
else
cat << \SHAR_EOF > 'tests/new.st'
Class Acl
| vara |
[
	new
		vara <- 'correct'
|
	printa
		vara print
]

Class Bcl :Acl
| varb |
[
	new
		varb <- 'correct'
|
	printb
		varb print
]

Class Main
[
	main		| i |
		i <- Bcl new .
		i printb .
		i printa
]
SHAR_EOF
if test 218 -ne "`wc -c < 'tests/new.st'`"
then
	echo shar: error transmitting "'tests/new.st'" '(should have been 218 characters)'
fi
fi # end of overwriting check
if test -f 'tests/num.out'
then
	echo shar: will not over-write existing file "'tests/num.out'"
else
cat << \SHAR_EOF > 'tests/num.out'
Little Smalltalk
	$a
True
False
65
A
$A
True
10
7.1
23.1406
23.1407
3.5 radians
15
10
7.1
7
True
False
True
1
7
-6
16rFE
0.2
-2
-1
1
-1
24
19.4481
0.523599 radians
2.07364
2.40824
12.6709
16rE.8A71DE
0.500001
12 @ 100
Main
	

SHAR_EOF
if test 226 -ne "`wc -c < 'tests/num.out'`"
then
	echo shar: error transmitting "'tests/num.out'" '(should have been 226 characters)'
fi
fi # end of overwriting check
if test -f 'tests/num.st'
then
	echo shar: will not over-write existing file "'tests/num.st'"
else
cat << \SHAR_EOF > 'tests/num.st'
Class Main
[
	testChars
		($A max: $a) print.
		(4 between: 3.1 and: (17/3)) print.
		($A < $0) print.
		$A asciiValue print.
		$A asString print.
		$A printString print.
		$A isVowel print.
		$A digitValue print
|
	testNums
		3 + 4.1 ; print.
		3.14159 exp print.
		1 pi exp print.
		3.5 radians print.
		13 roundTo: 5 ; print.
		13 truncateTo: 5 ; print.
		(smalltalk perform: #+ withArguments: #(3 4.1) ) print.
		(smalltalk doPrimitive: 10 withArguments: #(3 4) ) print
|
	testInts
		5 allMask: 4 ; print.
		4 allMask: 5 ; print.
		5 anyMask: 4 ; print.
		5 bitAnd: 3 ; print.
		5 bitOr: 3 ; print.
		5 bitInvert print.
		254 radix: 16 ; print.
		5 reciprocal print.
		-5 // 4 ; print.
		-5 quo: 4 ; print.
		-5 \\ 4 ; print.
		-5 rem: 4 ; print.
		4 factorial print.
|
	testFloats
		2.1 ^ 4 ; print.
		0.5 arcSin print.
		4.3 sqrt print.
		256 log: 10 ; print.
		16rC.ABC print.
		(14.5408 radix: 16) print.
		0.5236 radians sin print.
		(100 @ 12) transpose print.
|
	main
		self testChars.
		self testNums.
		self testInts.
		self testFloats.
]
SHAR_EOF
if test 1052 -ne "`wc -c < 'tests/num.st'`"
then
	echo shar: error transmitting "'tests/num.st'" '(should have been 1052 characters)'
fi
fi # end of overwriting check
if test -f 'tests/phil.out'
then
	echo shar: will not over-write existing file "'tests/phil.out'"
else
cat << \SHAR_EOF > 'tests/phil.out'
Little Smalltalk
	Philosopher 1 is thinking.
Philosopher 2 is thinking.
Philosopher 3 is thinking.
Philosopher 4 is thinking.
Philosopher 1 is eating.
Philosopher 5 is thinking.
Philosopher 3 is eating.
Philosopher 5 is eating.
Philosopher 2 is eating.
Philosopher 4 is eating.
Philosopher 1 is thinking.
Philosopher 2 is thinking.
Philosopher 3 is thinking.
Philosopher 4 is thinking.
Philosopher 1 is eating.
Philosopher 5 is thinking.
Philosopher 3 is eating.
Philosopher 5 is eating.
Philosopher 2 is eating.
Philosopher 4 is eating.
Philosopher 1 is sleeping.
Philosopher 2 is sleeping.
Philosopher 3 is sleeping.
Philosopher 4 is sleeping.
Philosopher 5 is sleeping.
Main
	

SHAR_EOF
if test 681 -ne "`wc -c < 'tests/phil.out'`"
then
	echo shar: error transmitting "'tests/phil.out'" '(should have been 681 characters)'
fi
fi # end of overwriting check
if test -f 'tests/phil.st'
then
	echo shar: will not over-write existing file "'tests/phil.st'"
else
cat << \SHAR_EOF > 'tests/phil.st'
Class  Main
[
	main
		( DiningPhilosophers new: 5 ) dine: 4
]

Class  DiningPhilosophers
| diners  forks  philosophers |
[
	new: aNumber
	    diners <- aNumber.
	    forks <- Array new: aNumber.
	    philosophers <- Array new: aNumber.
	    (1 to: diners) do:
		[ :p | forks at: p put: (Semaphore new: 1).
		       philosophers at: p put: (Philosopher new: p)]

|
	dine: time
	    (1 to: diners) do:
		[ :p | (philosophers at: p)
			    leftFork: (forks at: p)
			    rightFork: (forks at: ((p \\ diners) + 1))].
	    time timesRepeat:
		[(1 to: diners) do: [ :p | (philosophers at: p) philosophize]].
	    (1 to: diners) do:
		[ :p | (philosophers at: p) sleep]
]

Class  Philosopher
| leftFork  rightFork  myName  myPhilosophy |
[
	new:  name
	    myName <- name.
	    myPhilosophy <- [[true] whileTrue:
				[self think.
				 self getForks.
				 self eat.
				 self releaseForks.
				 selfProcess suspend]
			    ] newProcess

|
	leftFork: lfork  rightFork: rfork
	    leftFork <- lfork.
	    rightFork <- rfork
|
	getForks
	    ((myName \\ 2) == 0)
		ifTrue: [leftFork wait.  rightFork wait]
		ifFalse: [rightFork wait.  leftFork wait]
|
	releaseForks
	    leftFork signal.
	    rightFork signal

|
	think
	    ('Philosopher ',(myName asString),' is thinking.') print.
	    10 timesRepeat: [selfProcess yield]
|
	eat
	    ('Philosopher ',(myName asString),' is eating.') print.
	    10 timesRepeat: [selfProcess yield]

|
	philosophize
	    myPhilosophy resume
|
	sleep
	    myPhilosophy terminate.
	    ('Philosopher ',(myName asString),' is sleeping.') print.
	    myPhilosophy <- nil
]
SHAR_EOF
if test 1592 -ne "`wc -c < 'tests/phil.st'`"
then
	echo shar: error transmitting "'tests/phil.st'" '(should have been 1592 characters)'
fi
fi # end of overwriting check
if test -f 'tests/prime.st'
then
	echo shar: will not over-write existing file "'tests/prime.st'"
else
cat << \SHAR_EOF > 'tests/prime.st'
Class Main
[
	main	| x gen |
		gen <- Primes new.
		(smalltalk time: [ x <- gen first.
		[x < 300]
			whileTrue: [ x print. x <- gen next] ] ) print.
]
Class Primes
| lastPrime |
[
	first
		^ lastPrime <- 2
|
	next
		[lastPrime <- lastPrime + 1.
		 self testNumber: lastPrime]
			whileFalse.
		^ lastPrime
|
	testNumber: n
		(Primes new) do: [:x | 
			(x squared > n) ifTrue: [ ^ true ].
			(n \\ x = 0) ifTrue: [ ^ false ] ]
]
SHAR_EOF
if test 428 -ne "`wc -c < 'tests/prime.st'`"
then
	echo shar: error transmitting "'tests/prime.st'" '(should have been 428 characters)'
fi
fi # end of overwriting check
if test -f 'tests/prime3.st'
then
	echo shar: will not over-write existing file "'tests/prime3.st'"
else
cat << \SHAR_EOF > 'tests/prime3.st'
Class Main
[
	main	| x gen |
		gen <- Primes new.
		(smalltalk time: [
		x <- gen first.
		[x < 300]
			whileTrue: [ x print. x <- gen next] ]) print
]
Class Primes
| prevPrimes lastPrime |
[
	first
		prevPrimes <- LinkedList new.
		prevPrimes add: (lastPrime <- 2).
		^ lastPrime
|
	next
		[lastPrime <- lastPrime + 1.
		 self testNumber: lastPrime]
			whileFalse.
		prevPrimes addLast: lastPrime.
		^ lastPrime
|
	testNumber: n
		prevPrimes do: [:x | 
			(x squared > n) ifTrue: [ ^ true ].
			(n \\ x = 0) ifTrue: [ ^ false ] ]
]
SHAR_EOF
if test 533 -ne "`wc -c < 'tests/prime3.st'`"
then
	echo shar: error transmitting "'tests/prime3.st'" '(should have been 533 characters)'
fi
fi # end of overwriting check
if test -f 'tests/prime4.st'
then
	echo shar: will not over-write existing file "'tests/prime4.st'"
else
cat << \SHAR_EOF > 'tests/prime4.st'
Class Main
[
	main	| x gen |
		gen <- Primes new.
		(smalltalk time: [x <- gen first.
		[x < 300]
			whileTrue: [ x print. x <- gen next] ] ) print
]
Class Primes
| prevPrimes lastPrime |
[
	first
		prevPrimes <- Set new.
		prevPrimes add: (lastPrime <- 2).
		^ lastPrime
|
	next
		[lastPrime <- lastPrime + 1.
		 self testNumber: lastPrime]
			whileFalse.
		prevPrimes add: lastPrime.
		^ lastPrime
|
	testNumber: n
		prevPrimes do: [:x | 
			(n \\ x = 0) ifTrue: [ ^ false ] ].
		^ true
]
SHAR_EOF
if test 491 -ne "`wc -c < 'tests/prime4.st'`"
then
	echo shar: error transmitting "'tests/prime4.st'" '(should have been 491 characters)'
fi
fi # end of overwriting check
if test -f 'tests/primes.out'
then
	echo shar: will not over-write existing file "'tests/primes.out'"
else
cat << \SHAR_EOF > 'tests/primes.out'
Little Smalltalk
	2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229
233
239
241
251
257
263
269
271
277
281
283
293
Main
	

SHAR_EOF
if test 245 -ne "`wc -c < 'tests/primes.out'`"
then
	echo shar: error transmitting "'tests/primes.out'" '(should have been 245 characters)'
fi
fi # end of overwriting check
if test -f 'tests/primes.st'
then
	echo shar: will not over-write existing file "'tests/primes.st'"
else
cat << \SHAR_EOF > 'tests/primes.st'
Class Main
[
	main
		(Primes new) do: [:x | x print]
]
Class Primes
| primeGenerator lastFactor |
[
	first
		primeGenerator <- 2 to: 300.
		lastFactor <- primeGenerator first.
		^ lastFactor
|
	next
		primeGenerator <- (Factor new ;
					remove: lastFactor
					from:   primeGenerator ).
		^ lastFactor <- primeGenerator next.
]

Class Factor
| myFactor generator |
[
	remove: factorValue from: generatorValue
		myFactor <- factorValue.
		generator <- generatorValue
|
	next		| possible |
		[(possible <- generator next) notNil]
			whileTrue:
				[(possible \\ myFactor ~= 0)
					ifTrue: [ ^ possible] ].
		^ nil
]


SHAR_EOF
if test 618 -ne "`wc -c < 'tests/primes.st'`"
then
	echo shar: error transmitting "'tests/primes.st'" '(should have been 618 characters)'
fi
fi # end of overwriting check
if test -f 'tests/prob.st'
then
	echo shar: will not over-write existing file "'tests/prob.st'"
else
cat << \SHAR_EOF > 'tests/prob.st'
Class DiscreteProbability
	| randnum |
[
	initialize
		randnum <- Random new

|	next
		^ self inverseDistribution: randnum next

|	computeSample: m outOf: n	
		m > n ifTrue: [^ 0.0]
		^ n factorial / (n - m) factorial
]

Class Geometric	:DiscreteProbability
	| prob |	

[
	mean: m
		prob <- m

|	mean
		^ 1.0 / prob

|	variance
		^ (1.0 - prob) / prob * prob

|	density: x
		x > 0 ifTrue: [^prob * ((1.0-prob) raisedTo: x-1)]
		      ifFalse: [^1.0]

|	inverseDistribution: x
		^ (x ln / (1.0 - prob) ln) ceiling
]

Class Binomial	:DiscreteProbability
	| number prob |
[
	events: num mean: p
		(p between: 0.0 and: 1.0)
		   ifFalse: [self error: 'mean must be > 0'].
		number <- num.
		prob <- p

|	mean
		^ prob

|	variance
		^ prob * (1 - prob)

|	density: x
		(x between: 0.0 and number)
		   ifTrue: [^((self computeSample: x outOf: number)
			/ (self computeSample: x outOf: x))
			* (prob raisedTo: x) * ((1 - prob) raisedTo: number - x)]
		   ifFalse: [^0.0]

|	inverseDistribution: x
		x <= prob
			ifTrue: [^ 1]
			ifFalse: [^ 0]

|	next
	| t |
		t <- 0.
		number timesRepeat: [t <- t + super next].
		^ t
]
SHAR_EOF
if test 1118 -ne "`wc -c < 'tests/prob.st'`"
then
	echo shar: error transmitting "'tests/prob.st'" '(should have been 1118 characters)'
fi
fi # end of overwriting check
if test -f 'tests/sim1.out'
then
	echo shar: will not over-write existing file "'tests/sim1.out'"
else
cat << \SHAR_EOF > 'tests/sim1.out'
Little Smalltalk
	customer received at 4
customer has 3 scoops 
customer received at 5
customer has 3 scoops 
customer received at 8
customer has 3 scoops 
customer received at 10
customer has 3 scoops 
customer received at 13
customer has 3 scoops 
customer received at 14
customer has 3 scoops 
customer received at 19
customer has 3 scoops 
customer received at 23
customer has 3 scoops 
customer received at 27
customer has 3 scoops 
profits are 4.59
Main
	

SHAR_EOF
if test 463 -ne "`wc -c < 'tests/sim1.out'`"
then
	echo shar: error transmitting "'tests/sim1.out'" '(should have been 463 characters)'
fi
fi # end of overwriting check
if test -f 'tests/sim1.st'
then
	echo shar: will not over-write existing file "'tests/sim1.st'"
else
cat << \SHAR_EOF > 'tests/sim1.st'
"
	Simple Minded simulation from Chapter 6 of book
"
Class Main
[
	main		| i |
		i <- IceCreamStore new.
		[i time < 25] whileTrue: [ i proceed ].
		i reportProfits
]

Class Simulation
| currentTime nextEvent nextEventTime |
[
	new
		currentTime <- 0
|

	time
		^ currentTime
|
	addEvent: event at: eventTime
		nextEvent <- event.
		nextEventTime <- eventTime
|
	proceed
		currentTime <- nextEventTime.
		self processEvent: nextEvent
]

Class IceCreamStore :Simulation
| profit rand |
[
	new
		profit <- 0.
		rand <- Random new.
		"rand randomize.  taken out so results remain the same"
		self scheduleArrival
|
	scheduleArrival
		self addEvent: Customer new
			at: (self time + (rand randInteger: 5))
|
	processEvent: event
		('customer received at ', self time printString) print.
		profit <- profit + ( event numberOfScoops * 0.17 ).
		self scheduleArrival
|
	reportProfits
		('profits are ', profit printString) print
]

Class Customer
| rand |
[
	new
		(rand <- Random new) "--randomize (taken out)"
|
	numberOfScoops		| number |
		number <- rand randInteger: 3.
		('customer has ', number printString , ' scoops ') print.
		^ number
]
SHAR_EOF
if test 1141 -ne "`wc -c < 'tests/sim1.st'`"
then
	echo shar: error transmitting "'tests/sim1.st'" '(should have been 1141 characters)'
fi
fi # end of overwriting check
if test -f 'tests/sim2.out'
then
	echo shar: will not over-write existing file "'tests/sim2.out'"
else
cat << \SHAR_EOF > 'tests/sim2.out'
Little Smalltalk
	customer received at 4
group of 7 have 10 scoops 
customer received at 5
group of 7 have 9 scoops 
customer received at 8
group of 7 have 11 scoops 
customer received at 10
group of 7 have 7 scoops 
customer received at 13
group of 7 have 9 scoops 
customer received at 14
group of 7 have 10 scoops 
customer received at 19
group of 7 have 11 scoops 
customer received at 23
group of 7 have 8 scoops 
customer received at 27
group of 7 have 8 scoops 
profits are 14.11
Main
	

SHAR_EOF
if test 495 -ne "`wc -c < 'tests/sim2.out'`"
then
	echo shar: error transmitting "'tests/sim2.out'" '(should have been 495 characters)'
fi
fi # end of overwriting check
if test -f 'tests/sim2.st'
then
	echo shar: will not over-write existing file "'tests/sim2.st'"
else
cat << \SHAR_EOF > 'tests/sim2.st'
"
	Simple Minded simulation from Chapter 6 of book

	IceCream Store -
		single event queue
		multiple group size
		discrete probability on number of scoops selected
"
Class Main
[
	main		| i |
		i <- IceCreamStore new.
		[i time < 25] whileTrue: [ i proceed ].
		i reportProfits
]

Class Simulation
| currentTime nextEvent nextEventTime |
[
	new
		currentTime <- 0
|
	time
		^ currentTime
|
	addEvent: event at: eventTime
		nextEvent <- event.
		nextEventTime <- eventTime
|
	proceed
		currentTime <- nextEventTime.
		self processEvent: nextEvent
]

Class IceCreamStore :Simulation
| profit rand scoopDistribution |
[
	new
		profit <- 0.
		rand <- Random new.
		(scoopDistribution <- DiscreteProbability new)
			defineWeights: #(65 25 10).
		self scheduleArrival
|
	scheduleArrival
		self addEvent: Customer new
			at: (self time + (rand randInteger: 5))
|
	processEvent: event
		('customer received at ', self time printString) print.
		profit <- profit + ((self scoopsFor: event groupSize)  * 0.17 ).
		self scheduleArrival
|	
	scoopsFor: group		| number |
		number <- 0.
		group timesRepeat:
			[number <- number + scoopDistribution next].
		('group of ', group printString, ' have ', number
		printString, ' scoops ') print.
		^ number

|
	reportProfits
		('profits are ', profit printString) print
]

Class Customer
| groupSize |
[
	new
		groupSize <- (Random new "randomize" ) randInteger: 8
|
	groupSize
		^ groupSize
]

Class DiscreteProbability
| weights rand max |
[
	defineWeights: anArray
		weights <- anArray.
		(rand <- Random new) "randomize".
		max <- anArray inject: 0 into: [:x :y | x + y]
|
	next	| index value |
		value <- rand randInteger: max.
		index <- 1.
		[value > (weights at: index)]
			whileTrue: [value <- value - (weights at: index).
					index <- index + 1].
		^ index
]

SHAR_EOF
if test 1804 -ne "`wc -c < 'tests/sim2.st'`"
then
	echo shar: error transmitting "'tests/sim2.st'" '(should have been 1804 characters)'
fi
fi # end of overwriting check
if test -f 'tests/sim3.out'
then
	echo shar: will not over-write existing file "'tests/sim3.out'"
else
cat << \SHAR_EOF > 'tests/sim3.out'
Little Smalltalk
	event received at 3.46877
group of size 7 arrives
take chairs, schedule order
event received at 5.81336
group of size 7 arrives
take chairs, schedule order
event received at 6.46877
group of size 7 orders 10 scoops
event received at 6.81336
group of size 7 orders 9 scoops
event received at 8.81336
group of size 7 leaves
event received at 8.91228
group of size 7 arrives
take chairs, schedule order
event received at 9.46877
group of size 7 leaves
event received at 10.9123
group of size 7 orders 11 scoops
event received at 10.9499
group of size 7 arrives
take chairs, schedule order
event received at 11.1909
group of size 7 arrives
finds no chairs, leave
event received at 11.9123
group of size 7 leaves
event received at 11.9204
group of size 7 arrives
take chairs, schedule order
event received at 12.3266
group of size 7 arrives
finds no chairs, leave
event received at 13.1723
group of size 7 arrives
finds no chairs, leave
event received at 13.6961
group of size 7 arrives
finds no chairs, leave
event received at 13.7641
group of size 7 arrives
finds no chairs, leave
event received at 13.9204
group of size 7 orders 7 scoops
event received at 13.9499
group of size 7 orders 9 scoops
event received at 14.3689
group of size 7 arrives
finds no chairs, leave
event received at 14.3911
group of size 7 arrives
finds no chairs, leave
event received at 16.9499
group of size 7 leaves
event received at 17.9204
group of size 7 leaves
profits are 7.82
Main
	

SHAR_EOF
if test 1481 -ne "`wc -c < 'tests/sim3.out'`"
then
	echo shar: error transmitting "'tests/sim3.out'" '(should have been 1481 characters)'
fi
fi # end of overwriting check
if test -f 'tests/sim3.st'
then
	echo shar: will not over-write existing file "'tests/sim3.st'"
else
cat << \SHAR_EOF > 'tests/sim3.st'
"
	Simple Minded simulation from Chapter 6 of book

	IceCream Store -
		multiple event queue
"
Class Main
[
	main		| i |
		i <- IceCreamStore new.
		[i time < 60] whileTrue: [ i proceed ].
		i reportProfits
]

Class Simulation
| currentTime eventQueue |
[
	new
		eventQueue <- Dictionary new.
		currentTime <- 0
|
	time
		^ currentTime
|
	addEvent: event at: eventTime
		(eventQueue includesKey: eventTime)
			ifTrue: [(eventQueue at: eventTime) add: event]
			ifFalse: [eventQueue at: eventTime
					put: (Set new ; add: event)]
|	
	addEvent: event next: timeIncrement
		self addEvent: event at: currentTime + timeIncrement
|
	proceed		| minTime eventset event |
		minTime <- 99999.
		eventQueue keysDo:
			[:x | (x < minTime) ifTrue: [minTime <- x]].
		currentTime <- minTime.
		eventset <- eventQueue at: minTime ifAbsent: [^nil].
		event <- eventset first.
		eventset remove: event.
		(eventset isEmpty) ifTrue: [eventQueue removeKey: minTime].
		self processEvent: event
]

Class IceCreamStore :Simulation
| profit arrivalDistribution rand scoopDistribution remainingChairs |
[
	new
		profit <- 0.
		remainingChairs <- 15.
		rand <- Random new.
		(arrivalDistribution <- Normal new)
			setMean: 3.0 deviation: 1.0.
		(scoopDistribution <- DiscreteProbability new)
			defineWeights: #(65 25 10).
		self scheduleArrival
|
	scheduleArrival			| newcustomer  time |
		newcustomer <- Customer new.
		time <- self time + (arrivalDistribution next).
		(time < 15) ifTrue: [
			self addEvent: [self customerArrival: newcustomer]
				at: time ]
|
	processEvent: event
		('event received at ', self time printString) print.
		event value.
		self scheduleArrival
|
	customerArrival: customer	| size |
		size <- customer groupSize.
		('group of size ', size printString , ' arrives') print.
		(size < remainingChairs)
			ifTrue: [remainingChairs <- remainingChairs - size.
				 'take chairs, schedule order' print.
				 self addEvent: 
					[self customerOrder: customer]
					next: (rand randInteger: 3).
				]
			ifFalse: ['finds no chairs, leave' print]
|
	customerOrder: customer		| size numScoops |
		size <- customer groupSize.
		numScoops <- 0.
		size timesRepeat: 
			[numScoops <- numScoops + scoopDistribution next].
		('group of size ', size printString, ' orders ' ,
		numScoops printString, ' scoops') print.
		profit <- profit + (numScoops * 0.17).
		self addEvent:
			[self customerLeave: customer]
			next: (rand randInteger: 5)
|
	customerLeave: customer		| size |
		size <- customer groupSize.
		('group of size ', size printString, ' leaves') print.
		remainingChairs <- remainingChairs + customer groupSize
|
	reportProfits
		('profits are ', profit printString) print
]

Class Customer
| groupSize |
[
	new
		groupSize <- (Random new "randomize") randInteger: 8
|
	groupSize
		^ groupSize
]

Class DiscreteProbability
| weights rand max |
[
	defineWeights: anArray
		weights <- anArray.
		(rand <- Random new) "randomize".
		max <- anArray inject: 0 into: [:x :y | x + y]
|
	next	| index value |
		value <- rand randInteger: max.
		index <- 1.
		[value > (weights at: index)]
			whileTrue: [value <- value - (weights at: index).
					index <- index + 1].
		^ index
]

Class Normal :Random
| mean deviation |
[
	new
		self setMean: 1.0 deviation: 0.5
|
	setMean: m deviation: s
		mean <- m.
		deviation <- s
|
	next		| v1 v2 s u |
		s <- 1.
		[s >= 1] whileTrue:
			[v1 <- (2 * super next) - 1.
			 v2 <- (2 * super next) - 1.
			  s <- v1 squared + v2 squared ].
		u <- (-2.0 * s ln / s) sqrt.
		^ mean + (deviation * v1 * u)
]
SHAR_EOF
if test 3541 -ne "`wc -c < 'tests/sim3.st'`"
then
	echo shar: error transmitting "'tests/sim3.st'" '(should have been 3541 characters)'
fi
fi # end of overwriting check
if test -f 'tests/super.out'
then
	echo shar: will not over-write existing file "'tests/super.out'"
else
cat << \SHAR_EOF > 'tests/super.out'
Little Smalltalk
	1
1
2
2
2
4
2
4
2
2
Main
	

SHAR_EOF
if test 46 -ne "`wc -c < 'tests/super.out'`"
then
	echo shar: error transmitting "'tests/super.out'" '(should have been 46 characters)'
fi
fi # end of overwriting check
if test -f 'tests/super.st'
then
	echo shar: will not over-write existing file "'tests/super.st'"
else
cat << \SHAR_EOF > 'tests/super.st'
Class One
[
        test
                ^ 1
|       result1
                ^ self test
]

Class Two :One
[
        test
                ^ 2
]

Class Three :Two
[
        result2
                ^ self result1
|       result3
                ^ super test
]

Class Four :Three
[
        test
                ^ 4
]

Class Main
| example1 example2 example3 example4 |
[
        main
                example1 <- One new.
                example2 <- Two new.
                example3 <- Three new.
                example4 <- Four new.
                example1 test print.
                example1 result1 print.
                example2 test print.
                example2 result1 print.
                example3 test print.
                example4 result1 print.
                example3 result2 print.
                example4 result2 print.
                example3 result3 print.
                example4 result3 print
]
SHAR_EOF
if test 924 -ne "`wc -c < 'tests/super.st'`"
then
	echo shar: error transmitting "'tests/super.st'" '(should have been 924 characters)'
fi
fi # end of overwriting check
if test -f 'tests/temp.st'
then
	echo shar: will not over-write existing file "'tests/temp.st'"
else
cat << \SHAR_EOF > 'tests/temp.st'
Class Main
[
	main		| i |

		i <- 1.
		[i < 3] whileTrue: [i print. i <- i + 1]
]

SHAR_EOF
if test 83 -ne "`wc -c < 'tests/temp.st'`"
then
	echo shar: error transmitting "'tests/temp.st'" '(should have been 83 characters)'
fi
fi # end of overwriting check
if test -f 'tests/turing.st'
then
	echo shar: will not over-write existing file "'tests/turing.st'"
else
cat << \SHAR_EOF > 'tests/turing.st'
"
	Turing machine simulator contributed by Jan Gray,
		the University of Waterloo
"
Class Main
[
	main			| tm |
		tm <- TuringMachine new initialize.
		tm delta state: 0 input: $# nextState: 1 output: $L.
		tm delta state: 1 input: $I nextState: 1 output: $i.
		tm delta state: 1 input: $i nextState: 1 output: $L.
		tm delta state: 1 input: $# nextState: 2 output: $R.
		tm delta state: 2 input: $i nextState: 2 output: $R.
		tm delta state: 2 input: $# nextState: 'halt' output: $#.
		tm tape: 'IIIIII'.
		tm delta print.
		tm run
]
Class TuringMachine
|       tape            "Infinite tape"
        state           "Current state, TM continues if state is a number"
        delta           "A TransitionTable, which for each (state, input)
                         gives (next state, output)"
        tapeMoves       "A Dictionary which maps L and R into [tape left]
                         and [tape right]"
|
[
        initialize
                tapeMoves <- Dictionary new.
                tapeMoves at: $L put: [tape left].
                tapeMoves at: $R put: [tape right].
                delta <- TransitionTable new.
                self tape: ''.
                self state: 0
|
        tape: aString
                tape <- Tape new with: aString
|
        state: aState
                state <- aState
|
        delta
                ^ delta
|
        step
                | next |
                next <- delta atState: state input: tape read.
                state <- next state.
                (state isKindOf: Number)
                        ifTrue: [(tapeMoves includesKey: next symbol)
                                        ifTrue:  [(tapeMoves at: next symbol) value]
                                        ifFalse: [tape write: next symbol]]
|
        run
                state <- 0.
                self print.
                [state isKindOf: Number] whileTrue: [self step print]
|
        printString
                ^ 'State ', state printString, ', Tape ', tape printString
]
Class Pair	:Magnitude
| state symbol |
[
        state: aState symbol: aSymbol
                state <- aState.
                symbol <- aSymbol
|
        state
                ^ state
|
        symbol
                ^ symbol
|
	< aPair
		^ state < aPair state or:
			[state = aPair state and: [symbol < aPair symbol]]
|
        printString
                ^ state printString, '	', symbol printString
]
Class TransitionTable :Dictionary
[
        state: aState input: in nextState: nextState output: out
                self at: (Pair new state: aState symbol: in)
                     put: (Pair new state: nextState symbol: out).
		^ nil
|
        atState: aState input: in
                ^ self at: (Pair new state: aState symbol: in)
                       ifAbsent: [^ Pair new state: 'hung' symbol: nil].
|
        print
                'State	Read	Next	Write' print.
		self binaryDo: [:x :y |
			(x printString , '	', y printString) print]
]
Class Tape :Object
| tape position |
[
        with: aString
                tape <- '#', aString, '#'.
                position <- tape size
|
        read
                ^ tape at: position
|
        write: aChar
                tape at: position put: aChar.
|
        left
                (position > 1)
                        ifTrue: [position <- position - 1]
|
        right
                (position = tape size)
                        ifTrue: [tape <- tape, '#'].
                position <- position + 1
|
        printString
                ^ (tape copyFrom: 1 to: position - 1), '{',
                  ((tape at: position) asString), '}',
                  (tape copyFrom: position + 1 to: tape size)
]
SHAR_EOF
if test 3680 -ne "`wc -c < 'tests/turing.st'`"
then
	echo shar: error transmitting "'tests/turing.st'" '(should have been 3680 characters)'
fi
fi # end of overwriting check
if test -f 'tests/visitor.st'
then
	echo shar: will not over-write existing file "'tests/visitor.st'"
else
cat << \SHAR_EOF > 'tests/visitor.st'
Class SimulationObject :Object	
	| sizeDist waitDist |
[
	init
		sizeDist <- Binomial new initialize events: 5 mean: 0.4.
		waitDist <- Random new	"uniform distribution"

|	size
		^ sizeDist next

|	wait: sizeGroup	  "uniform distribution from 1 to 6"
		^ waitDist next * sizeGroup * 6
]

Class Visitor	:SimulationObject
	| sizeGroup wait alreadyEaten |
[
	initialize: superClass
		sizeGroup <- superClass size.
		wait <- superClass wait: sizeGroup.
		alreadyEaten <- false

|	entering
		(alreadyEaten == false)
		     ifTrue: [alreadyEaten <- true. ^ true].
		^ false

|	time	
		^ wait

|	groupSize
		^ sizeGroup

]
SHAR_EOF
if test 617 -ne "`wc -c < 'tests/visitor.st'`"
then
	echo shar: error transmitting "'tests/visitor.st'" '(should have been 617 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0