Skip to content

Commit 64ba165

Browse files
committed
Handle BitStringLiterals (binary, octal, decimal, hexadecimal) including length and signed/unsigned modifiers.
1 parent 133b86b commit 64ba165

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

pyVHDLModel/Expression.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
All declarations for literals, aggregates, operators forming an expressions.
3636
"""
3737
from enum import Flag
38-
from typing import Tuple, List, Iterable, Union, ClassVar
38+
from typing import Tuple, List, Iterable, Union, ClassVar, Optional as Nullable
3939

4040
from pyTooling.Decorators import export, readonly
4141

@@ -211,77 +211,76 @@ class BitStringBase(Flag):
211211
@export
212212
class BitStringLiteral(Literal):
213213
# _base: ClassVar[BitStringBase]
214-
_value: str
215-
_bits: int
214+
_value: str
215+
_binaryValue: str
216+
_bits: int
217+
_length: Nullable[int]
218+
_signed: Nullable[bool]
216219

217-
def __init__(self, value: str) -> None:
220+
def __init__(self, value: str, length: Nullable[int] = None, signed: Nullable[bool] = None) -> None:
218221
super().__init__()
219-
self._bits = len(value)
220222
self._value = value
223+
self._length = length
224+
self._signed = signed
221225

222-
@readonly
223-
def Bits(self) -> int:
224-
return self._bits
226+
self._binaryValue = None
227+
self._bits = None
225228

226229
@readonly
227230
def Value(self) -> str:
228231
return self._value
229232

233+
@readonly
234+
def BinaryValue(self) -> str:
235+
return self._binaryValue
236+
237+
@readonly
238+
def Bits(self) -> Nullable[int]:
239+
return self._bits
240+
241+
@readonly
242+
def Length(self) -> Nullable[int]:
243+
return self._length
244+
245+
@readonly
246+
def Signed(self) -> Nullable[bool]:
247+
return self._signed
248+
230249
def __str__(self) -> str:
231-
return "\"" + self._value + "\""
250+
signed = "" if self._signed is None else "s" if self._signed is True else "u"
251+
if self._base is BitStringBase.NoBase:
252+
base = ""
253+
elif self._base is BitStringBase.Binary:
254+
base = "b"
255+
elif self._base is BitStringBase.Octal:
256+
base = "o"
257+
elif self._base is BitStringBase.Decimal:
258+
base = "d"
259+
elif self._base is BitStringBase.Hexadecimal:
260+
base = "x"
261+
length = "" if self._length is None else str(self._length)
262+
return length + signed + base + "\"" + self._value + "\""
232263

233264

234265
@export
235266
class BinaryBitStringLiteral(BitStringLiteral):
236267
_base: ClassVar[BitStringBase] = BitStringBase.Binary
237268

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-
246269

247270
@export
248271
class OctalBitStringLiteral(BitStringLiteral):
249272
_base: ClassVar[BitStringBase] = BitStringBase.Octal
250273

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-
259274

260275
@export
261276
class DecimalBitStringLiteral(BitStringLiteral):
262277
_base: ClassVar[BitStringBase] = BitStringBase.Decimal
263278

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-
272279

273280
@export
274281
class HexadecimalBitStringLiteral(BitStringLiteral):
275282
_base: ClassVar[BitStringBase] = BitStringBase.Hexadecimal
276283

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-
285284

286285
@export
287286
class ParenthesisExpression: #(Protocol):

0 commit comments

Comments
 (0)