C. Built-in Functions Refal-5 includes a library of standard built-in functions. These include I/O and arithmetic operations, various system jobs which cannot be defined in Refal, and some simple procedures which are built into the system for greater efficiency. If need be, a function name used for a built-in function can be reassigned to denote a function defined in Refal. The built-in function will then be inaccessible. 1. I/O Functions <Card>
Functions that work with files require a file descriptor as an argument. A file descriptor is a macrodigit in the range 1-19; in some operations the descriptor 0 is allowed and refers to the terminal.
<Putout s.D e.Expr> returns empty (like Prout ). In all other respects Putout is identical to Put . 2. Arithmetic Functions Representationofnumbers. Whole numbers are represented as sequences of macrodigits using the base 232 . Negative whole numbers are preceded by the character '-' . Positive numbers may be preceded by '+' . Arithmetic functions return whole numbers in the standard form: '-' and a sequence of macrodigits for a negative number; no '+' for 0 or a positive number. Real numbers (of either sign) are represented as single symbols and take a 32-bit word. (For the syntax of real numbers see Reference Section B.) The basic format of binary arithmetic operations is:
If both arguments of an arithmetic function are whole numbers, the result is also a whole number; otherwise it is a real number.
The following functions ar-function are implemented: Sub or - subtracts e.N2 from e.N1 and returns the difference. Mul or * returns the product of the operands. Div or / if at least one argument is real, returns the real quotient. If both are whole, Divreturns the whole quotient of e.N1 and e.N2 ; the remainder is ignored. With this and the other two division functions, if e.N2 is 0, an error occurs. Divmod expects whole arguments and returns
Mod expects whole arguments and returns the remainder of the division of e.N1 by e.N2 . Compare compares the two numbers and returns '-'if e.N1 is less than e.N2 , '+' if it is greater, and '0' if they are equal. <Trunc eN> where eN is a real number, returns the truncated whole number. <Real eN> where eN is a whole number, returns the equal real number. <Realfun (e.Function) sN> or <Realfun (e.Function) s.N1 s.N2> returns the value of the function e.Function of one or two arguments. e.Function must be a string of characters naming a function available in the language C. For instance, <Realfun ('log') sN> returns the logarithm of sN . (For the list of available functions, see information on the system diskette.) 3. Stack Operations <Br e.Name'=' e.Expr> <Dg e.Name> digs the expression buried under the name e.Name , i.e., returns the last expression buried under this name and removes it from the stack. If there is no expression buried under e.Name , Dg returns the empty expression. <Cp e.Name> works as Dg but does not remove the expression from the stack. <Rp e.Name'=' e.Expr> replaces the expression buried under the name e.Name by e.Expr . <Dgall> digs out the whole stack. The stack is a string of terms of the form
4. Symbol and String Manipulation <Type e.Expr>
<Numb e.Digit-string> returns the macrodigit represented by e.Digit-string . <Symb s.Macrodigit> is the inverse of Numb . It returns the string of decimal digits representing s.Macrodigit . <Implode e.Expr> takes the initial alphanumeric characters of e.Expr and creates an identifier (symbolic name) from them. The initial string in e.Expr must begin with a letter and terminate with a non-alphanumeric character, parenthesis, or the end of the expression. The string must not exceed 15 characters. Underscore and dash are also permitted. Implode returns the identifier followed by the unimploded portion of e.Expr . If the first character is not a letter, Implodereturns the macrodigit 0 followed by the argument. <Explode s.Identifier> returns the string of character-symbols which make up s.Idenitifier . <Chr e.Expr> replaces every macrodigit in e.Expr by the character-symbol with the same ASCII code modulo 256. <Ord e.Expr> is the inverse of Char . It returns the expression in which all characters are replaced by macrodigits equal to their ASCII codes. <First s.N e.Expr> where s.N is a macrodigit, breaks up e.Expr into two parts -- e1 and e2 , and returns (e1)e2 . If the original expression e.Exprhas at least s.N terms (on the top level of structure), then the first s.N terms go to e1 , and the rest to e2 . Otherwise, e1 is e.Expr and e2 is empty. <Last s.N e.Expr> is similar to Firstbut it is e2 that has s.N terms. <Lenw e.Expr> returns the length of e.Exprin terms followed by e.Expr . <Lower e.Expr> returns the original expression e.Exprin which all capital letters are replaced by lower case letters. <Upper e.Expr> is similar to Lower . All lower case letters are capitalized. 5. System Functions <Step> <Time> returns a string containing the current system time. <Arg s.N> where s.N is a macrodigit, returns the command line argument which has the sequential number s.N . Command line arguments must not start with '-'(in order not to be confused with flags). <Mu s.F-name e.Expr> , or <Mu (e.String) e.Expr> finds the function whose name is s.F-name or
<Up e.Expr> upgrades e.Expr (demetacodes it). See Chapter 6 for restrictions on e.Expr . <Dn e.Expr>downgrades e.Expr (metacodes it). |