[net.lang.c] a basic like gosub in C

josh@crunch.uucp (Josh Siegel) (02/23/86)

In basic there is a GOSUB routine that will push the current
 position onto the stack and jump to another line.  Then, upon
 hitting a return will return to that place.  I wish to write
 something like that in C that will be movable between compilers.

A sample of something like it is below:

5  rem Basic
10 print "hi"
20 gosub 50
30 print "bye"
35 gosub 50
40 end
50 print "hello"
55 print "who are you?"
60 return

/* C code that does work */
main () {
    (void)gosub (0);
}
gosub (line)
int     line;
{
    switch (line) {
	default: 
	    puts ("hi");
	    (void)gosub (50);
	    puts ("bye");
	    (void)gosub (55);
	    exit (0);
	case 50: 
	    puts ("hello");
	case 55: 
	    puts ("who are you");
	    return (0);
    }
}


Can anybody think of a better way to do this? I want it for a
  Basic->C translator (Please, I am writting it for fun... No abuse...Please?).


	thanks much,

Send me mail . . . I   L O V E  mail

          Josh Siegel

        {convex,ucbvax,gatech,csu-cs,anl-mcs,lanl-a}!unmvax
                                                           \
                                                           !crunch!josh
                                                           /
{cmcl2,csu-cs,dirac,dspo,gel,ias,ihnp4,mtu,nmsu}!lanl!quark

holloway@drivax.UUCP (Bruce Holloway) (02/26/86)

In article <202@crunch.uucp> josh@crunch.uucp (Josh Siegel) writes:
>In basic there is a GOSUB routine that will push the current
> position onto the stack and jump to another line.  Then, upon
> hitting a return will return to that place.  I wish to write
> something like that in C that will be movable between compilers.

(Sorry I'm not sending MAIL... but it doesn't seem to work from "rn" under
the C shell....)

You should use the setjmp() and longjmp() routines.

Call setjmp with a implementation specific environment buffer. When a
longjmp is called with the same buffer, it returns from the setjmp with
a return code. setjmp returns zero in a normal routine, or the longjmp 
parameter if that's wwhere it came from. Hmmm.

	jmp_buf env;

	func(){
	    :
	    :
	    if(setjmp(env)){

/* Code executed when returning from LONGJMP */

		}
	    :
	    :
	    longjmp(env,ret_code);
	    }


-- 

+----------------------------------------------------------------------------+
|Whatever I write are not the opinions or policies of Digital Research, Inc.,|
|and probably won't be in the foreseeable future.                            |
+----------------------------------------------------------------------------+

Bruce Holloway

....!ucbvax!hplabs!amdahl!drivax!holloway
(I'm not THAT Bruce Holloway, I'm the other one.)