www.digitalmars.com         C & C++   DMDScript  

D - [BUG] compiler segfault

reply Ant <duitoolkit yahoo.ca> writes:
void main()
{
	int i = 10;
	while ( false, (i-- > 0) )
	{
		printf("i = %.*s\n", i);
	}
}

$ dmd T -I~/dmd/src/phobos

Segmentation fault

should it compile at all?

Ant
Mar 19 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Fri, 19 Mar 2004 17:38:36 -0500, Ant wrote:

grrr :(

I meant:
void main()
{
	int i = 10;
	while ( false, (i-- > 0) )
	{
		printf("i = %d\n", i);
	}
}

$ dmd T -I~/dmd/src/phobos
$ T

i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
i = 0

is this valid? "while ( false, (i-- > 0) )"

Ant
Mar 19 2004
next sibling parent Juan C <Juan_member pathlink.com> writes:
In article <pan.2004.03.19.22.44.02.7718 yahoo.ca>, Ant says...
On Fri, 19 Mar 2004 17:38:36 -0500, Ant wrote:

grrr :(

I meant:
void main()
{
	int i = 10;
	while ( false, (i-- > 0) )
	{
		printf("i = %d\n", i);
	}
}

$ dmd T -I~/dmd/src/phobos
$ T

i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
i = 0

is this valid? "while ( false, (i-- > 0) )"

Ant
Mar 19 2004
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Ant" <duitoolkit yahoo.ca> wrote in message
news:pan.2004.03.19.22.44.02.7718 yahoo.ca...
 On Fri, 19 Mar 2004 17:38:36 -0500, Ant wrote:

 grrr :(

 I meant:
 void main()
 {
 int i = 10;
 while ( false, (i-- > 0) )
 {
 printf("i = %d\n", i);
 }
 }

 $ dmd T -I~/dmd/src/phobos
 $ T

 i = 9
 i = 8
 i = 7
 i = 6
 i = 5
 i = 4
 i = 3
 i = 2
 i = 1
 i = 0

 is this valid? "while ( false, (i-- > 0) )"
Yes.
Mar 20 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Sat, 20 Mar 2004 12:16:07 -0800, Walter wrote:

 
 "Ant" <duitoolkit yahoo.ca> wrote in message
 is this valid? "while ( false, (i-- > 0) )"
Yes.
thank you. interesting: (from the manual) Expression: AssignExpression AssignExpression , Expression Anyone cares to explain why is it valid and what the use of "while ( false, (i-- > 0) )" ? thanks. what I was trying to do was something like "while ( !found && (i<lines.length) )" I don't know why I typed "," instead of "&&" but it cost me quite a bit of time to find the problem... Ant
Mar 20 2004
next sibling parent reply larry cowan <larry_member pathlink.com> writes:
In C, 
(expr1, expr2, expr3)
is a valid expression, and evaluates to the value of the rightmost
subexpression.  This is probably what is being carried forward.
So
(!found,i-- > 0)
would evaluate !found, then replace its evaluation with that of i-- > 0, and use
that - so it is NOT the same as 
(!found && i--> 0)

There are possible uses of side-effects, so something like
(i--,i++ > 0)
would check for i-1 > 0 without changing it.  Evaluation of a list of
expressions like this should always be left-to-right, with only the side-effects
being carried forward, the valuation is replaced with the next one until the
rightmost is reached.


In article <pan.2004.03.20.20.33.33.170646 yahoo.ca>, Ant says...
On Sat, 20 Mar 2004 12:16:07 -0800, Walter wrote:

 
 "Ant" <duitoolkit yahoo.ca> wrote in message
 is this valid? "while ( false, (i-- > 0) )"
Yes.
thank you. interesting: (from the manual) Expression: AssignExpression AssignExpression , Expression Anyone cares to explain why is it valid and what the use of "while ( false, (i-- > 0) )" ? thanks. what I was trying to do was something like "while ( !found && (i<lines.length) )" I don't know why I typed "," instead of "&&" but it cost me quite a bit of time to find the problem... Ant
Mar 20 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Sat, 20 Mar 2004 21:29:46 +0000, larry cowan wrote:

 In C, 
 (expr1, expr2, expr3)
 is a valid expression, and evaluates to the value of the rightmost
 subexpression.
[...]
 side-effects
[...]
 side-effects
Thank you. I didn't know. I believe I got it. should we vote to drop it!?... Ant
Mar 20 2004
parent reply Vathix <vathix dprogramming.com> writes:
Ant wrote:

 On Sat, 20 Mar 2004 21:29:46 +0000, larry cowan wrote:
 
 
In C, 
(expr1, expr2, expr3)
is a valid expression, and evaluates to the value of the rightmost
subexpression.
[...]
side-effects
[...]
side-effects
Thank you. I didn't know. I believe I got it. should we vote to drop it!?... Ant
I would, except it's common practice to use in a for loop: for(i = 0; i < 5; i++, s++) { ... } The D way could be different, but what? for(i = 0; i < 5; {i++; s++;}) { ... } That's not too bad, especially since the comma operator can be a bit tricky to use. -- Christopher E. Miller
Mar 20 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Sat, 20 Mar 2004 17:07:34 -0500, Vathix wrote:

 Ant wrote:
 
 On Sat, 20 Mar 2004 21:29:46 +0000, larry cowan wrote:
 
 
In C, 
(expr1, expr2, expr3)
is a valid expression, and evaluates to the value of the rightmost
subexpression.
I would, except it's common practice to use in a for loop: for(i = 0; i < 5; i++, s++) { ... } The D way could be different, but what? for(i = 0; i < 5; {i++; s++;}) { ... } That's not too bad, especially since the comma operator can be a bit tricky to use.
Yes, I did used it before.... Ant
Mar 20 2004
parent reply larry cowan <larry_member pathlink.com> writes:
On Sat, 20 Mar 2004 17:07:34 -0500, Vathix wrote:

 I would, except it's common practice to use in a for loop:
 for(i = 0; i < 5; i++, s++) { ... }
 
 The D way could be different, but what?
 for(i = 0; i < 5; {i++; s++;}) { ... }
 
Usage in the pre and post sections of a for loop is a bit different in that these don't get used as a value. You can in this way do if (x) a=1,b=2,c=3; without the braces or even if (x) f(x), g(x); to compress simple stuff and make it a quicker read. Not so great if they're not one-liners. The comma operator used in this way makes what would be multiple statements into a single one - the same effect as above in the for loop.
Mar 20 2004
parent reply Juan C <Juan_member pathlink.com> writes:
<snip>
if (x) a=1,b=2,c=3;  without the braces or even

if (x) f(x), g(x);  to compress simple stuff and make it a quicker read.
Not so great if they're not one-liners.
</snip> That sure seems like abuse to me. Hard for the uninitiated to read -- read "obfuscated". The comma operator has its (few) uses, but this aint it.
Mar 20 2004
next sibling parent C <dont respond.com> writes:
Whats good comma operator use ? the only thing ive seen it used for =

'reasonably' is C style macros.

On Sun, 21 Mar 2004 05:08:34 +0000 (UTC), Juan C =

<Juan_member pathlink.com> wrote:

 <snip>
 if (x) a=3D1,b=3D2,c=3D3;  without the braces or even

 if (x) f(x), g(x);  to compress simple stuff and make it a quicker re=
ad.
 Not so great if they're not one-liners.
</snip> That sure seems like abuse to me. Hard for the uninitiated to read -- =
 read
 "obfuscated".

 The comma operator has its (few) uses, but this aint it.
-- = D Newsgroup.
Mar 20 2004
prev sibling next sibling parent reply Ant <duitoolkit yahoo.ca> writes:
On Sun, 21 Mar 2004 05:08:34 +0000, Juan C wrote:

 <snip>
if (x) a=1,b=2,c=3;  without the braces or even

if (x) f(x), g(x);  to compress simple stuff and make it a quicker read.
Not so great if they're not one-liners.
</snip> That sure seems like abuse to me. Hard for the uninitiated to read -- read "obfuscated". The comma operator has its (few) uses, but this aint it.
Examples! Examples! but, but, it's broken: int f( int i ) { printf("i = %d\n", i); return i+14; } void f(int i, int j) { printf("i = %d\n", i+1); } void main() { int i = 1, j = 0; i = f( i , j ), f(( i , j )), f(( j , i )); } $ dmd T -I~/dmd/src/phobos T.d(16): cannot implicitly convert void to int Ant
Mar 20 2004
parent larry cowan <larry_member pathlink.com> writes:
It appears to be evaluated from left-to-right, but the assignment is
(trying to be) using the value of the leftmost expression.  It looks like the
assignment operator is taking precedence over the comma operator.  That's
correct, I believe.  Add one more set of parens:

i = ( f(i,j), f((i,j)), f((j,i)) );



In article <pan.2004.03.21.06.17.55.247300 yahoo.ca>, Ant says...
but, but, it's broken:

int f( int i )
{
	printf("i = %d\n", i);
	return i+14;
}

void f(int i, int j)
{
	printf("i = %d\n", i+1);
}

void main()
{
	int i = 1, j = 0;
	
	i = f( i , j ), f(( i , j )), f(( j , i ));

}

$ dmd T -I~/dmd/src/phobos
T.d(16): cannot implicitly convert void to int

Ant
Mar 21 2004
prev sibling parent larry cowan <larry_member pathlink.com> writes:
In article <c3j80i$h4$1 digitaldaemon.com>, Juan C says...
<snip>
if (x) a=1,b=2,c=3;  without the braces or even

if (x) f(x), g(x);  to compress simple stuff and make it a quicker read.
Not so great if they're not one-liners.
</snip> That sure seems like abuse to me. Hard for the uninitiated to read -- read "obfuscated". The comma operator has its (few) uses, but this aint it.
The uninitiated shouldn't be programming, or should learn or be taught. Obfuscated is something far more esoteric and dense - intentionally so. Basically I agree with you, if (x) { a=1; b=2; c=3; } is perfectly fine. I was saying what could be done, not advocating it. Perhaps you'ld like to suggest some of those "few" uses for me. As an operator, where the value is taken from the rightmost and used, please. Except for pre and post sections of a for loop where it's a distinct convenience, but only that, and more of a separator than a replacement operator, I've probably only used it in 3 or 4 of my last 300,000 lines of code.
Mar 20 2004
prev sibling parent reply larry cowan <larry_member pathlink.com> writes:
I tried a post a minute ago which didn't seem to take, so if this is a
duplicate, at least I hope it's a better thought out one...

In C,
(expr1,expr2,expr3)
would be evaluated left to right, with expr1 evaluated, then expr2 replacing the
value, then expr3 which would be the final value of the expression.

The value of (!found, i-- > 0) would be that of i-- > 0 with the !found value
effectively ignored. Thus it's not the same as (!found && i-- > 0).

There are uses where the side-effects of evaluating the left-side expressions
are meaningful.  This is not a really useful example, but (i--,i++ > 0) would
test for i-1 > 0 without modifying the value of i.

D seems to allow 

if (a=1,b=2,c=3,d=a+b+c,d == 6)

and evaluates it correctly here where it is not really appropriate, but does not
allow the same content in an assert statement where something like it might be
useful.


In article <pan.2004.03.20.20.33.33.170646 yahoo.ca>, Ant says...
On Sat, 20 Mar 2004 12:16:07 -0800, Walter wrote:

 
 "Ant" <duitoolkit yahoo.ca> wrote in message
 is this valid? "while ( false, (i-- > 0) )"
Yes.
thank you. interesting: (from the manual) Expression: AssignExpression AssignExpression , Expression Anyone cares to explain why is it valid and what the use of "while ( false, (i-- > 0) )" ? thanks. what I was trying to do was something like "while ( !found && (i<lines.length) )" I don't know why I typed "," instead of "&&" but it cost me quite a bit of time to find the problem... Ant
Mar 20 2004
parent reply Ant <duitoolkit yahoo.ca> writes:
On Sat, 20 Mar 2004 21:56:23 +0000, larry cowan wrote:

 I tried a post a minute ago which didn't seem to take, so if this is a
 duplicate, at least I hope it's a better thought out one...
 
 In C,
 (expr1,expr2,expr3)
 would be evaluated left to right, with expr1 evaluated, then expr2 replacing
the
 value, then expr3 which would be the final value of the expression.
 
 The value of (!found, i-- > 0) would be that of i-- > 0 with the !found value
 effectively ignored. Thus it's not the same as (!found && i-- > 0).
 
 There are uses where the side-effects of evaluating the left-side expressions
 are meaningful.  This is not a really useful example, but (i--,i++ > 0) would
 test for i-1 > 0 without modifying the value of i.
 
 D seems to allow 
 
 if (a=1,b=2,c=3,d=a+b+c,d == 6)
 
 and evaluates it correctly here where it is not really appropriate, but does
not
 allow the same content in an assert statement where something like it might be
 useful.
 
thanks, maybe I can see the benefit of that some day... void main() { assert(false, 0); } $ dmd T -I~/dmd/src/phobos T.d(3): found ',' when expecting ')' T.d(3): found '0' when expecting ';' following 'statement' T.d(3): found ')' instead of statement Ant
Mar 20 2004
parent Manfred Nowak <svv1999 hotmail.com> writes:
Ant wrote:
[...]
 	assert(false, 0);
[...]
 T.d(3): found ',' when expecting ')'
[...] You detected an inconsistency: parse.c: | check(TOKlparen, "assert"); | e = parseAssignExp(); expression.html: | AssertExpression: | assert ( Expression ) So long!
Mar 21 2004