Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!greg From: greg@utcsri.UUCP (Gregory Smith) Newsgroups: net.lang.c Subject: Re: expr?(void):(void) WHY I THINK IT'S *OK* ( or should be ) Message-ID: <3220@utcsri.UUCP> Date: Sun, 3-Aug-86 00:19:36 EDT Article-I.D.: utcsri.3220 Posted: Sun Aug 3 00:19:36 1986 Date-Received: Wed, 6-Aug-86 10:15:34 EDT References: <501@bunny.UUCP> <500@copper.UUCP> <273@watmath.UUCP> Reply-To: greg@utcsri.UUCP (Gregory Smith) Distribution: net Organization: CSRI, University of Toronto Lines: 172 Summary: long, but sparse. In article <1983@watmath.UUCP> rbutterworth@watmath.UUCP (Ray Butterworth) writes: >I'm converted. I've seen the light. I'm studing the Bible much more >closely. And look what I've found: > > comma-expression -> expression , expression > The type and value of the result are the type and value > of the right operand. > DMR, Chapter 7, Verse 15. > >But, isn't that what it says about the "?:" expression too? >Hallelujah! I've found a sin in our compiler. It actually >allows those evil "void expressions" on the right of a comma. >Why, this means we'll have to go back through the last few >weeks of news, take all those articles talking about void >functions and "?:" and repost them, this time changing the >"?:" to ",", since all the arguments in them hold equally >well. > >In the meantime, I urge everyone (well, those mentioned above anyway), >to put the follwing fix into their compilers. I'm sure it will find >many occurrences of this sin that may have crept into their code over >the years. BUT... BUT .... BUT... you mean I can't void a(),b(),c(); for( a(), b(); ... ; a(), c() ){ ???????????????????? I can't even i = ( a(), b(), 2 ); /* b() on RHS of 1st , */ ??? This is getting worse! ( I strongly suspect Mr Butterworth agrees with me...) There are two things going on in this debate: (1) whether the Powers That Be condone e?void:void (2) whether it makes sense and should be allowed. I haven't the foggiest about (1) but I believe strongly that it *should* be allowed. Herein is described in brief my belief system pertaining to this. It is based on CONTEXTS, not an original idea by any means. There are 4 contexts of importance in C: The void context, the conditional context, the value context, and the lvalue context. - Any expression in a void context is evaluated for side-effects only. - Any expression in a conditional context is evaluated to see if it is zero or non-zero. ( this is only significant if the expression has a logical-valued operator at its top level ). - The value of an expression in a value context is always used. - There is another context, the Lvalue context, which will only serve to cloud this particular issue, so I will ignore it, and lump it under 'value'. Let EV = expr in void context EC = expr in cond context EX = expr in value context. Then STMT ::= EV; | while( EC ) STMT | do STMT while( EC ); | for( EV; EC ; EV )STMT | switch( EX )STMT | if(EC) STMT | if(EC) STMT else STMT | return EX; The above are all of the statement forms containing expressions (maybe I missed a few ). The operators also create a particular context: expr ::= EC && EC EC || EC ! EC EC ? EE : EE <<< looky EV, EE <<< here < any other unary op> EX EX < any other binary op> EX EX[EX], EX(), EX++,EX--, EX(EX,..) etc etc ( here's the point ) I haven't said what EE is. Any expression in an EE context has the same effective context of the ',' or '?:' operator containing it. I.e. the context is inherited. Thus: if( x? p: q ) i ? a : b ; Since the first ?: is in a EC context, p and q are in conditional contexts. Likewise 'a' and 'b' are in void contexts. The expressions e1 and e2 below are in a value context: x = ( i ((p(),q()),...) So the context of q() should be inherited through the inner ',' from the LHS of the other ',' I.e., a void context. This is much more than a language purity point - this kind of analysis is necessary to generate decent testing code. If 'x THING2 ? THING1 : THING2 ]; Obviously it's going to be a constant. Any lint authors listening? Turn that off when a constant expression is called for. While I'm wishing for things, I wanna see 'assignment in conditional context'. We all know why by now... So what's the story? Isn't this The Way It Is? Doesn't the above make a whole lotta sense? Why the *^&%#^&%&^@ would e?void:void _in_a_void_ _context_ be considered linty? -- "You'll need more than a Tylenol if you don't tell me where my father is!" - The Ice Pirates ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg