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;
}
* 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;
}