[ENH] Empty return expression

Bert Freudenberg bert at isgnw.CS.Uni-Magdeburg.De
Mon Sep 27 08:35:44 UTC 1999


Parser patch to allow empty returns that indicate 'nothing returned' like 
	foo ifTrue: [self doSomething. ^]
instead of the currently often (mis-)used
	foo ifTrue: [^self doSomething]
as short form of
	foo ifTrue: [self doSomething. ^self]

Discussion: 
This syntax matches the implicit return of self at the end of
a method. It makes the "void" return type of a method explicit, which is
good for self-documenting code. It's backwards compatible. It's a minor
change in the parser. So why not?

It's not compatible to other Smalltalks - but then, the brace constructs
are even more different.

  /bert

Content-Type: TEXT/PLAIN; charset=US-ASCII; name="return-bf.24Sep148pm.cs"
Content-ID: <Pine.LNX.3.96.990927103544.31728U at balloon.cs.uni-magdeburg.de>
Content-Description: 

'From Squeak 2.5 of August 6, 1999 on 24 September 1999 at 1:48:35 pm'!
"Change Set:		return-bf
Date:			24 September 1999
Author:			Bert Freudenberg

Parser patch to allow empty returns that indicate 'nothing returned' like 
	foo ifTrue: [self doSomething. ^]
instead of the currently often (mis-)used
	foo ifTrue: [^self doSomething]
or as short form of
	foo ifTrue: [self doSomething. ^self]"!


!ParseNode class methodsFor: 'accessing' stamp: 'bf 9/24/1999 11:57'!
nodeSelf
	^NodeSelf! !


!Parser methodsFor: 'expression types' stamp: 'bf 9/24/1999 11:58'!
statements: argNodes innerBlock: inner

	| stmts returns start more blockComment |
	stmts _ OrderedCollection new.
	"give initial comment to block, since others trail statements"
	blockComment _ currentComment.
	currentComment _ nil.
	returns _ false.
	more _ hereType ~~ #rightBracket.
	[more]
		whileTrue: 
		[start _ self startOfNextToken.
		(returns _ self match: #upArrow)
			ifTrue: 
				[self expression
					ifFalse: [parseNode _ ParseNode nodeSelf].
				self addComment.
				stmts addLast: (parseNode isReturningIf
					ifTrue: [parseNode]
					ifFalse: [ReturnNode new
							expr: parseNode
							encoder: encoder
							sourceRange: (start to: self endOfLastToken)])]
			ifFalse: 
				[self expression
					ifTrue: 
						[self addComment.
						stmts addLast: parseNode]
					ifFalse: 
						[self addComment.
						stmts size = 0
							ifTrue: 
								[stmts addLast: 
									(encoder encodeVariable:
										(inner ifTrue: ['nil'] ifFalse: ['self']))]]].
		returns 
			ifTrue: 
				[self match: #period.
				(hereType == #rightBracket or: [hereType == #doIt])
					ifFalse: [^self expected: 'End of block']].
		more _ returns not and: [self match: #period]].
	parseNode _ BlockNode new
				arguments: argNodes
				statements: stmts
				returns: returns
				from: encoder.
	parseNode comment: blockComment.
	^ true! !





More information about the Squeak-dev mailing list