home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: GOTO, was: Tiny proposal for named loops.
- Message-ID: <TMB.92Aug27225227@arolla.idiap.ch>
- Date: 28 Aug 92 02:52:27 GMT
- References: <714668024@thor> <6800007@tisdec.tis.tandy.com>
- <1992Aug26.130335.26725@hemlock.cray.com>
- <MATT.92Aug26122422@physics16.berkele
- <KERS.92Aug27082828@cdollin.hpl.hp.com>
- Sender: news@ai.mit.edu
- Reply-To: tmb@idiap.ch
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 45
- In-reply-to: kers@hplb.hpl.hp.com's message of 27 Aug 92 07:28:28 GMT
-
- In article <KERS.92Aug27082828@cdollin.hpl.hp.com> kers@hplb.hpl.hp.com (Chris Dollin) writes:
-
- In article ... matt@physics16.berkeley.edu (Matt Austern) writes:
-
- The classic situation where gotos are generally believed to be useful
- is writing a finite-state machine.
-
- Use mutually recursive procedures, and lean on your language designers and
- compiler-writers until they're as efficient as the goto's would have been.
-
- Actually, since your C implementation is unlikely to support tail
- recursion optimization, it's probably a more practical idea to use
- what is now commonly known as "continuations" (the casts are necessary
- because the C type system can't handle the "correct" declarations):
-
- typedef void (*fpointer_help)();
- typedef fpointer_help (*fpointer)();
-
- void driver(fpointer state) {
- while(state) {
- state = (fpointer)state();
- }
- }
-
- fpointer some_state();
-
- fpointer some_other_state() {
- /* ... do something ... */
- return (fpointer)some_state;
- }
-
- fpointer some_state() {
- /* ... do something ... */
- return (fpointer)some_other_state;
- }
-
- But the different ways of expressing state machines in C (table-based,
- recursive, using goto, and using continuations) have different
- applications and different tradeoffs. In a Lisp interpreter that I
- wrote a long time ago, I was using continuations for the main
- interpreter (it needed lots of states, with source code scattered
- across lots of source files), and "goto" for the state machine inside
- the compacting, single-space garbage collector (for efficiency).
-
- Thomas.
-