[Cuis-dev] extending the syntax for blocks and methods

Jaromir Matas mail at jaromir.net
Sat Jul 16 12:16:42 PDT 2022


Hola ;)
Interesting... a few quick observations:
1) from the Smalltalk point of view anything between two square brackets is a block and would not evaluate until sent #value; however this
[succ: n | n + 1]
would be a syntactic sugar for
succ := [:n | n + 1]
which means there would be "some" evaluation (assignment) even without sending #value (because it's a syntactic sugar). Just saying it may be a bit confusing or inconsistent…
2) I understand Smalltalk is (or at least used to be) an LL(1) language; correct me if I'm wrong but your suggestion would make the grammar LL(2) or more because '[' followed by an identifier still doesn’t distinguish between possible branches and you have to look further ahead…
3) how would you parse [ x | y ] ... as a block with an "or" clause or an extension notation for x := [ y ] ?
Best,
Jaromir


--

Jaromír Matas

mail at jaromir.net

From: Francisco Garau via Cuis-dev<mailto:cuis-dev at lists.cuis.st>
Sent: Friday, July 15, 2022 20:39
To: cuis-dev at lists.cuis.st<mailto:cuis-dev at lists.cuis.st>
Cc: Francisco Garau<mailto:francisco.garau at gmail.com>
Subject: [Cuis-dev] extending the syntax for blocks and methods

Hola Amigos -- just wanted to share with you some ideas I've been thinking about for a while.

It all started when someone mentioned the colon in the syntax for block arguments could have whitespace around the argument so that you could write  [ : n | n + 1]

It makes perfect sense to see the block as an "inlined" method that doesn't require any method-lookup; therefore, the selector is superfluous. But if we accepted the selector inside the block, it could be used as the variable holding the block. So, instead of writing succ := [:n | n + 1]  we could write  [succ: n | n + 1]

Even better if the block evaluation accepts #: as a valid selector.  So you could write (succ: 3) = 4 instead of (succ value: 3) = 4.

A short example will make this clear.

Vanilla Smalltalk
succ := [:n | n + 1].
(succ value: 3) = 4.

Extended Block Syntax
[succ: n | n + 1].
(succ: 3 ) = 4.
"the above is equivalent to"
(succ perform: #: with: 3) = 4.

Being able to parse blocks with these syntax paves the way to unify the syntax for method definition—for example, a few hypothetical methods from the Point class.

"Point class>x:y:"
[x: xInteger y: yInteger |
   ^self basicNew setX: xInteger setY: yInteger].

"Point>>abs"
[abs |
   ^x abs @ y abs].

"Point>>extent:"
[extent: aPoint |
   ^Rectangle origin: self extent: aPoint]

Of course, it gets trickier when blocks have more than one argument..

[add: a to: b | a + b].
(add_to value: 3 value: 4) = 7. "old style block evaluation"
(add: 3 to: 4) = 7.

I think building a separate Parser is a sensible approach -- is there a Smalltalk Parser as a separate package that can be tweaked without affecting the running image?

Thanks,
Francisco



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20220716/b6a44593/attachment.htm>


More information about the Cuis-dev mailing list