playing with syntax alternatives

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

playing with syntax alternatives

Francisco Garau-2
Hello -- I want to do some experimentation with alternative syntax for Smalltalk. My main motivation is trying to unify the block and method syntax. I see a block as an in-lined method with an empty selector. This syntax helps to visualize that an object is basically a closure over its instance variables.

To make this work, I would also need to make Blocks understand the message #: (which would be equivalent to the current #value:).

Some examples will be more clear than I can describe.

Currently:

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

addTo := [:a :b | a + b].
(addTo value: 3 value: 4) = 7.

Proposal:

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

[add: a to: b | a + b].
(add :3 to : 4) = 7. 

Class declaration syntax:

[Point << Object |
| x y |

[hash | ^x hash + y hash].

[extent: aPoint | ^Rectangle origin: self extent: aPoint].

[distance: aPoint |
| dx dy |
dx := aPoint x - x.
dy := aPoint y - y.
^((dx * dx) + (dy * dy)) sqrt]. 
].

I am reaching out for ideas or direction on how to do such experiments. I would rather avoid touching the current  compiler. Maybe defining this alternative syntax that compiles to regular Smalltalk. Nowadays PEG parsers are very trendy. Is there one available for any of the Squeak/Smalltalk flavours?

Thanks,
Francisco












Reply | Threaded
Open this post in threaded view
|

Re: playing with syntax alternatives

marcel.taeumel
Hi Francisco,

have you checked out Ohm/S yet?

Best,
Marcel

Am 17.04.2021 11:48:19 schrieb Francisco Garau <[hidden email]>:

Hello -- I want to do some experimentation with alternative syntax for Smalltalk. My main motivation is trying to unify the block and method syntax. I see a block as an in-lined method with an empty selector. This syntax helps to visualize that an object is basically a closure over its instance variables.

To make this work, I would also need to make Blocks understand the message #: (which would be equivalent to the current #value:).

Some examples will be more clear than I can describe.

Currently:

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

addTo := [:a :b | a + b].
(addTo value: 3 value: 4) = 7.

Proposal:

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

[add: a to: b | a + b].
(add :3 to : 4) = 7. 

Class declaration syntax:

[Point << Object |
| x y |

[hash | ^x hash + y hash].

[extent: aPoint | ^Rectangle origin: self extent: aPoint].

[distance: aPoint |
| dx dy |
dx := aPoint x - x.
dy := aPoint y - y.
^((dx * dx) + (dy * dy)) sqrt]. 
].

I am reaching out for ideas or direction on how to do such experiments. I would rather avoid touching the current  compiler. Maybe defining this alternative syntax that compiles to regular Smalltalk. Nowadays PEG parsers are very trendy. Is there one available for any of the Squeak/Smalltalk flavours?

Thanks,
Francisco