Шрифт:
} 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,