for loop special case broken

Posted By: Uhrwerk

for loop special case broken - 02/07/13 23:46

Hello Mr. Lotter,

there is an issue with the following snippet.
Code:
#include <acknex.h>

void main()
{
	for (; ; wait(1));
}


the compiler does not reject it (that's correct, it's valid c code) but as soon as the engine starts it gets stuck in an endless loop. The actual engine window never pops up, just the white starter window.
Posted By: Rackscha

Re: for loop special case broken - 02/08/13 12:11

Valid code is Valid code.
The compiler checks for valid code, not functionality.

the above is like:
Code:
while(true){wait(1);}

Posted By: MasterQ32

Re: for loop special case broken - 02/08/13 13:02

It should be. But the engine freezes what leads to the conclusion that it isn't exactly the same thing.
I ran into this problem some time ago that the for-loop doesn't work like expected if you use wait.
My thoughts are that the compiler has a larger difference between a while-loop and a for-loop in assembler and so the wait(1) cannot return to the engine scheduler or something like that----
Posted By: jcl

Re: for loop special case broken - 02/08/13 13:19

The wait(1) is called, but does not delay. This is indeed a special case, as the wait() function is not fully C compliant.

It makes no difference if you try that in a while or for loop. wait() always belongs in the loop body. But we'll mention that in the manual.
Posted By: Uhrwerk

Re: for loop special case broken - 02/08/13 13:23

Why not fixing it?
Posted By: jcl

Re: for loop special case broken - 02/08/13 13:29

Because documenting it is easier laugh.
Posted By: Uhrwerk

Re: for loop special case broken - 02/08/13 13:33

It's not easier. It's easier for you. For everybody else it is much more complicated.
Posted By: jcl

Re: for loop special case broken - 02/08/13 14:01

It's only much more complicated for you. Not for everybody else.

I think our users mostly prefer implementing more new features over implementing more bizarre ways to call wait().
Posted By: Uhrwerk

Re: for loop special case broken - 02/08/13 14:30

No, this is not about me. I know this bug now. I'll never do it again and even if I did I'd immediately know what's wrong with my code. This is for example about MasterQ32:
Quote:
I ran into this problem some time ago that the for-loop doesn't work like expected if you use wait.

I also don't think this the usage of a for loop is bizarre. While the sample I originally posted is clearly pointless there actually are applications where this kind of a for loop is very elegant:
Code:
for (my-frame = 0; ; wait(1))
    my->frame += time_step;

You're a very experienced software engineer, probably much more experienced than me. But I still don't think that you're doing yourself or anybody else a favor when mentioning bugs in the manual instead of fixing them.

I think your users do not prefer new features over correct software. If the new WED gets delayed one working day because of this bug being fixed most users will gladly accept this.

Once again: Your software, your decision. I hold you in high regard for caring about this great piece of software for such a long time. It's just that I simply don't get your point here.
Posted By: jcl

Re: for loop special case broken - 02/08/13 15:00

Fair enough. Let me explain our point of view:

wait() is not a normal C function. So there are many special cases where it can not be used like such a function. Some of those special cases can be shielded from the user with little effort, others with a lot effort, others not at all. For instance, there is no solution to call wait() from an external DLL.

No user in the last 8 years had the idea to put a wait() call inside the control statement of a loop. Nor had we. You just don't program in this way when you want clean code, even if it's valid C syntax. So I consider this a rather bizarre case with little meaning for practical programming.

"Fixing" it would require to re-write the for and while loop handling in the compiler for considering the special case of a wait() call. This would not only come at a cost of less new features, any special case handling like this also makes a program less stable and harder to maintain.

In fact we would put a lot of effort in ending up with a worse software than before.

I hope you understand our decision in such cases.
Posted By: Uhrwerk

Re: for loop special case broken - 02/08/13 16:19

Thank you very much for taking the time to explain. It's highly appreciated. I'm still interested in the technical background. If answering my questions takes too much time feel free to ignore them.

Originally Posted By: jcl
"Fixing" it would require to re-write the for and while loop handling in the compiler...

I still don't understand the way you implemented for loops and why a "wait" is a special case here. For loops are nothing else but syntactical sugar. Any for loop like
Code:
for (expr1; expr1; expr3)
{
   body;
}

can easily replaced by
Code:
expr1;
while (expr2)
{
   body;
   expr3;
}

The only pitfall here is that if you have a "continue;" in the body you have to replace it with a "expr3; continue;". But that's it. Once you got the while loop you get the for loop almost for free. It shouldn't even be necessary to touch the actual compiler code for this.
Originally Posted By: jcl
You just don't program in this way when you want clean code
What's unclean about it? I wonder why noone had the idea for a macro like
Code:
#define each_frame for (;; wait(1))

This would effectively prevent you from programming endless loops because you forgot a wait. E.g.
Code:
each_frame
{
   draw_quad(...);
}


Originally Posted By: jcl
In fact we would put a lot of effort in ending up with a worse software than before.
I have to keep this in mind for my work.
Boss: There is a bug in your code. Fix it!
Me: No, introducing special cases will only reduce maintainability and make the software worse.
grin
Posted By: MasterQ32

Re: for loop special case broken - 02/08/13 17:53

wait is not a normal function (i think it hasn't even a real function structure)
so handling this is not so easy

and just change your code to this:
Code:
for (my-frame = 0; ; my->frame += time_step;)
    wait(1);

Posted By: Uhrwerk

Re: for loop special case broken - 02/08/13 18:53

I'm well aware of the fact, that wait is a "special" function and I'm even able to rewrite a simple for loop. I'm even not insisting on fixing this. JCL will know if a fix makes sense or does not.

The thing is that I want to understand what's going on - out of curiosity.
© 2024 lite-C Forums