Skip to content

Commit 083a41a

Browse files
committed
Improve IndexExpression docs & update SliceExpression too
Make IndexExpression docs more generic. Mention pointer indexing & slicing. Mention first and last element expressions for the slice.
1 parent 9a7ddf8 commit 083a41a

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

spec/expression.dd

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,22 +1235,29 @@ $(GNAME IndexExpression):
12351235
$(GLINK PostfixExpression) $(D [) $(GLINK ArgumentList) $(D ])
12361236
)
12371237

1238-
$(P $(I PostfixExpression) is evaluated. If $(I PostfixExpression) is an
1239-
expression of type static array or dynamic array, the symbol $(DOLLAR) is set to
1240-
be the number of elements in the array. If $(I PostfixExpression) is a $(I
1241-
ValueSeq), the symbol $(DOLLAR) is set to be the number of elements
1242-
in the sequence. A new declaration scope is created for the evaluation of the
1243-
$(GLINK ArgumentList) and $(DOLLAR) appears in that scope only.)
1244-
1245-
$(P If $(I PostfixExpression) is a $(I ValueSeq),
1246-
then the $(GLINK ArgumentList) must consist of only one argument,
1238+
$(P $(I PostfixExpression) is evaluated.
1239+
If $(I PostfixExpression) is an expression of static or
1240+
dynamic array type, the result of the indexing is an lvalue
1241+
of the *i*th element in the array.
1242+
If $(I PostfixExpression) is a pointer `p`, the result is `*(p + i)`.
1243+
In each case, `i` is an integer evaluated from $(I ArgumentList).
1244+
)
1245+
1246+
$(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq))
1247+
then the $(I ArgumentList) must consist of only one argument,
12471248
and that must be statically evaluatable to an integral constant.
12481249
That integral constant $(I n) then selects the $(I n)th
12491250
expression in the $(I ValueSeq), which is the result
12501251
of the $(I IndexExpression).
12511252
It is an error if $(I n) is out of bounds of the $(I ValueSeq).
12521253
)
12531254

1255+
$(P The special variable `$` is declared and set to be the number
1256+
of elements in the $(I PostfixExpression) (when available).
1257+
A new declaration scope is created for the evaluation of the
1258+
$(I ArgumentList) and `$` appears in that scope only.
1259+
)
1260+
12541261
$(H2 $(LNAME2 slice_expressions, Slice Expressions))
12551262

12561263
$(GRAMMAR
@@ -1266,9 +1273,17 @@ $(GNAME Slice):
12661273
)
12671274

12681275
$(P $(I PostfixExpression) is evaluated.
1269-
If $(I PostfixExpression) is an expression of static array or
1270-
dynamic array type, the result of the slice is a dynamic array
1271-
of the element type of the $(I PostfixExpression).
1276+
If $(I PostfixExpression) is a static or dynamic
1277+
array `a`, the result of the slice is a dynamic array
1278+
referencing elements `a[i]` to `a[j-1]` inclusive, where `i`
1279+
and `j` are integers evaluated from the first and second $(I
1280+
AssignExpression) respectively.
1281+
)
1282+
1283+
$(P If $(I PostfixExpression) is a pointer `p`, the result
1284+
will be a dynamic array referencing elements from `p[i]` to `p[j-1]`
1285+
inclusive, where `i` and `j` are integers evaluated from the
1286+
first and second $(I AssignExpression) respectively.
12721287
)
12731288

12741289
$(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq)), then
@@ -1278,20 +1293,20 @@ $(GNAME Slice):
12781293
It is an error if those bounds are out of range.
12791294
)
12801295

1281-
$(P The special variable `$`
1282-
is declared and set to be the number of elements in the $(I PostfixExpression).
1283-
A new declaration scope is created for the evaluation of the
1284-
$(GLINK AssignExpression)..$(GLINK AssignExpression)
1285-
and `$` appears in that scope only.
1286-
)
1287-
12881296
$(P The first $(I AssignExpression) is taken to be the inclusive
12891297
lower bound
12901298
of the slice, and the second $(I AssignExpression) is the
12911299
exclusive upper bound.
12921300
The result of the expression is a slice of the elements in $(I PostfixExpression).
12931301
)
12941302

1303+
$(P The special variable `$` is declared and set to be the number
1304+
of elements in the $(I PostfixExpression) (when available).
1305+
A new declaration scope is created for the evaluation of the
1306+
$(I AssignExpression)`..`$(I AssignExpression) and `$` appears in
1307+
that scope only.
1308+
)
1309+
12951310
$(P If the $(D [ ]) form is used, the slice is of all the elements in $(I PostfixExpression).
12961311
)
12971312

0 commit comments

Comments
 (0)