[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

flowto (Just for fun

Shao Miller

6/30/2011 2:30:00 PM

/****
* 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;
}
5 Answers

io_x

7/1/2011 8:20:00 AM

0


"Shao Miller" <sha0.miller@gmail.com> ha scritto nel messaggio
news:iui19t$lkn$1@dont-email.me...
> /****
> * 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

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;

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



Shao Miller

7/1/2011 3:46:00 PM

0

On 7/1/2011 3:19 AM, io_x wrote:
> "Shao Miller"<sha0.miller@gmail.com> ha scritto nel messaggio
> news:iui19t$lkn$1@dont-email.me...
>> /****
>> * 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
>
> 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();

gazelle

7/1/2011 6:25:00 PM

0

In article <iukq38$sul$1@dont-email.me>,
Shao Miller <sha0.miller@gmail.com> wrote:
>On 7/1/2011 3:19 AM, io_x wrote:
>> "Shao Miller"<sha0.miller@gmail.com> ha scritto nel messaggio
>> news:iui19t$lkn$1@dont-email.me...
>>> /****
>>> * 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
>>
>> 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();

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

Shao Miller

7/1/2011 7:55:00 PM

0

On 7/1/2011 1:25 PM, Kenny McCormack wrote:
>>
>> 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).
>

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.

io_x

7/2/2011 6:58:00 AM

0

"Shao Miller" <sha0.miller@gmail.com> ha scritto nel messaggio
news:iul55q$a22$1@dont-email.me...
i prefer- and all goes well- C macro if without arg
something as
#define nome1 0x123
#define u32 unsigned int
etc
possibily not use the ones with arg for some good reason
e.g. debugging