Named Blocks (Was Re: Oh noooooo!!)

Peter Rowell peter at thirdi.UUCP
Thu Sep 7 10:07:21 AEST 1989


In article <7598 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
...
>I just now produced one [a benign goto] ...
...
>  while(rule = (Rule*)Queue_iter_next(&rule_iter)) {
>    ...
>    while(rsym = (Symbol*)Queue_iter_next(&rsym_iter)) {
>    ...
>	switch (derives(rsym)) {
>	case derives_nothing:
>	  goto next_rule;
>	}
> ...
>    }
> ...
>  next_rule: continue;
>  }

I agree with David, since in C there really aren't any beautiful ways
to do the above.

One thing I would have loved to have seen introduced by ANSI would have
been "named blocks", but I am sure that parsing them is a bitch.
Borrowing heavily from SAIL, a named block could be defined as one that
has a string constant immediately following the opening curly brace.
The matching closing curly could have an optional string which, if
present, must match exactly the one attached to the opening brace.
This second part gives "begin/end" checking over arbitrarily nasty
nestings (like in the big switches often found in command interpreters,
etc.).  The name is available as an optional argument to "break" and
"continue".  If a name is missing, they (break/contiue) act as they do now.
If included, they allow multi-level break'ing and continue'ing.

Given this "improvement", the above might have been written:

    while(rule = (Rule*)Queue_iter_next(&rule_iter)) { "outer loop"
	while(rsym = (Symbol*)Queue_iter_next(&rsym_iter)) { "inner loop"
	    switch (derives(rsym)) { "switch 1"
		case derives_nothing:
		    continue "outer loop"; /* <<== next iteration of outer */
		case derives_something:
		    /* ... */
		    break;	/* a normal break */
		default:
		    break "inner loop";	/* <<== falls out the bottom of inner */
	    } "switch 1"
	} "inner loop"
	/* random code */
    } "outer loop"

which I believe is much more readbale.  Not all of the names were necessary,
but I was having fun.  Sigh, maybe we can do this in the next language.....

----------------------------------------------------------------------
Peter Rowell				peter at thirdi.UUCP
Third Eye Software, Inc.		(415) 321-0967
Menlo Park, CA  94025   	"There are already enough names." Lao Tze



More information about the Comp.lang.c mailing list