Operator precedence changes

dwsHere is a summary of recent DWScript changes, the major one is a change in operator precedence to something similar to Delphi and FreePascal.

Other changes are related to sets and bug fixes.

The precedence of operators has been changed to be inline with Delphi & FreePascal, this means that

  • AND, AS, SHL, SHR and SAR precedences are increased
  • IN and IMPLIES precedences are decreased

And the DWScript operator precedence table is now

  1. @, not
  2. *, /, div, mod, and, shl, shr, sar, as, ^, <<, >>
  3. +, -, or, xor
  4. =, <>, <, >, <=, >=, in, is, implies, implements

Other miscellaneous notes on operators:

  • operators ^, <<, >> are only supported for operator overloading, and do not have a default meaning, though ^ was intended for vector and matrix operations (rather than legacy dereferencing), while << and >> were intended for streaming or prefixing/post-fixing (rather than shr/shl alternatives).
  • SAR is the “shift arithmetic right” operator, which preserves the sign bit while SHR does not.
  • IMPLIES has low precedence like in Oxygene.

Other changes:

  • small sets (with 31 elements or less) can now be cast to integer and vice-versa
  • fixed support for set constants in default function parameters
  • fixed support for the empty sets as function parameter
  • inline record constants are now supported in record and class declarations (for field default values)
  • fixed an issue with break/continue in the JIT compiler when encountered in blocks that are not supported by the JIT compiler (bug in the fallback mechanism).
  • fixed an issue with RTTI symbol enumeration across units

5 thoughts on “Operator precedence changes

  1. Uh oh! Operator precedence changes. This might break some existing script code! Better add some new conditional defines so scripts will know what version of DWS they’re running on… 😉

  2. @Mason
    This was in fact not a feature change, but a fix.
    Being inline with FreePascal, Delphi, Turbo Pascal, GnuPascal, and every pascal compiler around is mandatory.
    I compile my code with Delphi, FPC and SMS, and I’ve 98% of shared code.
    Previous operator precedence was an issue.

    @Eric
    Thanks a lot for the fixes, and nice addition (like set cast to integer)!
    AFAIK I was at the origin of some of those: thanks for reacting and fixing them so quickly!
    Will it be part of the next SMS hotfix?
    Thanks again. DWS and SMS do rock…

  3. @Mason why “new” conditional defines? 😉 https://code.google.com/p/dwscript/wiki/ConditionalCompilation

    @Arnaud yes, should all be in the hot fix.
    I’ll readily admit to having overlooked operator precedence as I never rely on outside the arithmetic operators (an habit from c/c++ years), and to illustrate that, none of the existing unit tests were affected by the change… Though outside bit shifting expressions. I guess real world code that can be affected will be rare. I’ll also admit I don’t understand why or & and don’t have the same precedence.

  4. >AND, AS, SHL, SHR and SAR precedences are increased

    But isn’t Pascal operator precedence itself broken? It’s identical to C’s, which Richie admitted in hindsight wasn’t a good idea.

    So now one would need to write

    If (x=7) or (x=8)

    rather than

    if x = 7 or x = 8

    like in every other (sane) implementation of precedence? 🙁

    You were my HERO. You FOUGHT THE MAN. Allan Bauer trembles with fear when he hears your name! I consistently predict that once DWScript is paired with a plugin for a major open source IDE (JetBrains, Eclipse, etc.) and full Qt bindings, Delphi is finished. Please don’t backslide on me and start porting Pascal’s mistakes into DWScript..

    😉

  5. @Joseph the underlying issue is that Pascal only differentiates between bitwise and logical operators through the types, consider it a case of abusive operator overloading or too-aggressive type-inference 😉

    Not sure how/if this can be changed in a “pascalish” way.

Comments are closed.