The mode STRING
Keywords: pame

Subsections

- Exercises

# The mode `STRING`

The mode `STRING` is defined in the standard prelude as having the same mode as the expression `FLEX[1:0]CHAR`. That is, the identity declaration

       REF STRING s = LOC STRING

has exactly the same effect as the declaration

       REF FLEX[]CHAR s = LOC FLEX[1:0]CHAR

You will notice that although the mode indicant `STRING` appears on both sides of the identity declaration for `s`, in the second declaration the bounds are omitted on the left-hand side (the mode is a formal-declarer) and kept on the right-hand side (the actual-declarer). Without getting into abstruse grammatical explanations, just accept that if you define a mode like `STRING`, whenever it is used on the left-hand side of an identity declaration the compiler will ignore the bounds inherent in its definition.

We can now write

       s:="String"

which gives bounds of `[1:6]` to `s`. We can slice that row to get a value with mode `REF CHAR` which can be used in a formula. If we want to change the bounds of `s`, we must assign a value which yields a value of mode `[]CHAR` to the whole of `s` as in

        s:="Another string"

or

        s:=s[2:4]

Wherever `[]CHAR` appears in chapter 3, it may be safely replaced by `STRING`. This is because it is only names which are flexible so the flexibility of `STRING` is only available in `REF STRING` declarations.

There are two operators defined in the standard prelude which use an operand of mode `REF STRING`: `PLUSAB`, whose left operand has mode `REF STRING` and whose right operand has mode `STRING` or `CHAR`, and `PLUSTO`, whose left operand has mode `STRING` or `CHAR` and whose right operand has mode `REF STRING`. Using the concatenation operator `+`, their actions can be summarised as follows:

`a PLUSAB b` | ≡ a:=a+b
`a PLUSTO b` | ≡ b:=a+b

Thus `PLUSAB` concatenates `b` onto the end of `a`, and `PLUSTO` concatenates `a` to the beginning of `b`. Their alternative representations are `+:=` and `+=:` respectively. For example, if `a` refers to `"abc"` and `b` refers to `"def"`, after `a PLUSAB b`, `a` refers to `"abcdef"`, and after `a PLUSTO b`, `b` refers to `"abcdefdef"` (assuming the `PLUSAB` was elaborated first).

## Exercises

5.11
  Write a program which declares a name with mode `REF` `STRING` and then consecutively assigns the rows of characters `"ab"`, `"abc"`, upto the whole alphabet and prints each row on a separate line. Use a `FOR` loop clause. Ans
5.12
  Declare a flexible name which can refer to a 2-dimensional row whose elements have mode `REAL`. Assign a one-dimensional row whose elements are

         5.0 10.0 15.0 20.0

  Write the `print` phrase which will display each bound on the screen followed by a space, all on one line. Ans