The Artima Developer Community
Sponsored Link

Weblogs Forum
Heron Operator Precedence

5 replies on 1 page. Most recent reply: Sep 8, 2005 10:05 AM by Todd Blanchard

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Heron Operator Precedence (View in Weblogs)
Posted: Sep 6, 2005 6:51 AM
Reply to this message Reply
Summary
Operator precedence in Heron is tricky, because any sequence of legal operator characters is a legal operator.
Advertisement
At the present moment I leaning towards setting operator precedence in Heron :
  1. unary expressions (prefix operations)
  2. multiplicative * / %
  3. additive + -
  4. other
  5. assignment =
  6. comma ,

Here is the problem I am facing: In Heron there are no predefined operators. Any sequence of operator characters is in fact a legal operator. All operators map to a specifically named member function of the left hand value.

An example of a problem with just following C++ operator precedence rules is that an overloaded pipe operator (|) might actually mean to create a pipe and not bitwise or. Even > could have multiple meanings like redirect output to a file or greater than. Giving it a specific precedence based on usage of bitwise or is not a good idea. What about entirely new operators, like &&= which means assignment boolean and. It should be at the same level as the other assignment operators. Anyway the list of complications goes on and on. I think beyond the simple arithmetic precedence rules, it is best if I rely on disamibguation rules.

Maybe there is another way that everyone can be happy: I wonder if there there is an elegant way to implement precedence directly within a language? Maybe there would be an expression reordering step, that is controllable from code. Perhaps something inspired by expression template machinery? Perhaps operators could map to types or function pointers? I am open to new ideas here.


Vesa Karvonen

Posts: 116
Nickname: vkarvone
Registered: Jun, 2004

Re: Heron Operator Precedence Posted: Sep 6, 2005 7:09 AM
Reply to this message Reply
Are you familiar with the infix directive of languages including Standard ML, Alice ML and Haskell?

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Heron Operator Precedence Posted: Sep 6, 2005 7:20 AM
Reply to this message Reply
> Are you familiar with the infix directive of
> languages including Standard ML, Alice ML and Haskell?

No I'm not, thanks for pointing it out, I will look into it. Of course I would appreciate any information you cared to share.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Heron Operator Precedence Posted: Sep 6, 2005 9:12 AM
Reply to this message Reply
From http://www.dcs.ed.ac.uk/home/pgh/grammar.html

The precedence of an infix or split operator says how it "behaves" with respect to other infix or split operators. We seem to need at least two "layers", each having at least three precedence "strata" -- additive, multiplicative and exponential. Think of the usual syntax for boolean combinations of arithmetic equations. Here we have +,*,^ for expressions, then equality lower than all these, then +,*,^ for propositions. It seems reasonable to allow for 10 levels, although this is certainly rather `ad hoc'. This happens to be as in Haskell.

This is not particularly elegant. I would be happier telling programmers to use parantheses to disambiguate precedence.

Vesa Karvonen

Posts: 116
Nickname: vkarvone
Registered: Jun, 2004

Re: Heron Operator Precedence Posted: Sep 6, 2005 10:38 AM
Reply to this message Reply
I'm not exactly sure about what you are referring to. The article you are pointing at is not a particularly clear explanation of how Haskell handles infix operators. Here are the relevant sections from the Haskell specification: http://www.haskell.org/onlinereport/decls.html#sect4.4.2 , http://www.haskell.org/onlinereport/exps.html#sect3.2 . You can see an example of the use of fixity declaration in the Standard Prelude http://www.haskell.org/onlinereport/standard-prelude.html .

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

Why have it? Posted: Sep 8, 2005 10:05 AM
Reply to this message Reply
Simply associate operators left to right and let people use parens if they want to fiddle order of evaluation.

This is what Smalltalk does - "operators" are really just object messages. Messages don't have precendence.

3 + 5

sends message + with argument 5 to 3.

I assume you'll be doing something similar.

Flat View: This topic has 5 replies on 1 page
Topic: The Sum of Ant Previous Topic   Next Topic Topic: Java Puzzlers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use