Modes

25.1 Primitive modes

Denotations (literals) 3.14, 1e10, 2.5e-3, .5 TRUE, FALSE "a", """ (a quote character) packed bit string 2r1010, 8r17, 16rff packed character string via bytes pack("text")

EMPTY; SKIP yields an undefined value of any mode Predefined structured and other modes:

Definition / meaning FLEX [1:0] CHAR — a flexible row of characters; denotation "hello"

STRUCT (REAL re, im) — complex numbers; 1 I 2 builds 1 + 2i formats for formatted transput: $... $ FILE, CHANNEL transput (Chapter 34) semaphore for parallel processing (Chapter 35)

25.2 LONG and SHORT variants

Sizes are written with LONG or SHORT prefixes: LONG INT, LONG LONG INT, SHORT INT, LONG REAL, LONG LONG REAL, LONG COMPL, LONG BITS. Each implementation chooses actual sizes; environment enquiries tell you what you have:

In a68g, LONG LONG INT and LONG LONG REAL are multi-precision types whose precision is set with the --precision option; LONG modes have a fixed, larger size than the plain modes.

There is no implicit widening between sizes: use LENG (to a longer size) and SHORTEN:

BEGIN
   print(("max int        = ", max int, newline));
   print(("long max int   = ", long max int, newline));
   print(("max real       = ", max real, newline));
   print(("small real     = ", small real, newline));   # smallest x with 1 + x /= 1 #
   print(("int width      = ", int width, newline));
   print(("bits width     = ", bits width, newline))
END
BEGIN
   LONG LONG INT f := LONG LONG 1;              # denotation of size LONG LONG #
   FOR i TO 30 DO f *:= LENG LENG i OD;          # LENG LENG converts INT -> LONG LONG INT #
   print(("30! = ", whole(f, 0), newline))
END

25.3 Operations on primitive modes

BEGIN
   INT a = 17, b = 5;
   print((a + b, a - b, a * b, newline));
   print((a OVER b, a MOD b, newline));       # also written a % b, a %* b : 3 and 2 #
   print((a / b, newline));                   # / on INTs yields REAL: 3.4 #
   print((a ** 2, ABS -3, SIGN -3, ODD a, newline));
   REAL x = 2.7;
   print((ENTIER x, ROUND x, newline));       # 2 and 3 #
   print((sqrt(2), exp(1), ln(10), sin(pi / 2), arctan(1) * 4, newline));
   CHAR c = "A";
   print((ABS c, REPR 66, newline));          # 65 and "B" #
   BOOL p = TRUE, q = FALSE;
   print((p AND q, p OR q, NOT p, newline))
END

25.4 STRING

BEGIN
   STRING s := "Hello";
   s +:= ", world";                     # append #
   print((s, newline));
   print((UPB s, newline));             # length (lower bound is 1) #
   print((s[1:5], newline));            # slice: "Hello" #
   print((s[UPB s], newline));          # last character #
   STRING line = "-" * 20;              # repetition #
   print((line, newline));
   "x" +=: s;                           # prepend (PLUSTO) #
   print((s, newline));
   print(("abc" < "abd", newline));     # lexicographic comparison #
   INT pos;
   IF char in string("w", pos, s) THEN print(("w found at ", whole(pos, 0), newline)) FI
END

25.5 BITS and BYTES

BYTES packs a fixed number of characters into a word for efficiency (bytes pack); it is rarely needed in modern code.

BEGIN
   BITS b = 2r1100, c = 2r1010;
   print((b AND c, newline));       # 2r1000 (printed as a row of F/T flags) #
   print((b OR c, newline));
   print((ABS (b AND c), newline)); # 8 #
   print((BIN 5, newline));         # INT -> BITS #
   print((ABS (b SHL 1), ABS (b SHR 2), newline));   # 24 and 3 #
   print((bits width ELEM b, newline))  # test the rightmost bit; bit 1 is the leftmost #
END

25.6 COMPL

BEGIN
   COMPL z = 3 I 4, w = 1 I -1;
   print((RE z, IM z, ABS z, newline));    # 3, 4, 5 #
   print((z * w, newline));
   print((CONJ z, ARG z, newline))
END

25.7 Mode declarations

Modes are compared structurally: two independently declared STRUCT (REAL x, y) modes are the *same* mode.

A recursive mode must be "well formed": the recursion must pass through REF or PROC (a STRUCT cannot directly contain itself).

MODE VECTOR = [1:3] REAL;
MODE POINT = STRUCT (REAL x, y);
MODE NUMBER = UNION (INT, REAL);
MODE FUNC = PROC (REAL) REAL;
MODE NODE = STRUCT (INT value, REF NODE next);    # recursive via REF #