Precedence testing

new topic     » topic index » view thread      » older message » newer message

I've added six new operators to phix:

&& - bitwise and, same as and_bits(lhs,rhs) 
|| - bitwise or, same as or_bits(lhs,rhs) 
>> - shift right, same as floor(lhs/power(2,rhs)) 
<< - shift left, same as lhs*power(2,rhs) 
~ - same as length(rhs) [unary] 
. - subscripts and/or struct/class members 

and obviously they deserve proper precedence handling. At the same time, I'm going to split == and != to have a slightly lower precedence to < > <= and >= (in line with the rest of the planet, bar Python).

Of course that means I could easily spanner things, and realised the test suite does not actually have a proper set of precedence-related tests, so I'm about to pen a new tests/t66prec.exw and populate it with things like:

    2+3*4 is 2 + (3 * 4) = 14, not (2 + 3) * 4 = 20. 
    2*3+4 is (2 * 3) + 4 = 10, not 2 * (3 + 4) = 14. 
    1-2+3 is (1 - 2) + 3 = 2, not 1 - (2 + 3) = -4 

and figured I may as well ask on here for any similar examples anyone thinks should be added.

The new precedence table will be:

        highest precedence:     parenthesis/function/type calls/ternary operator 
 
                                subscripts/slices  . 
 
                                unary-  unary+  not  ~ 
 
                                *  / 
 
                                +  - 
                                 
                                >> << 
 
                                & 
 
                                <  >  <=  >= 
                                 
                                =  != 
                                 
                                && || 
 
                                and  or  xor 
 
        lowest precedence:      { , , , } 
 

Note that phix does not really do associativity handling: of course unary operators are right associative, because they have to be, but everything else is left associative. For example

a*b/c*d/e is ((((a*b)/c)*d)/e) 
a*-b/c*not d/e is ((((a*(-b))/c)*(not d))/e) 

So, if you've got any (perhaps slightly less obvious) code snippets you'd rather not see broken, or any other comments, post 'em here!

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu