Board index » delphi » Pascal evaluation order: how does Borland do it and what says the standard

Pascal evaluation order: how does Borland do it and what says the standard

(I think the standard does not say anything about it except it is undefined)

What function is called first, f or g?

writeln (f(2) + g(3));

--
Rudolf Polzer
REBOUNCE - http://www.mycgiserver.com/~rebounce
I wish I was what I was when I wished I was what I am now.

 

Re:Pascal evaluation order: how does Borland do it and what says the standard


Quote
Seiberdragan wrote...

>(I think the standard does not say anything about it except it is undefined)

>What function is called first, f or g?

>writeln (f(2) + g(3));

g() first, at least on my TP5.5.
It's easy to test it out yourself.

Re:Pascal evaluation order: how does Borland do it and what says the standard


"Mason Ip" <maso...@attachmate.co_> schrieb im Newsbeitrag
news:Zq1I5.716$l87.43446308@news1.van.metronet.ca...

Quote
> Seiberdragan wrote...

> >(I think the standard does not say anything about it except it is
undefined)

> >What function is called first, f or g?

> >writeln (f(2) + g(3));

> g() first, at least on my TP5.5.
> It's easy to test it out yourself.

Of course I can test. But I wanted to know if there is some Borland
documentation about it, or what the standard says (I think it says
"undefined behaviour").

BTW, the same in TP7

--
Rudolf Polzer
REBOUNCE - http://www.mycgiserver.com/~rebounce
I wish I was what I was when I wished I was what I am now.

Re:Pascal evaluation order: how does Borland do it and what says the standard


Quote
> > Seiberdragan wrote...
> > >(I think the standard does not say anything about it except it is
> undefined)
> > >What function is called first, f or g?

> > >writeln (f(2) + g(3));

> > g() first, at least on my TP5.5.
> > It's easy to test it out yourself.

> Of course I can test. But I wanted to know if there is some Borland
> documentation about it, or what the standard says (I think it says
> "undefined behaviour").

> BTW, the same in TP7

That also happens on my TP7, regardless of "complete boolean eval".
(after all, that switch is not related to non-boolean evaluation)
However, this does not happen in FreePascal.

Contrary to common belief, although parentheses are considered to be
of highest precedence, they are not necessary evaluated earlier than
other subexpressions parallel to them. (see attached program)

I think it is not so "undefined" as far as the compiler build is known.
Usually a specific version of compiler is hardcoded to do this in
a fixed way. However it may change from versions to versions, and
from dialects to dialects.

According to the VFAQ of the C language newsgroup, and from my beginner
C textbook, the sequence in which they are executed are controlled by
the compiler, that the programmer must not make any assumptions with.
Since people may attempt to port published programs to other platforms,
assumptions may cause problems. I think the same applies to Pascal.

To control the sequence of evaluation, set up some temporary variables
to hold the intermediate results, just the way you use parentheses.

{ a simple program to try on your favourite Pascal.
The last statement generates a Divide-by-Zero fault.
This is used to test whether the compiler does a denominator-
equal-to-zero check prior to division }

function f(v:boolean):boolean;
begin  write('f',v,' ');  f:=v;  end;

function g(v:boolean):boolean;
begin  write('g',v,' ');  g:=v;  end;

function p:string;  begin  write('p ');  p:='P';  end;

function q:string;  begin  write('q ');  q:='Q';  end;

function m(v:longint):longint;
begin  write('m',v,' ');  m:=v;  end;

function n(v:longint):longint;
begin  write('n',v,' ');  n:=v;  end;

begin
  writeln( f(true) and g(true) );
  writeln( g(true) and f(true) );
  writeln( f(false) and g(true) );
  writeln( g(false) and f(true) );
  writeln(m(1)+n(2)+m(3)+n(4));
  writeln(m(1)+n(2)+(m(12) div n(4)));
  writeln((m(3)+n(8)+m(9)) div n(4));
  writeln((m(1) + n(1)) div (m(2) - n(2)));
end.

Re:Pascal evaluation order: how does Borland do it and what says the standard


In article <8sqv2c$bj...@news.doit.wisc.edu>,

Quote
rdtsc <ryan...@hotmail.com> wrote:

>I think it is not so "undefined" as far as the compiler build is known.
>Usually a specific version of compiler is hardcoded to do this in
>a fixed way. However it may change from versions to versions, and
>from dialects to dialects.

Undefined does not mean random or varying. Undefined means that it has
not been defined in the documentation. For example some people think
that the fact that TP 7.0 clears the data segment somehow violates the
rule that initial values of variables are undefined. Of course it does
not.

Osmo

Re:Pascal evaluation order: how does Borland do it and what says the standard


"rdtsc" <ryan...@hotmail.com> schrieb im Newsbeitrag
news:8sqv2c$bju$1@news.doit.wisc.edu...

Quote
> > > Seiberdragan wrote...
> > > >(I think the standard does not say anything about it except it is
> > undefined)
> > > >What function is called first, f or g?

> > > >writeln (f(2) + g(3));

> > > g() first, at least on my TP5.5.
> > > It's easy to test it out yourself.

> > Of course I can test. But I wanted to know if there is some Borland
> > documentation about it, or what the standard says (I think it says
> > "undefined behaviour").

> > BTW, the same in TP7

> That also happens on my TP7, regardless of "complete boolean eval".
> (after all, that switch is not related to non-boolean evaluation)
> However, this does not happen in FreePascal.

Thanks, this was what I wanted to read. If FPC and TP do different things,
one can conclude, that the standard does not define it. So it is the same as
in C.
And are boolean expressions also evaluated from left to right when shortcut
evaluation is off? Or is this also undefined behaviour?

Quote

> Contrary to common belief, although parentheses are considered to be
> of highest precedence, they are not necessary evaluated earlier than
> other subexpressions parallel to them. (see attached program)

> I think it is not so "undefined" as far as the compiler build is known.
> Usually a specific version of compiler is hardcoded to do this in
> a fixed way. However it may change from versions to versions, and
> from dialects to dialects.

undefined means: do not rely on it, you may fail when a new compiler comes
out, or you try an optimization switch.

- Show quoted text -

Quote

> According to the VFAQ of the C language newsgroup, and from my beginner
> C textbook, the sequence in which they are executed are controlled by
> the compiler, that the programmer must not make any assumptions with.
> Since people may attempt to port published programs to other platforms,
> assumptions may cause problems. I think the same applies to Pascal.

> To control the sequence of evaluation, set up some temporary variables
> to hold the intermediate results, just the way you use parentheses.

> { a simple program to try on your favourite Pascal.
> The last statement generates a Divide-by-Zero fault.
> This is used to test whether the compiler does a denominator-
> equal-to-zero check prior to division }

> function f(v:boolean):boolean;

I know this trick, but I wanted to know if in Pascal there is the same
problem as in C:

int i = 2;
printf ("%d\n", i + i++);

4 or 5? Most compilers output 5 (RTL evaluation), but not all compilers do.

--
Rudolf Polzer
REBOUNCE - http://www.mycgiserver.com/~rebounce
I wish I was what I was when I wished I was what I am now.

Re:Pascal evaluation order: how does Borland do it and what says the standard


JRS:  In article <8sq27k$uf...@riker.addcom.de> of Fri, 20 Oct 2000
20:18:44 seen in news:comp.lang.pascal.borland, Seiberdragan

Quote
<rpol...@web.de> wrote:
>(I think the standard does not say anything about it except it is undefined)

>What function is called first, f or g?

>writeln (f(2) + g(3));

If the standard says undefined, then it is undefined by definition.

This means that the order can depend on anything at all.  The only thing
one can rely on (probably) is that the same compiler on the same program
should give the same order.

It may change between compiler versions.

It may be different in DOS / DPMI / WINDOWS.

It may be different in different dialects of Pascal.

It may depend on previous statements.

It may depend on something that none of us have thought of.

You MUST not depend on it, since it will be pretty consistent but may
jump up and bite you when you least expect it.

Use, for example, X := f(2) ; Writeln(X + g(3)) ;

Re another post :- please read Timo's quotmarg.html, which IIRC explains
about the proper, courteous, and economical use of quoting, including
not quoting signatures.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME ?
  Prof Timo Salmi's Usenet Q&A <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip>
  TS FAQs via : http://www.uwasa.fi/~ts/http/ : tsfaq.html quotmarg.html &c.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

Re:Pascal evaluation order: how does Borland do it and what says the standard


In article <i+kUTPAJRX85E...@merlyn.demon.co.uk>,
Dr John Stockton  <j...@merlyn.demon.co.uk> wrote:
:Re another post :- please read Timo's quotmarg.html, which IIRC explains
:about the proper, courteous, and economical use of quoting, including

The full reference being, for anyone interested

      http://www.uwasa.fi/~ts/http/quote.html

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland
Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html

Re:Pascal evaluation order: how does Borland do it and what says the standard


Im Artikel <8sq27k$uf...@riker.addcom.de>, "Seiberdragan" <rpol...@web.de>
schreibt:

Quote
>(I think the standard does not say anything about it except it is undefined)

Best guess ;-)

BTW, which "standard" do you mean? I assume that some ISO-Pascal standard
exists, but I'm not really sure. Borland introduces a new "standard" with every
new version of TP, BP or Delphi, and I'm pretty sure that OPL was never
specified nor documented reliably.

DoDi

Re:Pascal evaluation order: how does Borland do it and what says the standard


"VBDis" <vb...@aol.com> schrieb im Newsbeitrag
news:20001021144135.05856.00000949@nso-fp.aol.com...

Quote
> Im Artikel <8sq27k$uf...@riker.addcom.de>, "Seiberdragan" <rpol...@web.de>
> schreibt:

> >(I think the standard does not say anything about it except it is
undefined)

> Best guess ;-)

> BTW, which "standard" do you mean? I assume that some ISO-Pascal standard
> exists, but I'm not really sure. Borland introduces a new "standard" with
every
> new version of TP, BP or Delphi, and I'm pretty sure that OPL was never
> specified nor documented reliably.

> DoDi

ISO Pascal, not Extended ISO Pascal. (there are two "ISO Pascal"s out
there). These are the only Pascal standards.

--
Rudolf Polzer
REBOUNCE - http://www.mycgiserver.com/~rebounce
I wish I was what I was when I wished I was what I am now.

Re:Pascal evaluation order: how does Borland do it and what says the standard


Quote
Seiberdragan wrote:

standard
> > exists, but I'm not really sure. Borland introduces a new "standard" with

Actually, IIRC the point that what we now know as Borland dialects
started to deviate from ISO pascal is with UCLA pascal, on which the
original dialect of Borland was based.

--

Marco van de Voort (Mar...@stack.nl or ma...@freepascal.org)

Re:Pascal evaluation order: how does Borland do it and what says the standard


Im Artikel <8srs7v$ao...@riker.addcom.de>, "Seiberdragan" <rpol...@web.de>
schreibt:

Quote
>And are boolean expressions also evaluated from left to right when shortcut
>evaluation is off? Or is this also undefined behaviour?

In D4 I only found defined the order for the short circuit evaluation:
Quote

Short-circuit evaluation means strict left-to-right evaluation that stops as
soon as the result of the entire expression is determined.
<<
This statement suggests that the order otherwise is undefined.

DoDi

Re:Pascal evaluation order: how does Borland do it and what says the standard


Quote
In article <8srs7v$ao...@riker.addcom.de>, Seiberdragan wrote:
>"rdtsc" <ryan...@hotmail.com> schrieb im Newsbeitrag
>news:8sqv2c$bju$1@news.doit.wisc.edu...
>> > > Seiberdragan wrote...
>> > "undefined behaviour").

>> > BTW, the same in TP7

>> That also happens on my TP7, regardless of "complete boolean eval".
>> (after all, that switch is not related to non-boolean evaluation)
>> However, this does not happen in FreePascal.

>Thanks, this was what I wanted to read. If FPC and TP do different things,
>one can conclude, that the standard does not define it. So it is the same as
>in C.

That is not the case. FPC might deliberately do things different because of
what we consider to be bugs in TP (sometimes prolonged through several
tp versions because of compability).

However there are only very few of these cases, and most are documented.

Re:Pascal evaluation order: how does Borland do it and what says the standard


Quote
> > > exists, but I'm not really sure. Borland introduces a new "standard" with

> Actually, IIRC the point that what we now know as Borland dialects
> started to deviate from ISO pascal is with UCLA pascal, on which the
> original dialect of Borland was based.

Don't you mean UCSD Pascal rather ? Anyway Borland didn't follow
any standard since there were maybe no real standard before 1990
(7185 ISO standard). And... would they have cared about standards ?
A reason there was no Borland Ada83 seem they couldn't add
proprietary extensions.

______________________________________________________
Gautier  --  http://members.xoom.com/gdemont/gsoft.htm

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:Pascal evaluation order: how does Borland do it and what says the standard


On Fri, 20 Oct 2000 19:55:37 GMT, maso...@attachmate.co_ (Mason Ip)
wrote:

Quote
>Seiberdragan wrote...

>>(I think the standard does not say anything about it except it is undefined)

>>What function is called first, f or g?

>>writeln (f(2) + g(3));

>g() first, at least on my TP5.5.
>It's easy to test it out yourself.

It's not so easy:
I often use functions like that :

 Function SetAt( i : integer ) : Str1;    { TYPE Str1 = String[1]; }
 Begin
   Write;                   { == "flush" }
   TextAttr := i;        { some own range checking for i omitted }
   SetAt := '';           { =: empty String }
 End;{SetAt}

in statements like

Write(  SetAt(1),  ' TextAttribute is 1 ',  
           SetAt(2), ' TextAttribute is 2 ' );

to change the textcolor  within one Write-Stmt.
The order of execution follows the order of
SetAt's in Write(..);
It works as long you have the {empty} Write;  
statement in the function.
The function  is not required to be an empty string.
If you write
  SetAt := 'Something';
instead of
  SetAt := '';
"Something" will be written also.
{Function's Type has to be a longer
string then}.
This works in TP3, TP5.5 and later, but
not in _one_ of TP4 or TP5.

W.Riedel

Go to page: [1] [2]

Other Threads