🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Printf debugging for the win.

Published February 07, 2009
Advertisement
After 40 minutes in a debugger tracing through 'bytecode' (not really bytecode, but elemental runtime stuff), I found that the inner generator was at 1 by the time main printed 0. So somewhere the inner generator was being run twice before the outer generator actually returned control to main.

Make sense? No; of course not. And 'somewhere' isn't useful for problem solving. So I resorted to doing printf debugging in the language itself. 3 minutes later and I've a much clearer picture of at least where the problem occurs:

public static yields<int> take(int amount, IEnumerable<int> fromCollection){	local int ix = 0;	foreach( int rtn in fromCollection ){		print "start of take foreach.\r\n";		if( !(ix < amount) ){			print "before take return.\r\n";			return();		}		print "before take yield. returning rtn: " rtn " \r\n";		yield rtn;		print "after take yield. rtn is still: " rtn " \r\n";		ix = ix + 1;		print "end of take foreach.\r\n";	}	print "exit take.\r\n";}public static yields<int> allInt(){	local int i = 0;	while(true){		print "before allInt Yield. yielding i: " i " \r\n";		yield i;		print "after allInt Yield. i is still: " i " \r\n";		i = i + 1;	}}public static void main(){	foreach( int x in take(5,allInt()) ){		print "X: " x " \r\n";	}}


before allInt Yield. yielding i: 0 start of take foreach.before take yield. returning rtn: 0 after allInt Yield. i is still: 0 before allInt Yield. yielding i: 1 X: 0 *snip*


Yuck. The core of the problem is at output line 3->4. take's yield is supposed to return to main so the result gets printed out. Instead, it's moving the execution pointer into allInt and causing that to run again. It does it after setting the return value though, since 0 is eventually printed. So probably in the foreach block/loop, it thinks it's supposed to restart something rather than exiting back to main.

Now to find out why...


0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement