BS(1) DOMAIN/IX SYS5 BS(1)
NAME
bs - a compiler/interpreter for modest-sized programs
USAGE
bs [ file [ args ] ]
DESCRIPTION
Bs is a remote descendant of BASIC and SNOBOL4, with some C
language added. It is used for programming tasks where pro-
gram development time is as important as the resulting speed
of execution. Formalities of data declaration and
file/process manipulation are minimized. Line-at-a-time
debugging, the trace and dump statements, and useful run-
time error messages all simplify program testing. Further-
more, incomplete programs can be debugged; inner functions
can be tested before outer functions have been written and
vice versa.
If the command line file argument is provided, the file is
used for input before the console is read. By default,
statements read from the file argument are compiled for
later execution. Likewise, statements entered from the con-
sole are normally executed immediately (see compile and exe-
cute below). Unless the final operation is an assignment,
the result of an immediate expression statement is printed.
Bs programs are composed of input lines. If the last char-
acter on a line is a backslash (\), the line is continued.
Bs accepts lines of the following form:
statement
label statement
A bs statement is either an expression, or a keyword fol-
lowed by zero or more expressions. Some keywords (clear,
compile, !, execute, include, ibase, obase, and run) are
always executed as they are compiled. An expression is exe-
cuted for its side effects (value, assignment, or function
call). The details of expressions follow the description of
statement types below.
A label is a name (see EXPRESSION SYNTAX below) followed by
a colon. A label and a variable can have the same name.
STATEMENT TYPES
break Exit from the innermost for/while loop.
clear Clear the symbol table and compiled statements.
This statement type is executed immediately.
Printed 12/4/86 BS-1
BS(1) DOMAIN/IX SYS5 BS(1)
compile [expression]
Compile succeeding statements and override the
immediate execution default. Evaluate the
optional expression and use it as a filename for
further input. Associate a clear with this latter
case. This statement type is executed immedi-
ately.
continue Transfer to the loop-continuation of the current
for/while loop.
dump [name]
Print the name and current value of every nonlocal
variable. Optionally, if a specific name is used,
report only on that named variable. After an
error or interrupt, the number of the last state-
ment and (possibly) the user-function trace are
displayed.
exit [expression]
Go back to system level, returning the expression
as process status.
execute Change to immediate execution mode (an interrupt
has a similar effect). This does not cause stored
statements to execute (see run below).
for name = expression expression statement
for name = expression expression
...
next
for expression , expression , expression statement
for expression , expression , expression
...
next Repetitively execute a statement (first form) or a
group of statements (second form) under control of
the variable specified by name. The variable
takes on the value of the first expression, then
is incremented by one on each loop, not to exceed
the value of the second expression. The third and
fourth forms require three expressions separated
by commas. The first of these is the initializa-
tion, the second is the test (true to continue),
and the third is the loop-continuation action
(normally an increment).
fun f([a, ... ]) [v, ... ]
...
nuf Define the function name, arguments, and local
BS-2 Printed 12/4/86
BS(1) DOMAIN/IX SYS5 BS(1)
variables for a user-written function. Up to ten
arguments and local variables are allowed. Such
names cannot be arrays, nor can they be I/O asso-
ciated. Function definitions may not be nested.
freturn Signal the failure of a user-written function.
See the interrogation operator (?) below. If
interrogation is not present, freturn merely
returns zero. When interrogation is active, fre-
turn transfers to that expression (possibly
bypassing intermediate function returns).
goto name Pass control to the internally-stored statement
with the matching label.
ibase N Set the input base (radix) to N. The only sup-
ported values for N are 8, 10 (the default), and
16. Hexadecimal values 10-15 are entered as a-f.
A leading digit is required (i.e., f0a must be
entered as 0f0a). This statement type is executed
immediately.
if expression statement
if expression
...
[ else
... ]
fi Execute statement (first form) or group of state-
ments (second form) if the expression evaluates to
non-zero. The 0 and `` '' (null) strings evaluate
as zero. In the second form, an optional else
allows for a group of statements to be executed
when the first group is not. The only statement
permitted on the same line with an else is an if.
Only other fi statements can be on the same line
with an fi. The elision of else and if into an
elif is supported. Only a single fi is required
to close an if ... elif ... [ else ... ] sequence.
include expression
Expression must evaluate to a filename. The file
must contain bs source statements. Such state-
ments become part of the program being compiled.
Include statements may not be nested.
obase N Set the output base to N (see ibase above).
onintr label
onintr Provide program control of interrupts. In the
first form, control passes to the label given,
Printed 12/4/86 BS-3
BS(1) DOMAIN/IX SYS5 BS(1)
just as if a goto had been executed at the time
onintr was executed. The effect of the statement
is cleared after each interrupt. In the second
form, an interrupt causes bs to terminate.
return [expression]
Evaluate the expression and pass the result back
as the value of a function call. If no expression
is given, zero is returned.
run Reset the random number generator and pass control
to the first internal statement. If the run
statement is contained in a file, it should be the
last statement.
stop Stop execution of internal statements. Bs reverts
to immediate mode.
trace [expression]
Control function tracing. If expression is null
(or evaluates to zero), tracing is turned off.
Otherwise, a record of user-function calls/returns
will be printed. Each return decrements the trace
expression value.
while expression statement
while
expression
...
next While is similar to for, except that only the con-
ditional expression for loop-continuation is
given.
! Shell command
Make an immediate escape to the shell.
#... Interject commentary in a program.
EXPRESSION SYNTAX
name Specify a variable. Composed of an upper- or lowercase
letter, optionally followed by letters and digits.
Only the first six characters of a name are signifi-
cant. Except for those declared in fun statements, all
names are global to the program. A name can take on
numeric (double float) values, string values, or can be
associated with input/output (see the built-in function
open() below).
name ( [ expression [ ,expression ] ... ] )
Specify the name of a function. This name should be
BS-4 Printed 12/4/86
BS(1) DOMAIN/IX SYS5 BS(1)
followed by the arguments in parentheses, separated by
commas. Except for built-in functions (listed below),
the name must be defined with a fun statement. Argu-
ments to functions are passed by value.
name [ expression [ ,expression ] ... ]
Reference either arrays or tables (see built-in table
functions below). For arrays, each expression is trun-
cated to an integer and used as a specifier for the
name. The resulting array reference is syntactically
identical to a name; thus, a[1,2] is the same as
a[1][2]. The truncated expressions are restricted to
values between 0 and 32767.
number
Represent a constant value. Written in FORTRAN style,
this syntax contains digits and an optional decimal
point. It may also contain a scale factor consisting
of an e followed by perhaps a signed exponent.
string
Delimit character strings by characters in quotes. The
backslash (\) escape character allows the double quote
(\``), newline (\n), carriage return (\r), backspace
(\b), and tab (\t) characters to appear in a string.
Otherwise, a backslash stands for itself.
(expression)
Use parentheses to alter the normal order of evalua-
tion.
(expression ,expression [ ,expression ... ]) [expression]
Use the bracketed expression as a subscript to select a
comma-separated expression from the parenthesized list.
Number list elements from the left, starting at zero.
The following expression has the value of True if the
comparison is true:
( False, True )[ a == b ]
? expression
Test for the success of the expression, rather
than its value. This is useful for testing end-
of-file, the result of the eval built-in function.
Also helpful for checking the return from user-
written functions (see freturn). An interrogation
``trap'' (end-of-file, etc.) causes an immediate
transfer to the most recent interrogation, possi-
bly skipping assignment statements or intervening
function levels.
Printed 12/4/86 BS-5
BS(1) DOMAIN/IX SYS5 BS(1)
-expression
Negate the expression.
++name Increment the value of the variable (or array
reference). The result is the new value.
- -name Decrement the value of the variable. The result
is the new value.
!expression
Logically negate the expression. Make sure to use
the Shell escape.
expression operator expression
Abbreviate the common functions of two arguments
by separating the two arguments by an operator
denoting the function. Except for the assignment,
concatenation, and relational operators, convert
both operands to numeric form before the function
is applied.
BINARY OPERATORS
The binary operators described below are listed by increas-
ing precedence.
= Assignment operator. The left operand must be a name
or an array element. Result is the right operand.
Assignment binds right to left, all other operators
bind left to right.
_ (Underscore); concatenation operator.
& | Logical and (&) has result zero if either of its argu-
ments are zero. This operator has result one if both
of its arguments are non-zero. Logical or (|) has
result zero if both of its arguments are zero, and
result one if either of its arguments is non-zero.
Both operators treat a null string as a zero.
< <= > >= == !=
Relational operators (< less than, <= less than or
equal, > greater than, >= greater than or equal, ==
equal to, != not equal to). Return one if their argu-
ments are in the specified relation; otherwise, return
zero. Those operators at the same level extend as fol-
lows: a>b>c is the same as a>b & b>c. A string com-
parison is made if both operands are strings.
+ -
Add and subtract.
BS-6 Printed 12/4/86
BS(1) DOMAIN/IX SYS5 BS(1)
* / %
Multiply, divide, and remainder.
^ Exponentiate.
BUILT-IN FUNCTIONS FOR DEALING WITH ARGUMENTS
arg(i) Value of the ith actual parameter on the current
level of function call. At level zero, arg
returns the i-th command-line argument (arg(0)
returns bs).
narg() Return the number of arguments passed. At level
zero, it will return the command argument count.
BUILT-IN MATHEMATICAL FUNCTIONS
abs(x) Absolute value of x.
atan(x) Arctangent of x. Its value is between -π/2 and
π/2.
ceil(x) Return the smallest integer not less than x.
cos(x) Cosine of x (radians).
exp(x) Exponential function of x.
floor(x) Return the largest integer not greater than x.
log(x) Natural logarithm of x.
rand() Uniformly distributed random number between zero
and one.
sin(x) Sine of x (radians).
sqrt(x) Square root of x.
BUILT-IN FUNCTIONS FOR STRING OPERATIONS
size(s) Return the size (length in bytes) of s.
format(f,a) Return the formatted value of a. Assume
f to be a format specification in the
style of printf(3S). Only the %...f,
%...e, and %...s types are safe.
index(x,y) Return the number of the first position
in x that any of the characters from y
matches. No match yields zero.
trans(s,f,t) Translate characters of source s from
Printed 12/4/86 BS-7
BS(1) DOMAIN/IX SYS5 BS(1)
matching characters in f to a character
in the same position in t. Copy source
characters that do not appear in f to
the result, unless string f is longer
than t, in which case the source charac-
ters that match in the excess portion of
f will not appear in the result.
substr(s,start,width)
Return the substring of s defined by the
starting position and width.
match(string,pattern)
mstring(n) Pattern is similar to the regular
expression syntax of the ed(1) command.
The characters ., [, ] are special. The
caret placed inside brackets [^], the
asterisk (*), and the dollar sign ($)
are special. The mstring function
returns the n-th (1 <= n <= 10) sub-
string of the subject that occurred
between pairs of the pattern symbols \(
and \) for the most recent call to
match. To succeed, patterns must match
the beginning of the string (as if all
patterns began with ^). The function
returns the number of characters
matched. For example:
match(``a123ab123'', ``.*\([a-z]\)'') ==
6
mstring(1) == ``b''
BUILT-IN FILE HANDLING FUNCTIONS
open(name, file, function)
close(name)
The name argument must be a bs variable name (passed as
a string). For the open, the file argument may be one
of the following: 1) a 0 (zero), 1, or 2 representing
standard input, output or error output, respectively;
2) a string representing a filename; or 3) a string
beginning with an exclamation point (!) representing a
command to be executed (via sh -c). The function argu-
ment must be either r (read), w (write), W (write
without new-line), or a (append). After a close, the
name reverts to being an ordinary variable. The ini-
tial associations are:
open(``get'', 0, ``r'')
open(``put'', 1, ``w'')
BS-8 Printed 12/4/86
BS(1) DOMAIN/IX SYS5 BS(1)
open(``puterr, 2, ``w'')
access(s, m)
Execute access(2).
ftype(s)
Return a single-character file type indication: f for
regular file, p for FIFO (i.e., named pipe), d for
directory, b for block special, or c for character spe-
cial.
TABLES
table(name, size)
A table in bs is an associatively accessed, single-
dimension array. ``Subscripts'' (called keys) are
strings (numbers are converted). The name argument
must be a bs variable name (passed as a string). The
size argument sets the minimum number of elements to be
allocated. Bs prints an error message and stops on
table overflow.
item(name, i)
key()
The item function accesses table elements sequentially
(in normal use, there is no orderly progression of key
values). Where the item function accesses values, the
key function accesses the ``subscript'' of the previous
item call. The name argument should not be quoted.
Since exact table sizes are not defined, use the inter-
rogation operator to detect end-of-table. For example:
table(``t'', 100)
...
# If word contains ``party'', the following expression
adds one
# to the count of that word:
++t[word]
...
# To print out the the key/value pairs:
for i = 0, ?(s = item(t, i)), ++i if key() put =
key()_``:''_s
iskey(name, word)
Test whether the key word exists in the table name and
return one for true or zero for false.
MISCELLANEOUS BUILT-IN FUNCTIONS
eval(s)
Evaluate the string argument s as a bs expression.
This function is handy for converting numeric strings
Printed 12/4/86 BS-9
BS(1) DOMAIN/IX SYS5 BS(1)
to numeric internal form. It can also be used as a
crude form of indirection. For example, the following
increments the variable xyz:
name = ``xyz''
eval(``++''_ name)
In addition, eval preceded by the interrogation opera-
tor allows you to control bs error conditions. For
example, the following returns the value zero if there
is no file named ``XXX'' (instead of halting your pro-
gram):
?eval(``open(\``X\'', \``XXX\'', \``r\'')'')
The following executes a goto to the label L (if it
exists):
label=``L''
if !(?eval(``goto ''_ label)) puterr = ``no label''
last()
In immediate mode, last returns the most recently com-
puted value.
EXAMPLES
This shows how to use bs as a calculator:
# bs
# Distance (inches) light travels in a nanosecond.
186000 * 5280 * 12 / 1e9
311.78496
# Compound interest (6% for 5 years on $1,000).
int = .06 / 4
bal = 1000
for i = 1 5*4 bal = bal + bal*int
bal - 1000
346.855007
exit
#
The outline of a typical bs program is as follows:
# initialize things:
var1 = 1
open(``read'', ``infile'', ``r'')
...
BS-10 Printed 12/4/86
BS(1) DOMAIN/IX SYS5 BS(1)
# compute:
while ?(str = read)
...
next
# clean up:
close(``read'')
...
# last statement executed (exit or stop):
exit
# last input line:
run
Bs uses the Standard Input/Output package. Here are some
input/output examples:
# Copy ``oldfile'' to ``newfile''.
open(``read'', ``oldfile'', ``r'')
open(``write'', ``newfile'', ``w'')
...
while ?(write = read)
...
# close ``read'' and ``write'':
close(``read'')
close(``write'')
# Pipe between commands.
open(``ls'', ``!ls *'', ``r'')
open(``pr'', ``!pr -2 -h 'List''', ``w'')
while ?(pr = ls) ...
...
# be sure to close (wait for) these:
close(``ls'')
close(``pr'')
RELATED INFORMATION
ed(1)
sh(1)
access(2)
printf(3S
Printed 12/4/86 BS-11