Related articles |
---|
writing a compiler... ltk_RE_MO_VE_@libero.it (Tommy) (2003-06-03) |
Re: writing a compiler... vrotaru@seznam.cz (Vasile Rotaru) (2003-06-05) |
Re: writing a compiler... m.a.ellis@ucsnew1.ncl.ac.uk (2003-06-05) |
Re: writing a compiler... JeffKenton@attbi.com (Jeff Kenton) (2003-06-05) |
Re: writing a compiler... cfc@shell01.TheWorld.com (Chris F Clark) (2003-06-05) |
Re: writing a compiler... Steve_Lipscombe@amat.com (2003-06-08) |
Re: writing a compiler... vbdis@aol.com (2003-06-20) |
Re: writing a compiler... Conor.ONeill.NoSpamPlease@logicacmg.com (Conor O'Neill) (2003-06-20) |
Re: writing a compiler... lex@cc.gatech.edu (Lex Spoon) (2003-06-25) |
From: | Steve_Lipscombe@amat.com |
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
Maclaren.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.