subexpression precedence

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

subexpression precedence

James Ladd
Hi Pharo People,

I know Smalltalk has precedence rules when subexpressions () are involved.

2 + (  (3 * 4) - 1  )
evaluated last (   (evaluated first)  evaluated second  )

That is the inner most subexpression is evaluated first.

Given the following made up expression for the purposes of my question:

self at: (self offset) - 1 put: (2 * (12 / 4)).

Are there precedence rules for the subexpressions that are part of the keyword expression?
ie: Should (self offset) - 1 be evaluated before (2 * (12 / 4)).

My thinking is that with keywords the arguments are evaluated left to right - that is
at: before put: but within each the regular subexpression precedence applies?

- James.


Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Jan Vrany
Hi, I'm afraid this is not specified. There's no
language specification (*) as such of Pharo or any 
other available smalltalk - at least not that I know 
(prove me if I'm wrong).

Order in which subexpressions are evaluated is rarely specified.
IIRC, JVM Spec doesn't specify it either.

If the question was rather "How does it work in current Pharo 
implementation?", then it's easy to check, just add some sideffects 
to subexpressions (like Transcript show) and see in what order 
they're executed :-)

(*) Specification in a sense of Java Language Spec, 
    C# Language Specification, C99 Spec and so on.

Jan

On Wed, 2017-01-11 at 16:16 -0800, James Ladd wrote:

> Hi Pharo People,
>
> I know Smalltalk has precedence rules when subexpressions () are
> involved.
>
> 2 + (  (3 * 4) - 1  )
> evaluated last (   (evaluated first)  evaluated second  )
>
> That is the inner most subexpression is evaluated first.
>
> Given the following made up expression for the purposes of my
> question:
>
> self at: (self offset) - 1 put: (2 * (12 / 4)).
>
> Are there precedence rules for the subexpressions that are part of
> the
> keyword expression?
> ie: Should (self offset) - 1 be evaluated before (2 * (12 / 4)).
>
> My thinking is that with keywords the arguments are evaluated left to
> right
> - that is 
> at: before put: but within each the regular subexpression precedence
> applies?
>
> - James.
>
>
>
>
>
>
> --
> View this message in context: http://forum.world.st/subexpression-pre
> cedence-tp4929398.html
> Sent from the Pharo Smalltalk Developers mailing list archive at
> Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Eliot Miranda-2
Hi Jan,

> On Jan 11, 2017, at 4:54 PM, Jan Vrany <[hidden email]> wrote:
>
> Hi, I'm afraid this is not specified. There's no
> language specification (*) as such of Pharo or any
> other available smalltalk - at least not that I know
> (prove me if I'm wrong).

Wow, I just skimmed the messages section in the blue book and you're right.  I think this is an omission and that it should be specified that keyword message receiver and arguments are evaluated strictly left-to-right.  I don't know of a Smalltalk implementation that doesn't evaluate in this order.

But thanks for the OP's question and for your response.  I am surprised.

>
> Order in which subexpressions are evaluated is rarely specified.
> IIRC, JVM Spec doesn't specify it either.
>
> If the question was rather "How does it work in current Pharo
> implementation?", then it's easy to check, just add some sideffects
> to subexpressions (like Transcript show) and see in what order
> they're executed :-)
>
> (*) Specification in a sense of Java Language Spec,
>    C# Language Specification, C99 Spec and so on.
>
> Jan
>
>> On Wed, 2017-01-11 at 16:16 -0800, James Ladd wrote:
>> Hi Pharo People,
>>
>> I know Smalltalk has precedence rules when subexpressions () are
>> involved.
>>
>> 2 + (  (3 * 4) - 1  )
>> evaluated last (   (evaluated first)  evaluated second  )
>>
>> That is the inner most subexpression is evaluated first.
>>
>> Given the following made up expression for the purposes of my
>> question:
>>
>> self at: (self offset) - 1 put: (2 * (12 / 4)).
>>
>> Are there precedence rules for the subexpressions that are part of
>> the
>> keyword expression?
>> ie: Should (self offset) - 1 be evaluated before (2 * (12 / 4)).
>>
>> My thinking is that with keywords the arguments are evaluated left to
>> right
>> - that is
>> at: before put: but within each the regular subexpression precedence
>> applies?
>>
>> - James.
>>
>>
>>
>>
>>
>>
>> --
>> View this message in context: http://forum.world.st/subexpression-pre
>> cedence-tp4929398.html
>> Sent from the Pharo Smalltalk Developers mailing list archive at
>> Nabble.com.


_,,,^..^,,,_ (phone)
Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Chris Muller-3
> Wow, I just skimmed the messages section in the blue book and you're right.  I think this is an omission and that it should be specified that keyword message receiver and arguments are evaluated strictly left-to-right.  I don't know of a Smalltalk implementation that doesn't evaluate in this order.

This doesn't sound right.  I don't know whether I can dig out a
reference quickly or not, but I learned that precedence *within* the [
unary -> binary -> keyword ] is actually right to left, not left to
right.

  a := b := c := d := 4

executes right-to-left.  Other than humans generally read from
left-to-right, are there other reasons it should run left-to-right in
other contexts?

Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Chris Muller-3
> Other than humans generally read from

(pardon me)

Other than that the code is read from left to right...

Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

James Ladd
In reply to this post by Chris Muller-3
>>  a := b := c := d := 4

This one is straight forward but subexpressions, those with () around them are the topic.



On Thu, Jan 12, 2017 at 2:54 PM, Chris Muller <[hidden email]> wrote:
> Wow, I just skimmed the messages section in the blue book and you're right.  I think this is an omission and that it should be specified that keyword message receiver and arguments are evaluated strictly left-to-right.  I don't know of a Smalltalk implementation that doesn't evaluate in this order.

This doesn't sound right.  I don't know whether I can dig out a
reference quickly or not, but I learned that precedence *within* the [
unary -> binary -> keyword ] is actually right to left, not left to
right.

  a := b := c := d := 4

executes right-to-left.  Other than humans generally read from
left-to-right, are there other reasons it should run left-to-right in
other contexts?


Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

James Ladd
In reply to this post by Chris Muller-3
I think for Redline Smalltalk which is why I'm asking I'll make sure
my test subexpressions yield the same result as the same subexpressions on Pharo :)

On Thu, Jan 12, 2017 at 2:58 PM, Chris Muller <[hidden email]> wrote:
> Other than humans generally read from

(pardon me)

Other than that the code is read from left to right...


Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Martin McClure-2
In reply to this post by Jan Vrany
On 01/11/2017 04:54 PM, Jan Vrany wrote:
> Hi, I'm afraid this is not specified. There's no
> language specification (*) as such of Pharo or any
> other available smalltalk - at least not that I know
> (prove me if I'm wrong).

The ANSI Smalltalk spec says

"The individual statements are executed in left to right sequence," but
in context I believe that this just means that you execute the first
statement in the method, then the next, and so on.

Ah, here it is. Section 3.4.5.3 says

"The receiver and the arguments are evaluated before the message is
sent. They are evaluated in a left-to-right order."
So that's pretty clear.

Regards,

-Martin


Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Eliot Miranda-2


> On Jan 11, 2017, at 9:38 PM, Martin McClure <[hidden email]> wrote:
>
>> On 01/11/2017 04:54 PM, Jan Vrany wrote:
>> Hi, I'm afraid this is not specified. There's no
>> language specification (*) as such of Pharo or any
>> other available smalltalk - at least not that I know
>> (prove me if I'm wrong).
>
> The ANSI Smalltalk spec says
>
> "The individual statements are executed in left to right sequence," but in context I believe that this just means that you execute the first statement in the method, then the next, and so on.
>
> Ah, here it is. Section 3.4.5.3 says
>
> "The receiver and the arguments are evaluated before the message is sent. They are evaluated in a left-to-right order."
> So that's pretty clear.

Thanks, Martin.  I'm relieved :-).  "a" seems superfluous.  Hopefully there is only one left-to-right order ;-)

>
> Regards,
>
> -Martin
>
>

Reply | Threaded
Open this post in threaded view
|

Re: subexpression precedence

Jan Vrany
In reply to this post by Martin McClure-2
On Wed, 2017-01-11 at 21:38 -0800, Martin McClure wrote:

> On 01/11/2017 04:54 PM, Jan Vrany wrote:
> > Hi, I'm afraid this is not specified. There's no
> > language specification (*) as such of Pharo or any
> > other available smalltalk - at least not that I know
> > (prove me if I'm wrong).
>
> The ANSI Smalltalk spec says
>
> "The individual statements are executed in left to right sequence,"
> but 
> in context I believe that this just means that you execute the first 
> statement in the method, then the next, and so on.
>
> Ah, here it is. Section 3.4.5.3 says
>
> "The receiver and the arguments are evaluated before the message is 
> sent. They are evaluated in a left-to-right order."
> So that's pretty clear.

Oops, I overlooked this. Thanks!

Jan
>
> Regards,
>
> -Martin
>
>