Advice on operator precedence

For everything that's not in any way related to PureBasic. General chat etc...
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Advice on operator precedence

Post by Dare2 »

Like just about everyone else and their dog, I am writing a compiler. I am doing this to learn, and for personal use.

I am struggling a bit on deciding on the precedence of operators and would appreciate feedback. Currently I have it organised as follows:
  • Three operator groups: Arithmetic, Comparative, and Bitwise (Logical)
    With expressions containing mixed operators, precedence is (A) Arithmetic (B) Bitwise (C) Comparitive
    Within groups, precedence is as shown below.
    Anything inside () will be completely resolved before being applied to any expression on either side.
    Final level of precedence is first come, first served, reading Left to Right.

    Comparitives will return 0 or 1 if required as a result (eg, part of a larger operation or stored, etc), but where possible this extra step will be avoided and a simple false (0) or true (not false , not zero) will be used.

    Arithmetic operators:

    1 Unary plus
    1 Unary minus
    2 Exponentiation
    3 Shift left
    3 Shift right
    4 Multiply
    4 Divide
    5 Integer divide (Truncate/Ignore remainder)
    6 Modulus
    7 Rotate left
    7 Rotate right
    8 Add
    8 Subtract

    Comparisons:

    1 Equal to
    2 Unequal
    3 Less than
    3 Greater than
    4 Less than or Equal To
    4 Greater than or Equal To

    BitWise

    1 Not
    2 And
    3 Or
    4 Xor

    (Note: Other things like Eqv, NotEqv, Imp, Nand, Nor can either be derived from, or are the same as, the above, so I don't intend to support them)
However I notice that this does not exactly match the priorities in other compilers (and indeed, they differ between themselves).

Any thoughts or ideas? Any comments that would be useful regarding precedence?

Thanks! :)
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Advice on operator precedence

Post by Trond »

My suggestion:

Code: Select all

Name            Operator    Precedence  Associativity
String index    []          1
Exponentiation  ^           1           right to left!!!!!
Unary minus     -           1

Multiplication  *           2
Division        /           2
Integer div     div or \    2
Modulo          %           2 (or 2.5)
Inverse modulo  INV         2 (or 2.5)
Left Bit Shift  SHL         2
Right Bit Shift SHR         2
Rotation?

Addition        +           3
Subtraction     -           3

Equality        =           4
Inequality      <> or !=    4
Less than       <           4
Greater than    >           4
< or equal      <=          4
> or equal      >=          4

Logical not     NOT         5
Logical and     AND         6
Logical or      OR          6
Logical xor     XOR         6
They should be evaluated left to right except for the exponentiation. Where to put binary not/and/or/xor I do not know.

Things to keep in mind:
- The fewer levels, the faster the compiler.
- The fewer levels, the easier to implement.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi Trond,

Thanks!

By fewer levels you mean no grouping and few levels of priority? (Easier to implement sounds really good to me!)

Two things I don't understand:

1: Inverse Modulo (INV) is - what?
2: How do string indexes (the []) fit into this?

Also, I really like the idea of comparisons (and NOT as opposed to binary or logical ! not) returning 0 or 1 however it has a speed cost as far as I can tell (or think). :) The idea being the result can then be used arithmetically. Do you have any thoughts/opinions on this?

Thanks again!
@}--`--,-- A rose by any other name ..
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

@Trond

You have forgot to add parenthesis.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Dare2 wrote:By fewer levels you mean no grouping and few levels of priority? (Easier to implement sounds really good to me!)
Yes. But it's mostly the speed of the compiler that is affected by this.
Dare2 wrote:1: Inverse Modulo (INV) is - what?
No idea, but I think it's the ... drumroll ... inverse of modulo! Actually, I've never used it.
Dare2 wrote:2: How do string indexes (the []) fit into this?
It's to you can do funky stuff like "asdf"+str(5-3)+string$[5<=d and z]. It's probably not very useful.
Dare2 wrote:Also, I really like the idea of comparisons (and NOT as opposed to binary or logical ! not) returning 0 or 1 however it has a speed cost as far as I can tell (or think). :) The idea being the result can then be used arithmetically. Do you have any thoughts/opinions on this?
Yes, letting them return 0 or 1 should be fast enough.

Edit:
I didn't forget the parentheses, but I assumed Dare2 knows the only obvious thing to do with them.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Trond wrote:.. but I assumed Dare2 knows the only obvious thing to do with them.
hehe. :D (Bites tongue).

I am intending to create an obj directly, and think I have the coff format understood well enough to do so. However, working out the opcodes is now proving interesting, with nifty meaningful (after much headscratching) stuff like REX.W + 0F 21/r and r/m32, imm32 being giving as the explanation of how the thing is built. :D
@}--`--,-- A rose by any other name ..
Post Reply