Re: writing a compiler...
8 Jun 2003 22:00:05 -0400

          From comp.compilers

Related articles
writing a compiler... (Tommy) (2003-06-03)
Re: writing a compiler... (Vasile Rotaru) (2003-06-05)
Re: writing a compiler... (2003-06-05)
Re: writing a compiler... (Jeff Kenton) (2003-06-05)
Re: writing a compiler... (Chris F Clark) (2003-06-05)
Re: writing a compiler... (2003-06-08)
Re: writing a compiler... (2003-06-20)
Re: writing a compiler... (Conor O'Neill) (2003-06-20)
Re: writing a compiler... (Lex Spoon) (2003-06-25)
| List of all articles for this month |

Newsgroups: comp.compilers
Date: 8 Jun 2003 22:00:05 -0400
Organization: Compilers Central
References: 03-06-016 03-06-046
Keywords: design
Posted-Date: 08 Jun 2003 22:00:05 EDT

Martin wrote:

>IIRC it's Pascal that allows something like:
>if f(a) and g(b) then ...
>to evaluate f() and g() in either order.
>But if f() and g() have side-effects such that the behaviour of the
>program is determined by which executes first, then the program is
>wrong. I don't think that's something that can be checked by a
>compiler on an arbitrary program?

Pascal does allow this. Why not? Or, to put it another way, why would
you expect or assume that the functions would be executed in any
particular order? Why should it matter? ... it matters because you
have written 'tricky code'...

If you need to be sure of the execution order then write this:

if f(a) then
    if g(b) then...

If you rely on the execution order then you also rely on the function
being executed in the first place, which may not be true when it is
used in a boolean expression and the optimiser implements
short-circuit boolean evaluation.

In the example above

        if f(a) and g(b) then ...

if f(a) is false then g(b) will not be called (or vica versa!), since the
result is certain to be false.

AFAIK this is not unique to Pascal...

The lesson here is that a function (in the Pascal sense) should not
have any side-effects, so that the order of execution does not
matter. A Pascal Procedure (a void C function) should be used to
perform some action, to do some work, whereas a Pascal Function
(non-void C function) should *just* return a value.

I think this could be classed as a GOTCHA! as recently described by Nick

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.