0NOTE

Thu Jul 21 17:13:29 MST 2005


pulled over hoc sources from brian kernighan's webpage

	http://www.cs.bell-labs.com/who/bwk/index.html

compilation under macosx
	see Makefile
	immense number of warnings
		ok = expected for old k&r code
	runs fine = passes "tests" in directory


some changes:
	diff file follows explanatory comments

----------------------------------------------------------------------

hoc.ms
some ms macros not defined in groff_ms (need tmac.kernighan?)
	deleted those that were not transparently ignored
a few cosmetic changes
	removed a page break
	kept intro para and code together
	...

nroffed hoc.ms, under groff/macosx:

	groffer --tty hoc.ms  > hoc.ms.txt

groffed hoc.ms, under groff/macosx:

	groff -e -t -ms hoc.ms  >hoc.ms.ps


----------------------------------------------------------------------

code.c hoc.h hoc.y

code changes are commented in code.c
no comments in hoc.*

following explains

The function popstack(), declared int but really void, was added to
pop the stack without a return value of type Datum.  popstack(),
and previously pop(), is put onto the machine stack for execution by
use of code().  Both code and the execution loop assume a return value
of type int.  pop() returns type Datum.  sizeof(Datum) > sizeof(int).
At least on some hardware.

The function call by use of the pointer pc with pre/postfix incrementation
of pc is different comparing a MIPS C compiler with SunOS 4.1 and
AT&T SysVr2, at least.  MIPS increments after return, SunOS before -- a
problem, obviously, if pc is used in the function.

[another more elegant single solution to above two bugs - combine
type check with change in prefix incrementation/function call --
google comp.lang.c.]

div() duplicates a library name in macosx C;
changed div() -> dvi().

The remaining change, a cast to (Inst *), was merely for lint cleanliness.
code from hoc.jar, except replaced math.c and init.c with versions
from hoc.uunet


----------------------------------------------------------------------

diff -ibBw ./code.c /Users/jar/Downloads/kernighan-code Folder/hoc6/code.c
42,55d41
< /* popstack()
<  * pop the stack with a return value of type int;
<  * in some contexts, the stack is pop'd with return type int required:
<  * 	if pop with pop(), fail if sizeof(Datum) != sizeof(int).
<  * (note - used old k&r style to be consistent with hoc6 code:
<  * 	implicit int for function of type void...)
<  */
< popstack()
< {
< 	if (stackp == stack)
< 		execerror("stack underflow", (char *)0);
< 	--stackp;
< }
< 
127c113
< 	execute((Inst *)sp->u.defn);	/* type added for lint clean*/
---
> 	execute(sp->u.defn);
230,231c216
< /* div() *//* name conflict div for macosx C */
< dvi()
---
> div()
408,415c393,394
< 	Inst *pfunc;
< 
< 	for (pc = p; *pc != STOP && !returning; ) {
< 		pfunc = pc;
< 		pc++;
< 		(*(*pfunc))();
< /* 		(*((++pc)[-1]))(); *//* order ++ & fn-call compiler dpndnt */
< 	}
---
> 	for (pc = p; *pc != STOP && !returning; )
> 		(*((++pc)[-1]))();


diff -ibBw ./hoc.h /Users/jar/Downloads/kernighan-code Folder/hoc6/hoc.h
20,23c19
< 
< /* popstack() and div() -> dvi() - see comments in code.c */
< extern	popstack();
< extern 	eval(), add(), sub(), mul(), dvi(), negate(), power();
---
> extern	eval(), add(), sub(), mul(), div(), negate(), power();


diff -ibBw ./hoc.y /Users/jar/Downloads/kernighan-code Folder/hoc6/hoc.y
30c30
< 	| list asgn '\n'  { code2(popstack, STOP); return 1; }
---
> 	| list asgn '\n'  { code2(pop, STOP); return 1; }
39c39
< stmt:	  expr	{ code(popstack); }
---
> stmt:	  expr	{ code(pop); }
84c84
< 	| expr '/' expr	{ code(dvi); }
---
> 	| expr '/' expr	{ code(div); }


----------------------------------------------------------------------
----------------------------------------------------------------------


diff -ibBw ./hoc.ms /Users/jar/Downloads/kernighan-code Folder/hoc6/hoc.ms
255d254
< .KS
265,271c264,270
< $ hoc
< func ack() {
<         if ($1 == 0) return $2+1
<         if ($2 == 0) return ack($1-1, 1)
<         return ack($1-1, ack($1, $2-1))
< }
< ack(3, 2)
---
> .S $ "hoc
> .S "func ack() {
> .S "        if ($1 == 0) return $2+1
> .S "        if ($2 == 0) return ack($1-1, 1)
> .S "        return ack($1-1, ack($1, $2-1))
> .S "}
> .S "ack(3, 2)
273c272
< ack(3, 3)
---
> .S "ack(3, 3)
275c274
< ack(3, 4)
---
> .S "ack(3, 4)
280,281c279
< .KE
< .\" .bp
---
> .bp
292,296c290,294
< $ hoc
< func stirl() {
<     return sqrt(2*$1*PI) * ($1/E)^$1*(1 + 1/(12*$1))
< }
< stirl(10)
---
> .S $ hoc
> .S "func stirl() {
> .S "    return sqrt(2*$1*PI) * ($1/E)^$1*(1 + 1/(12*$1))
> .S "}
> .S "stirl(10)
298c296
< stirl(20)
---
> .S stirl(20)
306,307c304
< .ft CW
< func fac() if ($1 <= 0) return 1 else return $1 * fac($1-1)
---
> .S "func fac() if ($1 <= 0) return 1 else return $1 * fac($1-1)
312a310,313
> .S "i = 9
> .S "while ((i = i+1) <= 20) {
> .S \ \ \ \ \ \ \ \ print\ i,\ "\ \ ",\ fac(i)/stirl(i),\ "\en"
> .S "}
314,317d314
< i = 9
< while ((i = i+1) <= 20) {
< \ \ \ \ \ \ \ \ print\ i,\ "\ \ ",\ fac(i)/stirl(i),\ "\en"
< }
