Reia PEG Issue 5 : Precedence of arithmetic and shift operators

7 views
Skip to first unread message

graeme defty

unread,
Aug 22, 2010, 2:20:13 AM8/22/10
to Reia
This is one of a series of questions which have arisen from work in
developing a PEG for Reia:

OK - Last (for now ;-) ) though by no means least (in fact i expect
this one to generate the most discussion)

-- Symbol Involved:
Arithmetic, shift and logical (* / % + - << >> | ^ &)

-- Issue:
These in Reia are in two groups, as follows
* / % &
+ - << >> | ^

This is somewhat in line with erlang, which also has only two groups:
/ * div rem band and
+ - bor bxor bsl bsr or xor )

but wildly at odds with Ruby which has far more granular levels of
precedence:
* / %
+ - (binary)
<< >>
&
| ^

IMHO Ruby has it right.

-- Rationale:
This is going to be a tough one because whatever I come up with just
sounds to me like "cos it should be", but here goes...

Along the same lines that we perform the multiplicative before the
additive (and these are closely related) we should be performing the
arithmetic before (or after) the logical - they are not nearly as
closely related, it seems to me, as the arithmetic are to each other.

I usually find it helps to think of and example, so take

1 + 3 << 3 - 2

I just find it hard to think that the right thing to do here is start
at the left and work along the row applying each time the next operand
with the next operator to the partial result so far. It's just plain
WRONG! (Told you it was a weak argument.) To me it seems logical that
tis expression should represent either

(1 + 3) << (3 - 2)

OR

1 + (3 << 3) - 2

It is not clear to me which of these is preferable, but *either* is
better than

((1 + 3) << 3) - 2


Now as to the question of which is better, I guess the Ruby list gives
me
- produce some values (with the normal arithmetic operations and their
precedences),
- use them to shift each other around, and then
- bitwise and, or and xor the results together

Well no strong motivation for this sequence over any other, but it
does sound like the kind of thing you might want to do. (ha ha ha -
weak or what?)

At the very least, the & should separated from ^ and | by the additive
operators. I find it very illogical to perform & before addition, but
^ and | have to wait until after.

Well, anyway, I hope someone can come up with some better arguments in
this area and I am not being totally emotional in my choice, but there
it is.

-- My Suggestion:
Adopt the Ruby precedences

graeme defty

unread,
Aug 22, 2010, 8:42:34 AM8/22/10
to Reia
Sorry - the peculiar sentence near the end should have read

"At the very least, the & should not be separated from ^ and | by the
additive
operators."

g
__________________________

Kim Shrier

unread,
Aug 22, 2010, 4:56:31 PM8/22/10
to re...@googlegroups.com
An argument for using the Ruby precedence levels is that Reia has a
Ruby-like syntax (much more than an Erlang-like syntax) Since the
language looks Ruby-ish it would probably not trip up newcomers to
the language if the precedence was the same as Ruby.

However, in practice, I have found it a bad idea to rely on the
precedence rules and I always use parenthesis to disambiguate any
arithmetic/logical expression evaluation. I program in too many
different languages to ever keep all the precedence rules straight.

To sum up my 2 cents, I don't think it really matters to me what
is settled on but in this case it would probably be a good idea to
follow Ruby's lead since one of the points of Reia is to make it
easier for people to use the power of the Erlang VM by providing a
Ruby-like language built on top of the Erlang VM.

Kim

> --
> You received this message because you are subscribed to the Google
> Groups "Reia" group.
> To post to this group, send email to re...@googlegroups.com.
> To unsubscribe from this group, send email to reia+uns...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/reia?hl=en
> .
>

Tony Arcieri

unread,
Aug 24, 2010, 2:07:42 PM8/24/10
to re...@googlegroups.com
Yeah I'll definitely correct these to use the Ruby precedence
--
Tony Arcieri
Medioh! A Kudelski Brand
Reply all
Reply to author
Forward
0 new messages