home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12992 < prev    next >
Encoding:
Internet Message Format  |  1992-08-27  |  2.2 KB

  1. Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: GOTO, was: Tiny proposal for named loops.
  5. Message-ID: <TMB.92Aug27225227@arolla.idiap.ch>
  6. Date: 28 Aug 92 02:52:27 GMT
  7. References: <714668024@thor> <6800007@tisdec.tis.tandy.com>
  8.     <1992Aug26.130335.26725@hemlock.cray.com>
  9.     <MATT.92Aug26122422@physics16.berkele
  10.     <KERS.92Aug27082828@cdollin.hpl.hp.com>
  11. Sender: news@ai.mit.edu
  12. Reply-To: tmb@idiap.ch
  13. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  14.     Perceptive)
  15. Lines: 45
  16. In-reply-to: kers@hplb.hpl.hp.com's message of 27 Aug 92 07:28:28 GMT
  17.  
  18. In article <KERS.92Aug27082828@cdollin.hpl.hp.com> kers@hplb.hpl.hp.com (Chris Dollin) writes:
  19.  
  20.    In article ... matt@physics16.berkeley.edu (Matt Austern) writes:
  21.  
  22.       The classic situation where gotos are generally believed to be useful
  23.       is writing a finite-state machine.
  24.  
  25.    Use mutually recursive procedures, and lean on your language designers and
  26.    compiler-writers until they're as efficient as the goto's would have been.
  27.  
  28. Actually, since your C implementation is unlikely to support tail
  29. recursion optimization, it's probably a more practical idea to use
  30. what is now commonly known as "continuations" (the casts are necessary
  31. because the C type system can't handle the "correct" declarations):
  32.  
  33.     typedef void (*fpointer_help)();
  34.     typedef fpointer_help (*fpointer)();
  35.  
  36.     void driver(fpointer state) {
  37.         while(state) {
  38.             state = (fpointer)state();
  39.         }
  40.     }
  41.  
  42.     fpointer some_state();
  43.  
  44.     fpointer some_other_state() {
  45.         /* ... do something ... */
  46.         return (fpointer)some_state;
  47.     }
  48.  
  49.     fpointer some_state() {
  50.         /* ... do something ... */
  51.         return (fpointer)some_other_state;
  52.     }
  53.  
  54. But the different ways of expressing state machines in C (table-based,
  55. recursive, using goto, and using continuations) have different
  56. applications and different tradeoffs. In a Lisp interpreter that I
  57. wrote a long time ago, I was using continuations for the main
  58. interpreter (it needed lots of states, with source code scattered
  59. across lots of source files), and "goto" for the state machine inside
  60. the compacting, single-space garbage collector (for efficiency).
  61.  
  62.                 Thomas.
  63.