Advertisement
Guest User

Untitled

a guest
May 19th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.51 KB | None | 0 0
  1. BS(1) General Commands Manual BS(1)
  2.  
  3. NAME
  4. bs - a compiler/interpreter for modest-sized programs
  5.  
  6. SYNOPSIS
  7. bs [ file [ arg ... ] ]
  8.  
  9. DESCRIPTION
  10. Bs is a remote descendant of Basic and Snobol4 with a little C lan-
  11. guage thrown in. Bs is designed for programming tasks where program
  12. development time is as important as the resulting speed of execution.
  13. Formalities of data declaration and file/process manipulation are min-
  14. imized. Line-at-a-time debugging, the trace and dumppile and execute
  15. below). The result of an immediate expression statement is printed.
  16.  
  17. Bs programs are made up of input lines. If the last character on a
  18. line is a \, the line is continued. Bs accepts lines of the following
  19. form:
  20.  
  21. statement
  22. label statement
  23.  
  24. A label is a name (see below) followed by a colon. A label and a
  25. variable can have the same name.
  26.  
  27. A bs statement is either an expression or a keyword followed by zero
  28. or more expressions. Some keywords (clear, compile, !, execute, and
  29. run) are always executed as they are compiled.
  30.  
  31. Statement Syntax:
  32.  
  33. expression
  34. The expression is executed for its side effects (value, assignment
  35. or function call). The details of expressions follow the descrip-
  36. tion of statement types below.
  37.  
  38. break
  39. Break exits from the inner-most for/while loop.
  40.  
  41. clear
  42. Clears the symbol table and compiled statements. Clear is executed
  43. immediately.
  44.  
  45. compile [ expression ]
  46. Succeeding statements are compiled (overrides the immediate execu-
  47. tion default). The optional expression is evaluated and used as a
  48. file name for further input. A clear is associated with this lat-
  49. ter case. Compile is executed immediately.
  50.  
  51. include expression
  52. The expression should evaluate to a file name. The file must con-
  53. tain bs source statements. Include statements may not be nested.
  54.  
  55. continue
  56. Continue transfers to the loop-continuation of the current
  57. for/while loop.
  58.  
  59. dump
  60. The name and current value of every non-local variable is printed.
  61. After an error or interrupt, the number of the last statement and
  62. (possibly) the user-function trace are displayed.
  63.  
  64. exit [ expression ]
  65. Return to system level. The expression is returned as process sta-
  66. tus.
  67.  
  68. execute
  69. Change to immediate execution mode (an interrupt has a similar ef-
  70. fect). This statement does not cause stored statements to execute
  71. (see run below).
  72.  
  73. for name = expression expression statement
  74. for name = expression expression
  75. ...
  76. next
  77.  
  78. for expression , expression , expression statement
  79. for expression , expression , expression
  80. ...
  81. next
  82. The for statement repetitively executes a statement (first form) or
  83. a group of statements (second form) under control of a named vari-
  84. able. The variable takes on the value of the first expression,
  85. then is incremented by one on each loop, not to exceed the value of
  86. the second expression. The third and fourth forms require three
  87. expressions separated by commas. The first of these is the ini-
  88. tialization, the second is the test (true to continue), and the
  89. third is the loop-continuation action (normally an increment).
  90.  
  91. fun f([a, ... ]) [v, ... ]
  92. ...
  93. nuf
  94. Fun defines the function name, arguments, and local variables for a
  95. user-written function. Up to ten arguments and local variables are
  96. allowed. Such names cannot be arrays, nor can they be I/O associ-
  97. ated. Function definitions may not be nested.
  98.  
  99. freturn
  100. A way to signal the failure of a user-written function. See the
  101. interrogation operator (?) below. If interrogation is not present,
  102. freturn merely returns zero. When interrogation is active, freturn
  103. transfers to that expression (possibly by-passing intermediate
  104. function returns).
  105.  
  106. goto name
  107. Control is passed to the internally stored statement with the
  108. matching label.
  109.  
  110. if expression statement
  111. if expression
  112. ...
  113. [ else
  114. ... ]
  115. fi
  116. The statement (first form) or group of statements (second form) is
  117. executed if the expression evaluates to non-zero. The strings 0
  118. and "" (null) evaluate as zero. In the second form, an optional
  119. else allows for a group of statements to be executed when the first
  120. group is not. The only statement permitted on the same line with
  121. an else is an if; only other fi's can be on the same line with a
  122. fi.
  123.  
  124. return [expression]
  125. The expression is evaluated and the result is passed back as the
  126. value of a function call. If no expression is given, zero is re-
  127. turned.
  128.  
  129. onintr label
  130. onintr
  131. The onintr command provides program control of interrupts. In the
  132. first form, control will pass to the label given, just as if a goto
  133. had been executed at the time onintr was executed. The effect of
  134. the statement is cleared after each interrupt. In the second form,
  135. an interrupt will cause bs to terminate.
  136.  
  137. run
  138. The random number generator is reset. Control is passed to the
  139. first internal statement. If the run statement is contained in a
  140. file, it should be the last statement.
  141.  
  142. stop
  143. Execution of internal statements is stopped. Bs reverts to immedi-
  144. ate mode.
  145.  
  146. trace [ expression ]
  147. The trace statement controls function tracing. If the expression
  148. is null (or evaluates to zero), tracing is turned off. Otherwise,
  149. a record of user-function calls/returns will be printed. Each re-
  150. turn decrements the trace expression value.
  151.  
  152. while expression statement
  153. while expression
  154. ...
  155. next
  156. While is similar to for except that only the conditional expression
  157. for loop-continuation is given.
  158.  
  159. ! shell command
  160. An immediate escape to the Shell.
  161.  
  162. # ...
  163. This statement is ignored. It is used to interject commentary in a
  164. program.
  165.  
  166. Expression Syntax:
  167.  
  168. name
  169. A name is used to specify a variable. Names are composed of a let-
  170. ter (upper or lower case) optionally followed by letters and dig-
  171. its. Only the first six characters of a name are significant. Ex-
  172. cept for names declared in fun statements, all names are global to
  173. the program. Names can take on numeric (double float) values,
  174. string values, or can be associated with input/output (see the
  175. built-in function open() below).
  176.  
  177. name ( [expression [ , expression] ... ] )
  178. Functions can be called by a name followed by the arguments in
  179. parentheses separated by commas. Except for built-in functions
  180. (listed below), the name must be defined with a fun statement. Ar-
  181. guments to functions are passed by value.
  182.  
  183. name [ expression [ , expression ] ... ]
  184. Each expression is truncated to an integer and used as a specifier
  185. for the name. The resulting array reference is syntactically iden-
  186. tical to a name. a[1,2] is the same as a[1][2]. The truncated ex-
  187. pressions are restricted to values between 0 and 32767.
  188.  
  189. number
  190. A number is used to represent a constant value. A number is writ-
  191. ten in Fortran style, and contains digits, an optional decimal
  192. point, and possibly a scale factor consisting of an e followed by a
  193. possibly signed exponent.
  194.  
  195. string
  196. Character strings are delimited by " characters. The \ escape
  197. character allows the double quote (\"), new-line (\n), carriage re-
  198. turn (\r), backspace (\b), and tab (\t) characters to appear in a
  199. string. Otherwise, \ stands for itself.
  200.  
  201. ( expression )
  202. Parentheses are used to alter the normal order of evaluation.
  203.  
  204. ( expression, expression [, expression ... ] ) [ expression ]
  205. The bracketed expression is used as a subscript to select a comma-
  206. separated expression from the parenthesized list. List elements
  207. are numbered from the left, starting at zero. The expression:
  208.  
  209. ( False, True )[ a == b ]
  210.  
  211. has the value True if the comparison is true.
  212.  
  213. ? expression
  214. The interrogation operator tests for the success of the expression
  215. rather than its value. At the moment, it is useful for testing
  216. end-of-file (see examples in the Programming Tips section below),
  217. the result of the eval built-in function, and for checking the re-
  218. turn from user-written functions (see freturn). An interrogation
  219. ``trap'' (end-of-file, etc.) causes an immediate transfer to the
  220. most recent interrogation, possibly skipping assignment statements
  221. or intervening function levels.
  222.  
  223. - expression
  224. The result is the negation of the expression.
  225.  
  226. ++ name
  227. Increments the value of the variable (or array reference). The re-
  228. sult is the new value.
  229.  
  230. -- name
  231. Decrements the value of the variable. The result is the new value.
  232.  
  233. ! expression
  234. The logical negation of the expression. Watch out for the shell
  235. escape command.
  236.  
  237. expression operator expression
  238. Common functions of two arguments are abbreviated by the two argu-
  239. ments separated by an operator denoting the function. Except for
  240. the assignment, concatenation, and relational operators, both oper-
  241. ands are converted to numeric form before the function is applied.
  242.  
  243. Binary Operators (in increasing precedence):
  244.  
  245. =
  246. = is the assignment operator. The left operand must be a name or
  247. an array element. The result is the right operand. Assignment
  248. binds right to left, all other operators bind left to right.
  249.  
  250. _
  251. _ (underscore) is the concatenation operator.
  252.  
  253. & |
  254. & (logical and) has result zero if either of its arguments are
  255. zero. It has result one if both of its arguments are non-zero. |
  256. (logical or) has result zero if both of its arguments are zero. It
  257. has result one if either of its arguments is non-zero. Both opera-
  258. tors treat a null string as a zero.
  259.  
  260. < <= > >= == !=
  261. The relational operators (< less than, <= less than or equal, >
  262. greater than, >= greater than or equal, == equal to, != not equal
  263. to) return one if their arguments are in the specified relation.
  264. They return zero otherwise. Relational operators at the same level
  265. extend as follows: a>b>c is the same as a>b & b>c. A string com-
  266. parison is made if both operands are strings.
  267.  
  268. + -
  269. Add and subtract.
  270.  
  271. * / %
  272. Multiply, divide, and remainder.
  273.  
  274. ^
  275. Exponentiation.
  276.  
  277. Built-in Functions:
  278.  
  279. Dealing with arguments
  280.  
  281. arg(i)
  282. is the value of the i-th actual parameter on the current level of
  283. function call. At level zero, arg returns the i-th command-line
  284. argument (arg(0) returns bs).
  285.  
  286. narg()
  287. returns the number of arguments passed. At level zero, the command
  288. argument count is returned.
  289.  
  290. Mathematical
  291.  
  292. abs(x)
  293. is the absolute value of x.
  294.  
  295. atan(x)
  296. is the arctangent of x. Its value is between -<pi>/2 and <pi>/2.
  297.  
  298. ceil(x)
  299. returns the smallest integer not less than x.
  300.  
  301. cos(x)
  302. is the cosine of x (radians).
  303.  
  304. exp(x)
  305. is the exponential function of x.
  306.  
  307. floor(x)
  308. returns the largest integer not greater than x.
  309.  
  310. log(x)
  311. is the natural logarithm of x.
  312.  
  313. rand()
  314. is a uniformly distributed random number between zero and one.
  315.  
  316. sin(x)
  317. is the sine of x (radians).
  318.  
  319. sqrt(x)
  320. is the square root of x.
  321.  
  322. String operations
  323.  
  324. size(s)
  325. the size (length in bytes) of s is returned.
  326.  
  327. format(f, a)
  328. returns the formatted value of a. F is assumed to be a format
  329. specification in the style of printf(3S). Only the %...f, %...e,
  330. and %...s types are safe.
  331.  
  332. index(x, y)
  333. returns the number of the first position in x that any of the char-
  334. acters from y matches. No match yields zero.
  335.  
  336. trans(s, f, t)
  337. Translates characters of the source s from matching characters in f
  338. to a character in the same position in t. Source characters that
  339. do not appear in f are copied to the result. If the string f is
  340. longer than t, source characters that match in the excess portion
  341. of f do not appear in the result.
  342.  
  343. substr(s, start, width)
  344. returns the sub-string of s defined by the starting position and
  345. width.
  346.  
  347. match(string, pattern)
  348. mstring(n)
  349. The pattern is similar to the regular expression syntax of the
  350. ed(1) command. The characters ., [, ], ^ (inside brackets), * and
  351. $ are special. The mstring function returns the n-th (1 <= n <=
  352. 10) substring of the subject that occurred between pairs of the
  353. pattern symbols \( and \) for the most recent call to match. To
  354. succeed, patterns must match the beginning of the string (as if all
  355. patterns began with ^). The function returns the number of charac-
  356. ters matched. For example:
  357.  
  358. match("a123ab123", ".*\([a-z]\)") == 6
  359. mstring(1) == "b"
  360.  
  361. File handling
  362.  
  363. open(name, file, function)
  364. close(name)
  365. The name argument must be a bs variable name (passed as a string).
  366. For the open, the file argument may be 1) a 0 (zero), 1, or 2 rep-
  367. resenting standard input, output, or error output, respectively, 2)
  368. a string representing a file name, or 3) a string beginning with an
  369. ! representing a command to be executed (via sh -c). The function
  370. argument must be either r (read), w (write), W (write without new-
  371. line), or a (append). After a close, the name reverts to being an
  372. ordinary variable. The initial associations are:
  373.  
  374. open("get", 0, "r")
  375. open("put", 1, "w")
  376. open("puterr", 2, "w")
  377.  
  378. Examples are given in the following section.
  379.  
  380. access(s, m)
  381. executes access(2).
  382.  
  383. ftype(s)
  384. returns a single character file type indication: f for regular
  385. file, d for directory, b for block special, or c for character spe-
  386. cial.
  387.  
  388. Odds and ends
  389.  
  390. eval(s)
  391. The string argument is evaluated as a bs expression. The function
  392. is handy for converting numeric strings to numeric internal form.
  393. Eval can also be used as a crude form of indirection, as in:
  394.  
  395. name = "xyz"
  396. eval("++"_ name)
  397.  
  398. which increments the variable xyz. In addition, eval preceded by
  399. the interrogation operator permits the user to control bs error
  400. conditions. For example:
  401.  
  402. ?eval("open(\"X\", \"XXX\", \"r\")")
  403.  
  404. returns the value zero if there is no file named ``XXX'' (instead
  405. of halting the user's program). The following executes a goto to
  406. the label L (if it exists):
  407.  
  408. label="L"
  409. if !(?eval("goto "_ label)) puterr = "no label"
  410.  
  411. plot(request, args)
  412. The plot function produces output on devices recognized by
  413. tplot(1G). The requests are as follows:
  414.  
  415. Call Function
  416.  
  417. plot(0, term) causes further plot output to be
  418. piped into tplot(1G) with an argu-
  419. ment of -Tterm.
  420.  
  421. plot(1) ``erases'' the plotter.
  422.  
  423. plot(2, string) labels the current point with
  424. string.
  425.  
  426. plot(3, x1, y1, x2, y2) draws the line between (x1,y1) and
  427. (x2,y2).
  428.  
  429. plot(4, x, y, r) draws a circle with center (x,y)
  430. and radius r.
  431.  
  432. plot(5, x1, y1, x2, y2, x3, y3)
  433. draws an arc (counterclockwise)
  434. with center (x1,y1) and endpoints
  435. (x2,y2) and (x3,y3).
  436.  
  437. plot(6) is not implemented.
  438.  
  439. plot(7, x, y) makes the current point (x,y).
  440.  
  441. plot(8, x, y) draws a line from the current point
  442. to (x,y).
  443.  
  444. plot(9, x, y) draws a point at (x,y).
  445.  
  446. plot(10, string) sets the line mode to string.
  447.  
  448. plot(11, x1, y1, x2, y2) makes (x1,y1) the lower right cor-
  449. ner of the plotting area and
  450. (x2,y2) the upper left corner of
  451. the plotting area.
  452.  
  453. plot(12, x1, y1, x2, y2) causes subsequent x (y) coordinates
  454. to be multiplied by x1 (y1) and
  455. then added to x2 (y2) before they
  456. are plotted. The initial scaling
  457. is plot(12, 1.0, 1.0, 0.0, 0.0).
  458.  
  459. Some requests do not apply to all plotters. All requests except
  460. zero and twelve are implemented by piping characters to plot(1G).
  461. See plot(5) for more details.
  462.  
  463. last()
  464. in immediate mode, last returns the most recently computed value.
  465.  
  466. PROGRAMMING TIPS
  467. Using bs as a calculator:
  468.  
  469. $ bs
  470. # distance (inches) light travels in a nanosecond
  471. 186000 * 5280 * 12 / 1e9
  472. 11.78496
  473. ...
  474.  
  475. # Compound interest (6% for 5 years on $1,000)
  476. int = .06 / 4
  477. bal = 1000
  478. for i = 1 5*4 bal = bal + bal*int
  479. bal - 1000
  480. 346.855007
  481. ...
  482. exit
  483.  
  484. The outline of a typical bs program:
  485.  
  486. # Initialize things:
  487. var1 = 1
  488. open("read", "infile", "r")
  489. ...
  490. # Compute:
  491. while ?(str = read)
  492. ...
  493. next
  494. # Clean up:
  495. close("read")
  496. ...
  497. # Last statement executed (exit or stop):
  498. exit
  499. # Last input line:
  500. run
  501.  
  502. Input/Output examples:
  503.  
  504. # Copy "oldfile" to "newfile".
  505. open("read", "oldfile", "r")
  506. open("write", "newfile", "w")
  507. ...
  508. while ?(write = read)
  509. ...
  510. # Close "read" and "write":
  511. close("read")
  512. close("write")
  513.  
  514. # Pipe between commands.
  515. open("ls", "!ls *", "r")
  516. open("pr", "!pr -2 -h 'List'", "w")
  517. while ?(pr = ls) ...
  518. ...
  519. # Be sure to close (wait for) these:
  520. close("ls")
  521. close("pr")
  522.  
  523. SEE ALSO
  524. ed(1), plot(1G), sh(1), access(2), printf(3S), stdio(3S), Section 3 of
  525. this volume for further description of the mathematical functions
  526. (pow(3M) is used for exponentiation), plot(5). Bs uses the Standard
  527. Input/Output package.
  528.  
  529. BUGS
  530. There are built-in design limits. Bs source programs are restricted
  531. to fewer than 250 lines and fewer than 250 variables (the name of an
  532. array counts as a variable, as does each dimension and each referenced
  533. element).
  534.  
  535. All names (labels, variables, functions, statement keywords) are in-
  536. ternally truncated to six characters.
  537.  
  538. CB-UNIX 2.1 BS(1)
  539.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement