Skip to content

Commit 3d5c36a

Browse files
committed
docs/reference: finish Value section.
1 parent cd51e02 commit 3d5c36a

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

amaranth/hdl/_ast.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class Value(metaclass=ABCMeta):
466466
value-castable objects, and the Amaranth standard library provides several built-in ones:
467467
468468
* :mod:`amaranth.lib.enum` classes are a drop-in replacement for the standard Python
469-
:class:`enum` classes that can be defined with an Amaranth shape;
469+
:mod:`enum` classes that can be defined with an Amaranth shape;
470470
* :mod:`amaranth.lib.data` classes allow defining complex data structures such as structures
471471
and unions.
472472
@@ -1087,22 +1087,35 @@ def __len__(self):
10871087
def __getitem__(self, key):
10881088
"""Bit slicing.
10891089
1090-
.. todo::
1090+
Selects a constant-width, constant-offset part of :py:`self`. All three slicing syntaxes
1091+
(:py:`self[i]`, :py:`self[i:j]`, and :py:`self[i:j:k]`) as well as negative indices are
1092+
supported. Like with other Python containers, out-of-bounds indices are trimmed to
1093+
the bounds of :py:`self`.
10911094
1092-
Describe this operation.
1093-
"""
1094-
n = len(self)
1095+
To select a variable-offset part of :py:`self`, use :meth:`bit_select` or
1096+
:meth:`word_select` instead.
1097+
1098+
Returns
1099+
-------
1100+
:class:`Value`, :py:`unsigned(1)`, :ref:`assignable <lang-assignable>`
1101+
If :py:`key` is an :class:`int`.
1102+
:class:`Value`, :py:`unsigned(j - i)`, :ref:`assignable <lang-assignable>`
1103+
If :py:`key` is a slice :py:`i:j` where :py:`i` and :py:`j` are :class:`int`\\ s.
1104+
:class:`Value`, :py:`unsigned(len(range(*slice(i, j, k).indices(len(self)))))`, :ref:`assignable <lang-assignable>`
1105+
If :py:`key` is a slice :py:`i:j:k` where :py:`i`, :py:`j`, and :py:`k` are :class:`int`\\ s.
1106+
"""
1107+
length = len(self)
10951108
if isinstance(key, int):
1096-
if key not in range(-n, n):
1097-
raise IndexError(f"Index {key} is out of bounds for a {n}-bit value")
1109+
if key not in range(-length, length):
1110+
raise IndexError(f"Index {key} is out of bounds for a {length}-bit value")
10981111
if key < 0:
1099-
key += n
1112+
key += length
11001113
return Slice(self, key, key + 1, src_loc_at=1)
11011114
elif isinstance(key, slice):
11021115
if isinstance(key.start, Value) or isinstance(key.stop, Value):
11031116
raise TypeError(f"Cannot slice value with a value; use Value.bit_select() or "
11041117
f"Value.word_select() instead")
1105-
start, stop, step = key.indices(n)
1118+
start, stop, step = key.indices(length)
11061119
if step != 1:
11071120
return Cat(self[i] for i in range(start, stop, step))
11081121
return Slice(self, start, stop, src_loc_at=1)
@@ -1220,14 +1233,19 @@ def matches(self, *patterns):
12201233
"""Pattern matching.
12211234
12221235
Matches against a set of patterns, recognizing the same grammar as :py:`with m.Case()`.
1236+
The pattern syntax is described in the :ref:`language guide <lang-matchop>`.
12231237
1224-
.. todo::
1225-
1226-
Describe the pattern language in detail.
1238+
Each of the :py:`patterns` may be a :class:`str` or a :ref:`constant-castable object
1239+
<lang-constcasting>`.
12271240
12281241
Returns
12291242
-------
12301243
:class:`Value`, :py:`unsigned(1)`
1244+
1245+
Raises
1246+
------
1247+
:exc:`SyntaxError`
1248+
If a pattern has invalid syntax.
12311249
"""
12321250
matches = []
12331251
# This code should accept exactly the same patterns as `with m.Case(...):`.

docs/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@ See also the introduction to :ref:`values <lang-values>` and :ref:`casting from
116116
.. autoclass:: Value
117117
:special-members: __bool__, __pos__, __neg__, __add__, __radd__, __sub__, __rsub__, __mul__, __rmul__, __mod__, __rmod__, __floordiv__, __rfloordiv__, __eq__, __ne__, __lt__, __le__, __gt__, __ge__, __abs__, __invert__, __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __lshift__, __rlshift__, __rshift__, __rrshift__, __len__, __getitem__, __contains__, __hash__
118118
.. autoclass:: ValueCastable()
119-
.. autoclass:: ValueLike()
119+
.. autoclass:: ValueLike()

0 commit comments

Comments
 (0)