[squeak-dev] Converting underscore assignment to ANSI?

Levente Uzonyi leves at elte.hu
Fri May 13 23:07:56 UTC 2011


On Fri, 13 May 2011, Casey Ransberger wrote:

> Hello everyone,
>
> I have some code I wrote for Cuis, where we prefer underscore (left arrow)
> assignment, which I've had occasion to start porting to Squeak. Without the
> left-arrow glyph, the underscores look really awful, and I'd like to be able
> to swap them for ANSI assignment if I release the code for platforms other
> than Cuis. I've googled a bit and searched the projects on SqueakSource, but
> I'm not finding FixUnderscores.
>
> Also, I was wondering if FixUnderscores had any gotchas I might want to
> worry about.

As I wrote it recently, FixUnderscores is crap. FixUnderscores2 is a lot 
better (properly fixes underscores, doesn't modify your fonts, etc).
But it's pretty easy to write such code in Squeak without subclassing 
Scanner, so I hacked together the following (evaluate it in a workspace):

fixUnderScoresInSourceString := [ :methodString |
 	String streamContents: [ :stream |
 		| previousUnderscoreIndex |
 		previousUnderscoreIndex := 0.
 		Scanner new
 			scanTokenPositionsIn: methodString
 			into: [ :startPosition :endPosition |
 				(startPosition = endPosition and: [
 					(methodString at: startPosition) == $_ ]) ifTrue: [
 						stream
 							next: startPosition - previousUnderscoreIndex - 1
 							putAll: methodString
 							startingAt: previousUnderscoreIndex + 1;
 							nextPutAll: ':='.
 						previousUnderscoreIndex := startPosition ] ].
 		stream
 			next: methodString size - previousUnderscoreIndex
 			putAll: methodString
 			startingAt: previousUnderscoreIndex + 1 ] ].

fixUnderScoresInMethod := [ :compiledMethod |
 	| source fixedSource |
 	source := compiledMethod getSource asString.
 	fixedSource := fixUnderScoresInSourceString value: source.
 	source = fixedSource
 		ifTrue: [ false ]
 		ifFalse: [
 			compiledMethod methodClass
 				compile: fixedSource
 				classified: (compiledMethod methodClass organization categoryOfElement: compiledMethod selector)
 				withStamp: compiledMethod timeStamp
 				notifying: nil.
 			true ] ].

testMethodString := 'testMethod
 	"This_is_a_comment_with_underscores."

 	| x |
 	x _ $_.
 	^x = ''_'' first and: [ x = #''_'' first and: [ x = #(_) first first ] ]'.

underscoresTest := Object subclass: #UnderscoresTest
 	instanceVariableNames: ''
 	classVariableNames: ''
 	poolDictionaries: ''
 	category: 'UnderscoresTest'.

underscoresTest compile: testMethodString classified: 'instance side method'.
underscoresTest class compile: testMethodString classified: 'class side method'.
Object compile: testMethodString classified: '*UnderscoresTest'.

CurrentReadOnlySourceFiles cacheDuring: [
 	((PackageInfo named: 'UnderscoresTest') methods select: [ :each |
 		fixUnderScoresInMethod value: each compiledMethod ]) explore ]
"add your assertions here :)"


Levente

>
> Thanks in advance for any pointers you can give!
>
> -- 
> Casey Ransberger
>



More information about the Squeak-dev mailing list