Previous Previous chapter · Next Next chapter · Contents Table of Contents

Chapter 6 : DATA TYPES AND CONVERSION

Most other programming languages require the user to explicitly declare the type of data to be stored in a variable. In SNOBOL4, any variable may contain any data type. Furthermore, the variable's type may be freely altered during program execution. SNOBOL4 remembers what kind of data is in each variable.

6.1 DATA TYPE NAMES

The formal name of a data type is specified by an upper-case string (or lower-case if case-folding is in effect), such as 'INTEGER', or 'ARRAY'. It is used with the CONVERT function to specify the data type conversion desired. The formal name is also the string returned when the DATATYPE() function is used to determine an object's type.

6.2 DATA TYPE CONVERSION

Data may be implicitly or explicitly converted from one type to another.

6.2.1 Implicit Conversion

Implicit conversion occurs automatically when SNOBOL4 requires a certain data type, and your program provides it in another form. Conversion to the correct data type will be attempted, and an error message given if conversion is not possible.

6.2.2 Explicit Conversion

A program may use the CONVERT() function to explicitly convert an object to another data type. The first argument is the object to be converted; the second is a string containing the formal name of the desired data type. The formal name must be in uppercase (lower-case allowed if case-folding). If conversion is possible, the function succeeds and returns the converted object. If not, the function fails. The call looks like this:
    NEWTYPE = CONVERT(OBJECT, "DESIRED TYPE")

6.2.3 Permissible Conversions

ARRAY to STRING

The formal name "ARRAY" is produced. The defining array dimension string is appended if less than 20 characters:
    A = ARRAY('1:50,6')
    OUTPUT = A
produces the string "ARRAY('1:50,6')".

CODE to STRING

The formal name "CODE" is produced:
    C = CODE(' PIT2 = .OPPIT4 :(RETURN)')
    OUTPUT = C
displays the string "CODE".

EXPRESSION to PATTERN

This occurs implicitly within a pattern match, or by using the EVAL function. The deferred expression is evaluated, using current values for any variables which appear. Example:
    LASTN = *(RTAB(N) REM . LCHARS)
     . . .
    N = 4
    SUBJECT LASTN                        :F(TOO_SHORT)
EXPRESSION to STRING

The formal name "EXPRESSION" is produced. For example,
    LASTN = *(RTAB(N) REM . LCHARS)
    OUTPUT = LASTN
produces the string "EXPRESSION".

INTEGER to PATTERN

This only occurs implicitly within a pattern match. The integer is converted to a string, and the string converted to a pattern. Example:
    SUBJECT 19 = ''
INTEGER to STRING

Leading zeros are suppressed, and a minus sign appears if the integer was negative. Integer zero is converted to the string "0". For example,
    A = -23;  B = 0;  C = 92
    OUTPUT = A B C
produces the string "-23092".

NAME to STRING

The formal name "NAME" is produced:
    N = .A[2]
    OUTPUT = N
displays the string "NAME".

PATTERN to STRING

The formal name "PATTERN" is produced. For example,
    WPAT = BREAK(LETTERS) SPAN(LETTERS) . WORD
    OUTPUT = WPAT
produces the string "PATTERN".

DEFINED    DATA TYPE to STRING
The formal name from the defining DATA function call is returned.
    DATA('COMPLEX(REAL,IMAG)')
    R1 = COMPLEX(2, 3)
    OUTPUT = R1
produces the string "COMPLEX".

STRING to INTEGER

The string must not have any leading or trailing blanks. A leading plus or minus sign is allowed, but must be followed by at least one digit. Leading zeros are allowed, and the resulting value must be in the legal range for integer values. A null string is converted to integer zero.
    RESULT = ("-14" + "") / "2"
stores integer -7 in RESULT.

STRING to PATTERN

This only occurs implicitly within a pattern match. The pattern created will match the specified substring:
    SUBJECT "HOPE"
TABLE to ARRAY

This only occurs when using the CONVERT function. The table is converted to a two dimensional array. Example:
    T = TABLE(100)
     . . .
    A = CONVERT(T, "ARRAY")         :F(EMPTY)
The table is converted to a rectangular array. Null table entries are omitted, and there must be at least one nonnull entry or the function fails. An N by 2 array is created, where N is the number of nonnull table values. The first array column contains the table subscripts, the second column contains the entry values.

TABLE to STRING

The formal name "TABLE" is returned with the present size of the table and its expansion increment. For example,
            T = TABLE(10,10)
             . . .
    ; Insert 45 nonnull elements into T
             . . .
            OUTPUT = T
produces the string "TABLE(50,10)" (because table segments in this case are allocated in multiples of 10).

The following matrix indicates conversions with CONVERT():

               |      Result Type            E
               |                             X
               |                             P
               |     I   P                   R   D
               | S   N   A                   E   E
               | T   T   T       A   T       S   F
               | R   E   T   N   R   A   C   S   I
               | I   G   E   A   R   B   O   I   N
      Argument | N   E   R   M   A   L   D   O   E
        Type   | G   R   N   E   Y   E   E   N   D
    -----------+-----------------------------------
        STRING | *   I   P               C   E
       INTEGER | S   *   P
       PATTERN | F       *
          NAME | F           *
         ARRAY | A               *   1
         TABLE | T               2   *
          CODE | F                       *
    EXPRESSION | F       P                   *
       DEFINED | F                               *
* The argument object is returned unchanged.
A The formal data type name "ARRAY" is returned with the defining prototype string if it is less than 20 characters.
C CONVERT(string,"CODE) behaves exactly like CODE(string).
E Produces an unevaluated expression, that may be subsequently used in a pattern, or evaluated with the EVAL() function.
F The formal data type name is returned.
I Numeric conversion is conditioned on magnitude and syntax restrictions. No leading or trailing blanks are permitted.
P Occurs implicitly within a pattern match.
S A number may always be converted to its string form.
T The string "TABLE" is returned with the present size of the table and its expansion increment: "TABLE(50,10)".
1 The array must be rectangular, with a second dimension of 2 (N rows by 2 columns). A table with N entries is created. The table subscripts are taken from the first column of the array; the table values are copied from the second column.
2 The table is converted to a rectangular array. Null table entries are omitted, and there must be at least one nonnull entry or the function fails. An N by 2 array is created, where N is the number of nonnull table values. The first array column contains the table subscripts, the second column contains the entry values.


Previous Previous chapter · Next Next chapter · Contents Table of Contents