Discussion:
[fpc-pascal] precedence "is" and "and"
Mattias Gaertner via fpc-pascal
2018-12-08 19:38:23 UTC
Permalink
Hi,

According to the docs, the "is" operator is fourth level, the "and" is
second level, so the "and" must be computed before the "is". But it
seems fpc treats "is" and "and" as same level:

{$mode objfpc}
uses Classes;
var
b: boolean;
o: TObject;
begin
// this compiles, but should fail:
if o is TComponent and b then ;

// this fails, correct
if b and o is TComponent then ;

// for completeness, this fails:
if TComponent and b then ;
end.

The strange thing, is that the Delphi compiler works as fpc and the
Delphi docs says the same as the fpc docs:
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Expressions_(Delphi)#Operator_Precedence


Mattias
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepa
Mattias Gaertner via fpc-pascal
2018-12-08 19:50:59 UTC
Permalink
On Sat, 8 Dec 2018 20:38:23 +0100
Post by Mattias Gaertner via fpc-pascal
[...]
According to the docs, the "is" operator is fourth level, the "and" is
second level, so the "and" must be computed before the "is". But it
[...]
Btw, there is a difference between fpc and delphi treating "is" and
"or" precedence. The following compiles in fpc, but not in Delphi:

if b or o is TComponent then ;

Same with "xor".

This compiles in fpc and Delphi:
if o is TComponent or b then ;

It seems under Delphi "is", "and", "or", "xor" have the same precedence
level and the Delphi docs are wrong.

Mattias
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lis
Mattias Gaertner via fpc-pascal
2018-12-08 19:56:54 UTC
Permalink
On Sat, 8 Dec 2018 20:38:23 +0100
Post by Mattias Gaertner via fpc-pascal
Hi,
According to the docs, the "is" operator is fourth level, the "and" is
second level, so the "and" must be computed before the "is".
Link:
https://www.freepascal.org/docs-html/ref/refch12.html

Mattias
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/
Marco van de Voort
2018-12-08 20:23:57 UTC
Permalink
Post by Mattias Gaertner via fpc-pascal
According to the docs, the "is" operator is fourth level, the "and" is
second level, so the "and" must be computed before the "is". But it
logical (boolean) vs bitwise AND difference?


_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/lis
Mattias Gaertner via fpc-pascal
2018-12-08 20:49:52 UTC
Permalink
On Sat, 8 Dec 2018 21:23:57 +0100
Post by Marco van de Voort
Post by Mattias Gaertner via fpc-pascal
According to the docs, the "is" operator is fourth level, the "and"
is second level, so the "and" must be computed before the "is". But
logical (boolean) vs bitwise AND difference?
I don't see where the bitwise should be involved here. There is no
integer.
The docs do not mention a different precedence lvl for logical/bitwise
"and".
It does not explain difference between "and" and "or".

Mattias
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.
Michael Van Canneyt
2018-12-08 21:03:11 UTC
Permalink
Post by Mattias Gaertner via fpc-pascal
On Sat, 8 Dec 2018 21:23:57 +0100
Post by Marco van de Voort
Post by Mattias Gaertner via fpc-pascal
According to the docs, the "is" operator is fourth level, the "and"
is second level, so the "and" must be computed before the "is". But
Hm.

In delphi, AS is second level, and 'is' is fourth level.

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Expressions_(Delphi)

If FPC does this differently, then I think this is an incompatibility.
Post by Mattias Gaertner via fpc-pascal
Post by Marco van de Voort
logical (boolean) vs bitwise AND difference?
I don't see where the bitwise should be involved here. There is no
integer.
The docs do not mention a different precedence lvl for logical/bitwise
"and".
Because to the best of my knowledge, here isn't any difference in
precedence, so there is nothing to explain.
Post by Mattias Gaertner via fpc-pascal
It does not explain difference between "and" and "or".
Of course it does. See section on Logical operators

https://www.freepascal.org/docs-html/current/ref/refsu46.html

or boolean operators

https://www.freepascal.org/docs-html/current/ref/refsu47.html

I must confess it assumes that the reader knows what logical 'and' and 'or'
means, but someone who does not know that will probably not read the docs.
(although, he will also not understand "not" either and maybe read the docs...)

Michael.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-
Mattias Gaertner via fpc-pascal
2018-12-08 21:45:11 UTC
Permalink
On Sat, 8 Dec 2018 22:03:11 +0100 (CET)
Post by Michael Van Canneyt
[...]
In delphi, AS is second level, and 'is' is fourth level.
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Expressions_(Delphi)
If FPC does this differently, then I think this is an incompatibility.
What has "as" to do with this thread?
Post by Michael Van Canneyt
Post by Mattias Gaertner via fpc-pascal
Post by Marco van de Voort
logical (boolean) vs bitwise AND difference?
I don't see where the bitwise should be involved here. There is no
integer.
The docs do not mention a different precedence lvl for
logical/bitwise "and".
Because to the best of my knowledge, here isn't any difference in
precedence, so there is nothing to explain.
Yes, that's my point. Even if bitwise "and" would play a role here, the
precedence would still be the same.
Post by Michael Van Canneyt
Post by Mattias Gaertner via fpc-pascal
It does not explain difference between "and" and "or".
Of course it does. See section on Logical operators
https://www.freepascal.org/docs-html/current/ref/refsu46.html
or boolean operators
https://www.freepascal.org/docs-html/current/ref/refsu47.html
I looked at it, but I still don't understand.
Perhaps you can explain the examples I gave?

// compiles, but should fail:
if o is TComponent and b then ;
if o is TComponent or b then ;

// compiles in fpc, but not in Delphi:
if b or o is TComponent then ;

// does not compile, correct
if b and o is TComponent then ;

Mattias
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freep
Michael Van Canneyt
2018-12-09 00:01:08 UTC
Permalink
Post by Mattias Gaertner via fpc-pascal
On Sat, 8 Dec 2018 22:03:11 +0100 (CET)
Post by Michael Van Canneyt
[...]
In delphi, AS is second level, and 'is' is fourth level.
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Expressions_(Delphi)
If FPC does this differently, then I think this is an incompatibility.
What has "as" to do with this thread?
Perhaps FPC treats "is" and "as" on the same level, it would explain the
error ?
Post by Mattias Gaertner via fpc-pascal
Post by Michael Van Canneyt
Post by Mattias Gaertner via fpc-pascal
Post by Marco van de Voort
logical (boolean) vs bitwise AND difference?
I don't see where the bitwise should be involved here. There is no
integer.
The docs do not mention a different precedence lvl for
logical/bitwise "and".
Because to the best of my knowledge, here isn't any difference in
precedence, so there is nothing to explain.
Yes, that's my point. Even if bitwise "and" would play a role here, the
precedence would still be the same.
Agreed. It was Marco who brought this up ?
Post by Mattias Gaertner via fpc-pascal
Post by Michael Van Canneyt
Post by Mattias Gaertner via fpc-pascal
It does not explain difference between "and" and "or".
Of course it does. See section on Logical operators
https://www.freepascal.org/docs-html/current/ref/refsu46.html
or boolean operators
https://www.freepascal.org/docs-html/current/ref/refsu47.html
I looked at it, but I still don't understand.
Perhaps you can explain the examples I gave?
if o is TComponent and b then ;
if o is TComponent or b then ;
My hypothesis is that FPC treats 'is' and 'as' on level 2
(contrary to documentation). This is consistent with what you observe.

If 'is' is second level (i.e. the same as "as") then that would explain why
it compiles, because the 'o is TComponent' will be evaluated first:

in the first line because evaluation happens from left to right,
in the second line because 'is' takes precedence over 'or'.
Post by Mattias Gaertner via fpc-pascal
if b or o is TComponent then ;
Delphi is consistent with 'is' being on a lover level:
"is" is lower level, so b or O is treated first.

Again, FPC's behaviour is consistent with treating 'is' on the same level as
'as': because 'is' takes precedence over 'or', you get no error.
Post by Mattias Gaertner via fpc-pascal
// does not compile, correct
if b and o is TComponent then ;
Indeed. (b and O) is evaluated first (left to right)

All is consistent with "is" and "as" being on the same level.

This is not how delphi does it.

So we adapt the documentation, or make the compiler delphi compatible.

Given that this has been in the compiler for years, I guess most people use
brackets (I know I do)

Michael.

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/l
Marco van de Voort
2018-12-09 11:56:16 UTC
Permalink
Post by Michael Van Canneyt
Post by Mattias Gaertner via fpc-pascal
I don't see where the bitwise should be involved here. There is no
integer.
The docs do not mention a different precedence lvl for logical/bitwise
"and".
My line of thought was the docs might mean bitwise there.
Post by Michael Van Canneyt
Because to the best of my knowledge, here isn't any difference in
precedence, so there is nothing to explain.
And that was what my post was, checking that.

I'm more of the "more parenthesis never hurt anybody" school, so
operator precedence beyond the normal mathematical is not my strong point.
Post by Michael Van Canneyt
I must confess it assumes that the reader knows what logical 'and' and 'or'
means, but someone who does not know that will probably not read the docs.
(although, he will also not understand "not" either and maybe read the docs...)
Maybe clarify that?
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lis
Michael Van Canneyt
2018-12-09 12:02:07 UTC
Permalink
Post by Marco van de Voort
Post by Michael Van Canneyt
Because to the best of my knowledge, here isn't any difference in
precedence, so there is nothing to explain.
And that was what my post was, checking that.
I'm more of the "more parenthesis never hurt anybody" school, so
operator precedence beyond the normal mathematical is not my strong point.
I am of the same school. When in doubt, use parentheses.
This is unambiguous.
Post by Marco van de Voort
Post by Michael Van Canneyt
I must confess it assumes that the reader knows what logical 'and' and 'or'
means, but someone who does not know that will probably not read the docs.
(although, he will also not understand "not" either and maybe read the docs...)
Maybe clarify that?
Surely you are joking ?

The documentation is there to explain how the pascal language works,
not how programming and computers in general work.

Michael.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailm

Martin Wynne
2018-12-08 22:10:24 UTC
Permalink
Post by Mattias Gaertner via fpc-pascal
var
b: boolean;
o: TObject;
begin
if o is TComponent and b then ;
It will compile if $BOOLEVAL is on the default (-) because the result
can be determined without considering precedence, see:

https://www.freepascal.org/docs-html/prog/progsu4.html

Try it the other way round and it will fail:

o: TComponent;
if o is TObject

Martin.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lis
Loading...