["BUG"][FIX?]Incorrect Pseudo-Codefor#whileTrue:/#whileFalse:?

Bob Arning arning at charm.net
Thu Jan 4 14:33:02 UTC 2001


On Wed, 03 Jan 2001 23:46:36 -0600 Mark van Gulik <ghoul6 at home.net> wrote:
>Trick: If you're tired of relying on Compiler tricks in Smalltalk to
>implement space-efficient iteration, just do this:
>
>    ...
>    here := thisContext pc.
>    ...
>    thisContext pc: here.
>    ...
>
>It probably won't work, but it's more accurate pseudocode than the recursive
>version in whileTrue:.

Mark,

With a slight tweak, it does!

Cheers,
Bob
-----code follows-----
'From Squeak2.9alpha of 17 July 2000 [latest update: #3219] on 4 January 2001 at 9:30:20 am'!
"Change Set:		blockJump
Date:			4 January 2001
Author:			Bob Arning

Experimental implementation of #whileTrue2: (aka #whileTrue:) which uses Mark van Gulik's GOTO trick. The result is an implementation that does not require compiler optimization and (hopefully) will cause less confusion."!


!ContextPart methodsFor: 'instruction decoding' stamp: 'RAA 1/4/2001 09:30'!
jumpTo: newPC 

	pc _ newPC! !


!BlockContext methodsFor: 'controlling' stamp: 'RAA 1/4/2001 09:28'!
whileTrue2: aBlock 
	| loopAgain |
"
| x |
x _ 0.
[(x _ x + 1) < 10] whileTrue2: [Transcript show: x printString; cr].
"
	loopAgain _ thisContext pc.
	self value ifFalse: [^self].
	aBlock value.
	thisContext jumpTo: loopAgain.
! !





More information about the Squeak-dev mailing list