flowto (Just for fun)

S

Shao Miller

/****
* flowto.c
* 2011, Shao Miller
* Just for fun. :)
*/
#if 0 /* Definition */

"flowto": A function-like macro resembling
a jump-statement.

Syntax

1 flowto ( identifier ) statement

Constraints

2 The identifier in a flowto statement shall name a
label located somewhere in the enclosing function. A
flowto statement shall not jump from outside the scope
of an identifier having a variably modified type to
inside the scope of that identifier.

Semantics

3 A flowto statement causes a jump to the statement
prefixed by the named label in the enclosing
function after executing the specified statement.

4 If the statement contains a break statement that is
not within an enclosing iteration statement or
switch statement and that break statement is reached,
the named label will not be jumped to and any
remainder of the flowto statement will not be
executed.

5 If the statement contains a continue statement that
is not within an enclosing iteration statement and
that continue statement is reached, the named label
will be jumped to immediately.

6 EXAMPLE 1 It is sometimes convenient to jump out of
an if statement after some conditional action has
been taken.
/* ... */
if (failure_condition)
flowto (cleanup_label)
/* Report problem */
puts("Error! Cleaning up...");
else
/* Report success */
puts("Success");
/* Continue with program */
/* ... */
return;
cleanup_label:
/* Clean up due to failure */
/* ... */
return;

7 EXAMPLE 2 A break statement within a flowto
statement can be used to suppress the jump.
/* ... */
flowto (carry_on) {
if (failure_condition)
/* Do not carry on */
break;
puts("Carrying on...");
}
/* Report problem */
puts("Error! Cleaning up...");
/* Clean up due to failure */
/* ... */
return;
carry_on:
/* Continue with program */
/* ... */

#endif /* Definition */

#define flowto(label) \
switch (1) \
while (1) \
if (1) { \
goto label; \
} else default:


/**** Test program */
#include <stdio.h>

int main(void) {
if (1)
flowto (test1)
puts("Jumping to test1...");
else
puts("Failure!");

if (1)
test2:
flowto (test3) {
puts("Reached test2.");
puts("Jumping to test3...");
}
else
puts("Failure!");

if (1)
flowto (test2)
test1:
while (1) {
puts("Reached test1.");
puts("Jumping to test2...");
break;
}
else
puts("Failure!");

test3:
puts("Reached test3.");
flowto (test1)
if (1)
break;

return 0;
}
 
S

Shao Miller

i'm not too much smart.. nor to know english very well
but why that would be the advantage of above to:
1 statement; goto indetifier;

It's one statement instead of two.

if (foo)
bar();
else
baz();

if (foo)
{ bar(); goto somewhere; }
else
baz();

if (foo)
flowto(somewhere) bar();
else
baz();
 
K

Kenny McCormack

It's one statement instead of two.

if (foo)
bar();
else
baz();

if (foo)
{ bar(); goto somewhere; }
else
baz();

if (foo)
flowto(somewhere) bar();
else
baz();

Of course, it is just a matter of time until one of the residents pedants
gets all hyper about how you are just being cute trying to avoid a set of
braces - and how they, in their innnate superiority, always use braces
regardless of how many statements are involved (i.e., in the case where that
number is 1).

--
Is God willing to prevent evil, but not able? Then he is not omnipotent.
Is he able, but not willing? Then he is malevolent.
Is he both able and willing? Then whence cometh evil?
Is he neither able nor willing? Then why call him God?
~ Epicurus
 
S

Shao Miller

Of course, it is just a matter of time until one of the residents pedants
gets all hyper about how you are just being cute trying to avoid a set of
braces - and how they, in their innnate superiority, always use braces
regardless of how many statements are involved (i.e., in the case where that
number is 1).

That's possible. Well, part of the "fun" is for a "minimal diff" when
working with someone else's code and that code doesn't use braces. If
the original goes:

if (foo)
bar();
else
baz();

and you wish to inject a jump after the call to 'bar', you can replace
their line:

if (foo)
- bar();
+ { bar(); goto somewhere; }
else
baz();

or add two lines:

if (foo)
+ {
bar();
+ goto somewhere; }
else
baz();

or add one line, assuming the 'flowto' macro is available (which might
not be the case without an additional '#include' line):

if (foo)
+ flowto(somewhere)
bar();
else
baz();

Heh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top