Читаем UNIX полностью

Inst *code(f) /* install one instruction or operand */

 Inst f;

{

 Inst *oprogp = progp;

 if (progp >= &prog[NPROG])

  execerror("program too big", (char*)0);

 *progp++ = f;

 return oprogp;

}

execute(p)

 Inst *p;

{

 for (pc = p; *pc != STOP && !returning; )

  (*((++pc)[-1]))();

}

<p>3.7.4 <code>double</code></p>

proc double() {

 if ($1 > 1) {

  double($1/2)

 }

 print($1)

}

double(1024)

<p>3.7.5 <code>fac</code></p>

func fac() {

 if ($1 <= 0) return 1 else return $1 * fac($1-1)

}

<p>3.7.6 <code>fac1</code></p>

func fac() if ($1 <= 0) return 1 else return $1 * fac($1-1)

fac(0)

fac(7)

fac(10)

<p>3.7.7 <code>fac2</code></p>

func fac() {

 if ($1 <= 0) {

  return 1

 }

 return $1 * fac($1-1)

}

i=0

while(i<=20){

 print "factorial of ", i, "is ", fac(i), "\n"

 i=i+1

}

<p>3.7.8 <code>fib</code></p>

proc fib() {

 a = 0

 b = 1

 while (b < $1) {

  print b

  c = b

  b = a+b

  a = c

 }

 print "\n"

}

<p>3.7.9 <code>fib2</code></p>

{

 n=0

 a=0

 b=1

 while(b<10000000){

  n=n+1

  c=b

  b=a+b

  a=c

  print(b)

 }

 print(n)

}

<p>3.7.10 <code>fibsum</code></p>

proc fib(){

 a=1

 b=1

 c=2

 d=3

 sum = a+b+c+d

 while(d<$1){

  e=d+c

  print(e)

  a=b

  b=c

  c=d

  d=e

  sum=sum+e

 }

 print(sum)

}

fib(1000)

<p>3.7.11 <code>fibtest</code></p>

proc fib() {

 a = 0

 b = 1

 while (b < $1) {

  c = b

  b = a+b

  a = c

 }

}

i = 1

while (i < 1000) {

 fib(1000)

 i = i + 1

}

<p>3.7.12 <code>hoc.h</code></p>

typedef struct Symbol { /* symbol table entry */

 char *name;

 short type;

 union {

  double val; /* VAR */

  double (*ptr)(); /* BLTIN */

  int (*defn)(); /* FUNCTION, PROCEDURE */

  char *str; /* STRING */

 } u;

 struct Symbol *next; /* to link to another */

} Symbol;

Symbol *install(), *lookup();

typedef union Datum { /* interpreter stack type */

 double val;

 Symbol *sym;

} Datum;

extern Datum pop();

extern eval(), add(), sub(), mul(), div(), negate(), power();

typedef int (*Inst)();

#define STOP (Inst)0

extern Inst *progp, *progbase, prog[], *code();

extern assign(), bltin(), varpush(), constpush(), print(), varread();

extern prexpr(), prstr();

extern gt(), lt(), eq(), ge(), le(), ne(), and(), or(), not();

extern ifcode(), whilecode(), call(), arg(), argassign();

extern funcret(), procret();

<p>3.7.13 <code>hoc.ms</code></p>

.EQ

delim @@

.EN

.TL

Hoc - An Interactive Language For Floating Point Arithmetic

.AU

Brian Kernighan

Rob Pike

.AB

.I Hoc

is a simple programmable interpreter

for floating point expressions.

It has C-style control flow,

function definition and the usual

numerical built-in functions such as cosine and logarithm.

.AE

.NH

Expressions

.PP

.I Hoc

is an expression language,

much like C:

although there are several control-flow statements,

most statements such as assignments

are expressions whose value is disregarded.

For example, the assignment operator

= assigns the value of its right operand

to its left operand, and yields the value,

so multiple assignments work.

The expression grammar is:

.DS

.I

expr: number

 | variable

 | ( expr )

Перейти на страницу:

Похожие книги