hdl.ast: add Value.{rotate_left,rotate_right}.

This commit is contained in:
Dan Ravensloft 2020-04-13 14:40:39 +01:00 committed by GitHub
parent 8b137438d0
commit 06c45c9ff0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 0 deletions

View file

@ -423,6 +423,42 @@ class Value(metaclass=ABCMeta):
else:
return Cat(*matches).any()
def rotate_left(self, offset):
"""Rotate left by constant modulo 2**len(self).
Parameters
----------
offset : int
Amount to rotate by.
Returns
-------
Value, out
The input rotated left by offset if offset is positive, else the input rotated right by -offset.
"""
if not isinstance(offset, int):
raise TypeError("Rotate amount must be an integer, not {!r}".format(offset))
offset %= len(self)
return Cat(self[-offset:], self[:-offset]) # meow :3
def rotate_right(self, offset):
"""Rotate right by constant modulo 2**len(self).
Parameters
----------
offset : int
Amount to rotate by.
Returns
-------
Value, out
The input rotated right by offset if offset is positive, else the input rotated left by -offset.
"""
if not isinstance(offset, int):
raise TypeError("Rotate amount must be an integer, not {!r}".format(offset))
offset %= len(self)
return Cat(self[offset:], self[:offset])
def eq(self, value):
"""Assignment.