Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!iuvax!uxc.cso.uiuc.edu!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: entry at other than main (was want to know) Message-ID: <19173@mimsy.UUCP> Date: 20 Aug 89 18:40:50 GMT References: <8487@bsu-cs.bsu.edu> <2980@solo9.cs.vu.nl> <182@sunquest.UUCP> <10147@csli.Stanford.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 93 In article <10147@csli.Stanford.EDU> poser@csli.Stanford.EDU (Bill Poser) writes: >Chris Torek says that in Cobol subroutines must be declared before >use but that program execution starts at the top. Not COBOL: SNOBOL. Utterly different languages. >Does this mean that you can't use subroutines, Not at all. >or that [SNOBOL] allows declarations, which as non-executable statements >can precede the top-level function, It does not. In SNOBOL, declarations are executable statements. >separate from the actual subroutine definitions? They must be together (although it is permissible to branch out and back in, thus weaving the subroutine and the main program together into one big ugly piece, as I recall). It has been too long since I wrote anything in SNOBOL IV, and I never wrote much (3 programs?), so I do not recall the syntax. By suitable snooping about, however, I have found someone's `snobolhmwk' file---a homework assignment from 1984. (Being at a large university has its advantages :-) .) It includes several example programs: 1 * program 1 2 X = 1 3 DEFINE('P()X') 4 DEFINE('Q()','A') :(G) 5 A X = X + 1 :(RETURN) 6 P X = 5 7 Q() 8 OUTPUT = X :(RETURN) 9 G P() 10 OUTPUT = X 11 END (the line numbers are mine, inserted for reference). As I recall, this creates a global `x' and sets it to one, then defines P() as a procedure which will be found when called by looking for label `P'. (Labels go in the left column.) It also defines Q(), which begins at A (not Q). I am not sure what the X after DEFINE('P() means, but the obvious guess is that it is a local variable. The :(G) means `goto label G' (unconditionally). Thus, by line 3, the interpreter knows about three variables: X (a value) and P and Q (procedures that begin at labels P and A respectively). It jumps to line 9, which calls P(); P() sets its local X to 5 and calls Q(); Q() increments an X (whether the local one or the global, I am not now sure; I suspect it is the most recent one, i.e., the local) and returns; P() prints the current value of X (by assigning to the pseudo-variable `OUTPUT'), and returns; then the main program prints the value of the global X. Here is a more interesting SNOBOL program: 1 X = 'Z' 2 Y = 'X' 3 Z = 'Y' :(R) 4 S Z = 'Y' 5 $X = $Z '0' 6 $Y = $Y '1' 7 OUTPUT = X ',' Y ',' Z 8 EQ(A,0) :S(RETURN) 9 DEFINE('Q(Y,A)Z','S') 10 Q('Y',A-1) :(RETURN) 11 R DEFINE('P(X,A)','S') 12 P('Z',1) 13 END This sets X to "Z", Y to "X", Z to "Y", and branches to R (line 11). This defines procedure P (beginning at S) with arguments X and A. Line 12 then calls P with the (now local) X set to "Z" and the local A set to 1. At line 4, Z (global) is set to "Y"; at line 5, the variable named by X---and X is "Z", so this means the global Z---is set to whatever is named by Z (here the global Y) concatenated with the string "0", so this sets the global Z to "X0". Line 6 sets concatenates the string "1" into whatever variable Y names (here X), so this changes the local X from "Z" to "Z1". It then prints X, Y, and Z, which should produce the output Z1,X,X0 At line 8, if A is equal to zero, we return (the notation :S(RETURN) means `return if the expression on the left did not fail). Since A is 1, not zero, the comparison fails and the return is ignored, and we fall through to line 9, which suddenly declares Q as a procedure which has two arguments (Y and A) and one local variable (Z) and which begins at label S (line 4). We then call Q, passing "Y" and A-1 (0), and when (if) Q returns, we return from P(). Tracing the execution of Q is left to *you*.... -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris