Читаем UNIX — универсальная среда программирования полностью

.IT $3

refers to the third (1-indexed) argument.

They are passed by value and within functions

are semantically equivalent to variables.

It is an error to refer to an argument numbered greater than the

number of arguments passed to the routine. The error checking

is done dynamically, however, so a routine may have variable numbers

of arguments if initial arguments affect the number of arguments

to be referenced (as in C's @printf@).

.PP

Functions and procedures may recurse, but the stack has limited depth

(about a hundred calls). The following shows a

.I

hoc

definition of Ackermann's function:

.ix Ackermann's~function

.DS

.ft CW

.ix [ack]~function

.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)

 29

.S "ack(3, 3)

 61

.S "ack(3, 4)

hoc: stack too deep near line 8

\...

.ft

.DE

.bp

.NH

Examples

.PP

Stirling's~formula:

.ix Stirling's~formula

.EQ

n! ~\(ap~ sqrt {2n pi} (n/e) sup n (1+ 1 over 12n )

.EN

.DS

.ft CW

.S $ hoc

.S "func stirl {

.S " return sqrt(2*$1*PI) * ($1/E)"$1*(1 + 1/(12*$1)) .S "}

.S "stirl(10)

 3628684.7

.S stirl(20)

 2.4328818e+18

.ft R

.DE

.PP

Factorial function, @n!@:

.ix [fac]~function

.DS

. S "func fac if ($1 = 0) return 1 else return $1 * fac($1-1)

.ft R

.DE

.PP

Ratio of factorial to Stirling approximation:

.DS

.S "i = 9

.S "while ((i = i+1) = 20) {

.S \ \ \ \ \ \ \ \ print\ i,\ "\ \ ",\ fac(i)/stirl(i),\ "\en"

.S "} .ft CW

10 1.0000318

11 1.0000265

12 1.0000224

13 1.0000192

14 1.0000166

15 1.0000146

16 1.0000128

17 1.0000114

18 1.0000102

19 1.0000092

20 1.0000083

.ft

.DE

3.7.14 hoc.y

%{

#include "hoc.h"

#define code2(c1,c2) code(c1); code(c2)

#define code3(c1,c2,c3) code(c1); code(c2); code(c3)

%}

%union {

 Symbol *sym; /* symbol table pointer */

 Inst *inst; /* machine instruction */

 int narg; /* number of arguments */

}

%token sym NUMBER STRING PRINT VAR BLTIN UNDEF WHILE IF ELSE

%token sym FUNCTION PROCEDURE RETURN FUNC PROC READ

%token narg ARG

%type inst expr stmt asgn prlist stmtlist

%type inst cond while if begin end

%type sym procname

%type narg arglist

%right '='

%left OR

%left AND

%left GT GE LT LE EQ NE

%left '+' '-' %left '/'

%left UNARYMINUS NOT

%right '^'

%%

list: /* nothing */

 | list '\n'

 | list defn '\n'

 | list asgn '\n' { code2(pop, STOP); return 1; }

 | list stmt '\n' { code(STOP); return 1; }

 | list expr '\n' { code2(print, STOP); return 1; }

 | list error '\n' { yyerrok; }

 ;

asgn: VAR '=' expr { code3(varpush,(Inst)$1,assign); $$=$3; }

 | ARG '=' expr

 { defnonly("$"); code2(argassign,(Inst)$1); $$=$3;}

 ;

stmt: expr { code(pop); }

Перейти на страницу:
Нет соединения с сервером, попробуйте зайти чуть позже