LPC Substructures
1. Indexing and Ranging - General Introduction
----------------------------------------------
Since v20.25a6, MudOS provides a way of indexing or getting
slices (which I will, following common use, call 'ranging') of
strings/buffers/arrays/mappings (for mappings only indexing is
available) as well as means of changing the values of data
via lvalues (i.e. 'assignable values') formed by indexing/ranging.
As an example, if we set str as "abcdefg", str[0] will
be 'a', str[1] 'b' etc. Similarly, the nth element of an array
arr is accessed via arr[n-1], and the value corresponding to
key x of mapping m, m[x]. The '<' token can be used to denote
indexing from the right, i.e. str[ )[a_1][a_2]...[a_n]
( )[a_1][a_2]...[a_n]
(9)
/* Remark: n >= 1 here */
assignment token is one of +=, -=, *=, &=, /=, %=, ^=, |=, =, <<=,
>>=.
However, because of the same reason that when we assign to a
string, we obtain a new copy, (x = "foo")[2] = 'a' is invalidated at
runtime. (One way to think about this is, essentially, assignment leaves
the rhs as a return value, so x = "foo" returns "foo", the right hand
side, which is not the same "foo" as the one in x. For arrays/buffers
this is no problem because by assigning, we share the array/buffer)
Call the lvalues in (9) complex lvalues. Then the following is
also a valid lvalue:
( )[a_1][a_2]...[a_n]
(10)
and if we now call the above lvalues also complex lvalues, it would
still be consistent, i.e. (((a[0] = b)[1] = c)[2] = d)[3] is an okay
lvalue (though I wouldn't suggest using it for clarity's sake :)).
Now, the last class of valid lvalues are range lvalues, which
are denoted by ranging either a basic, indexed or complex lvalue:
[n1..n2]
[n1..n2]
[n1..n2]
plus other ranges such as [ y)[a]
Diagnosis: Something like (x = foo)[2..3] or (x = foo) was taken to
be an lvalue.
Err 4: Illegal to have (x[a..b] y) to be the beginning of
an lvalue
Diagnosis: You did something as described, i.e. (x[1..6] = foo)[3] is
not allowed.
Err 5: Illegal lvalue
Diagnosis: Oops, we are out of luck here :) Try looking at your lvalue
more carefully, and see that it obeys the rules described
in section 3 above.
5. Coming attractions
---------------------
Perhaps a pointer type will be introduced to allow passing
by reference into functions. Mappings may be multivalued and
multi-indexable.
Author : Symmetry@Tmi-2, IdeaExchange
Last Updated : Tue Jan 10 11:02:40 EST 1995