Array variables or constants may be subscripted or sliced. A subscript is an expression that is evaluated to get the index of a single element while a slice is a subarray of an array i.e. the array formed by all elements between the given lower and upper bound of the slice inclusive. The general form is a list of bounds:
bound, bound, ... |
where each bound is a single expression (a subscript) or a slice. Slice definitions has one of four forms.
expr : expr expr : * * : expr * |
The first form specifies the lower and the upper bound of the slice, the second form specifies the lower bound and takes the end of the array as the upper bound, the third form takes the beginning of the array as the lower bound and specifies the upper bound, while the fourth and last form takes the beginning and the end of the array as lower and upper bounds.
Strings can be treated as one dimensional arrays of characters indexed from 1 to the size of the string. Subscripting and slicing for strings is identical to subscripting and slicing for arrays.
rec (string[10] name; int grade) x |
The fields name and grade would then be referenced as x.name and x.grade resp. The syntax for unions is equivalent to the syntax for records.
Dereferencing pointers, i.e. to obtain the value the pointer is pointing on, is done using the hat (^) operator. Assume the following pointers
ptr int p # a pointer to an integer ptr ptr int pp # a pointer to a pointer to an int ptr rec(int i1,i2) prec # a pointer to a record |
Some examples of dereferencing would be
p^ # the integer to which the pointer p points pp^ # the integer pointer to which the pointer pp points pp^^ # the integer to which pp^ points prec^ # the record to which prec points to |
Assume the following declarations.
type point = rec(real x,y)
type prim_color = enum(red,blue,yellow)
type rectangle = rec(point upleft,botright;
prim_color color;
string[20] label)
rectangle display[1:N]
ptr rectangle ptrect
|
Then sample references include the following:
display[3].color # the color of the 3rd displayed rectangle
display[3].label[1:4] # the first four chars of the label of
# display[3]
display[3].upleft.x # the x coordinate of the upleft point of
# display[3]
prect^.upleft.x # the x coordinate of the upleft point of
# the rectangle to which prect points
|