Брайан Керниган - UNIX — универсальная среда программирования
- Название:UNIX — универсальная среда программирования
- Автор:
- Жанр:
- Издательство:Финансы и статистика
- Год:1992
- Город:Москва
- ISBN:5-289-00253-4
- Рейтинг:
- Избранное:Добавить в избранное
-
Отзывы:
-
Ваша оценка:
Брайан Керниган - UNIX — универсальная среда программирования краткое содержание
В книге американских авторов — разработчиков операционной системы UNIX — блестяще решена проблема автоматизации деятельности программиста, системной поддержки его творчества, выходящей за рамки языков программирования. Профессионалам открыт богатый "встроенный" арсенал системы UNIX. Многочисленными примерами иллюстрировано использование языка управления заданиями shell.
Для программистов-пользователей операционной системы UNIX.
UNIX — универсальная среда программирования - читать онлайн бесплатно ознакомительный отрывок
Интервал:
Закладка:
d1.val = (double)(d1.val != 0.0 && d2.val != 0.0);
push(d1);
}
or() {
Datum d1, d2;
d2 = pop();
d1 = pop();
d1.val = (double)(d1.val != 0.0 || d2.val != 0.0);
push(d1);
}
not() {
Datum d;
d = pop();
d.val = (double)(d.val == 0.0);
push(d);
}
power() {
Datum d1, d2;
extern double Pow();
d2 = pop();
d1 = pop();
d1.val = Pow(d1.val, d2.val);
push(d1);
}
assign() {
Datum d1, d2;
d1 = pop();
d2 = pop();
if (d1.sym->type != VAR && d1.sym->type != UNDEF)
execerror("assignment to non-variable", d1.sym->name);
d1.sym->u.val = d2.val;
d1.sym->type = VAR;
push(d2);
}
print() /* pop top value from stack, print it */
{
Datum d;
d = pop();
printf("\t%.8g\n", d.val);
}
prexpr() /* print numeric value */
{
Datum d;
d = pop();
printf("%.8g ", d.val);
}
prstr() /* print string value */
{
printf(%s", (char*)*pc++);
}
varread() /* read into variable */
{
Datum d;
extern FILE *fin;
Symbol *var = (Symbol*)*pc++;
Again:
switch (fscanf(fin, "%lf", &var->u.val)) {
case EOF:
if (moreinput())
goto Again;
d.val = var->u.val = 0.0;
break;
case 0:
execerror("non-number read into", var->name);
break;
default:
d.val = 1.0;
break;
}
var->type = VAR;
push(d);
}
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]))();
}
3.7.4 double
proc double() {
if ($1 > 1) {
double($1/2)
}
print($1)
}
double(1024)
3.7.5 fac
func fac() {
if ($1 <= 0) return 1 else return $1 * fac($1-1)
}
3.7.6 fac1
func fac() if ($1 <= 0) return 1 else return $1 * fac($1-1)
fac(0)
fac(7)
fac(10)
3.7.7 fac2
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
}
3.7.8 fib
proc fib() {
a = 0
b = 1
while (b < $1) {
print b
c = b
b = a+b
a = c
}
print "\n"
}
3.7.9 fib2
{
n=0
a=0
b=1
while(b<10000000){
n=n+1
c=b
b=a+b
a=c
print(b)
}
print(n)
}
3.7.10 fibsum
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)
3.7.11 fibtest
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
}
3.7.12 hoc.h
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();
3.7.13 hoc.ms
.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 )
| expr binop expr
| unop expr
| function ( arguments )
.R
.DE
Numbers are floating point.
The input format is
that recognized by @scanf@(3):
.ix [scanf]
digits, decimal point, digits,
.ix [hoc] manual
.ix assignment expression
.ix multiple assignment
@e@ or @E@, signed exponent.
At least one digit or a decimal point
must be present;
the other components are optional.
.PP
Variable names are formed from a letter
followed by a string of letters and numbers,
@binop@ refers to binary operators such
as addition or logical comparison;
@unop@ refers to the two negation operators,
'!' (logical negation, 'not')
and '\-' (arithmetic negation, sign change).
Table 1 lists the operators.
.TS
center, box;
с s
lfCW l.
\fBTable 1:\fP Operators, in decreasing order of precedence
.sp .5
^ exponentiation (\s-1FORTRAN\s0 **), right associative
! \- (unary) logical and arithmetic negation
* / multiplication, division
+ \- addition, subtraction
> >= relational operators: greater, greater or equal,
< <= less, less or equal,
\&== != equal, not equal (all same precedence)
&& logical AND (both operands always evaluated)
|| logical OR (both operands always evaluated)
\&= assignment, right associative
.ТЕ
.ix table~of [hoc] operators
.PP
Functions, as described later, may be defined by the user.
Function arguments are expressions separated by commas.
There are also a number of built-in functions,
all of which take a single argument,
described in Table 2.
.TS
center, box;
с s
lfCW l.
\fBTable 2:\fP Built-in Functions
.sp .5
abs(x) @| x |@, absolute value of @x@
atan(x) arc tangent of @x@
cos(x) @cos (x)@, cosine of @x@
exp(x) @e sup x@, exponential of @x@
int(x) integer part of @x@, truncated towards zero
log(x) @log (x)@, logarithm base @e@ of @x@
log10(x) @log sub 10 (x)@, logarithm base 10 of @x@
sin(x) @sin (x)@, sine of @x@
sqrt(x) @sqrt x@, @x sup half@
.ТЕ
.ix table~of [hoc] functions
.PP
Logical expressions have value 1.0 (true) and 0.0 (false).
As in C,
any non-zero value is taken to be true.
Интервал:
Закладка: