Skip to content

Commit ba4e1fd

Browse files
committed
Enhance BitStringLiteral for binary, octal, decimal and hexadecimal.
1 parent db3da12 commit ba4e1fd

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

pyVHDLModel/Expression.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
3535
All declarations for literals, aggregates, operators forming an expressions.
3636
"""
37-
from typing import Tuple, List, Iterable, Union
37+
from enum import Flag
38+
from typing import Tuple, List, Iterable, Union, ClassVar
3839

3940
from pyTooling.Decorators import export, readonly
4041

@@ -196,14 +197,32 @@ def __str__(self) -> str:
196197
return "\"" + self._value + "\""
197198

198199

200+
@export
201+
class BitStringBase(Flag):
202+
NoBase = 0
203+
Binary = 2
204+
Octal = 8
205+
Decimal = 10
206+
Hexadecimal = 16
207+
Unsigned = 32
208+
Signed = 64
209+
210+
199211
@export
200212
class BitStringLiteral(Literal):
213+
# _base: ClassVar[BitStringBase]
201214
_value: str
215+
_bits: int
202216

203217
def __init__(self, value: str) -> None:
204218
super().__init__()
219+
self._bits = len(value)
205220
self._value = value
206221

222+
@readonly
223+
def Bits(self) -> int:
224+
return self._bits
225+
207226
@readonly
208227
def Value(self) -> str:
209228
return self._value
@@ -212,6 +231,58 @@ def __str__(self) -> str:
212231
return "\"" + self._value + "\""
213232

214233

234+
@export
235+
class BinaryBitStringLiteral(BitStringLiteral):
236+
_base: ClassVar[BitStringBase] = BitStringBase.Binary
237+
238+
def __init__(self, value: str, bits: int = 0) -> None:
239+
super().__init__(value)
240+
if bits > 0:
241+
self._bits = bits
242+
243+
def __str__(self) -> str:
244+
return "b\"" + self._value + "\""
245+
246+
247+
@export
248+
class OctalBitStringLiteral(BitStringLiteral):
249+
_base: ClassVar[BitStringBase] = BitStringBase.Octal
250+
251+
def __init__(self, value: str, bits: int = 0) -> None:
252+
super().__init__(value)
253+
if bits > 0:
254+
self._bits = bits
255+
256+
def __str__(self) -> str:
257+
return "o\"" + self._value + "\""
258+
259+
260+
@export
261+
class DecimalBitStringLiteral(BitStringLiteral):
262+
_base: ClassVar[BitStringBase] = BitStringBase.Decimal
263+
264+
def __init__(self, value: str, bits: int = 0) -> None:
265+
super().__init__(value)
266+
if bits > 0:
267+
self._bits = bits
268+
269+
def __str__(self) -> str:
270+
return "d\"" + self._value + "\""
271+
272+
273+
@export
274+
class HexadecimalBitStringLiteral(BitStringLiteral):
275+
_base: ClassVar[BitStringBase] = BitStringBase.Hexadecimal
276+
277+
def __init__(self, value: str, bits: int = 0) -> None:
278+
super().__init__(value)
279+
if bits > 0:
280+
self._bits = bits
281+
282+
def __str__(self) -> str:
283+
return "x\"" + self._value + "\""
284+
285+
215286
@export
216287
class ParenthesisExpression: #(Protocol):
217288
__slots__ = () # FIXME: use ExtendedType?

pyVHDLModel/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
__email__ = "[email protected]"
4949
__copyright__ = "2016-2025, Patrick Lehmann"
5050
__license__ = "Apache License, Version 2.0"
51-
__version__ = "0.31.3"
51+
__version__ = "0.32.0"
5252

5353

5454
from enum import unique, Enum, Flag, auto

0 commit comments

Comments
 (0)