|
35 | 35 | All declarations for literals, aggregates, operators forming an expressions. |
36 | 36 | """ |
37 | 37 | 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 |
39 | 39 |
|
40 | 40 | from pyTooling.Decorators import export, readonly |
41 | 41 |
|
@@ -211,77 +211,76 @@ class BitStringBase(Flag): |
211 | 211 | @export |
212 | 212 | class BitStringLiteral(Literal): |
213 | 213 | # _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] |
216 | 219 |
|
217 | | - def __init__(self, value: str) -> None: |
| 220 | + def __init__(self, value: str, length: Nullable[int] = None, signed: Nullable[bool] = None) -> None: |
218 | 221 | super().__init__() |
219 | | - self._bits = len(value) |
220 | 222 | self._value = value |
| 223 | + self._length = length |
| 224 | + self._signed = signed |
221 | 225 |
|
222 | | - @readonly |
223 | | - def Bits(self) -> int: |
224 | | - return self._bits |
| 226 | + self._binaryValue = None |
| 227 | + self._bits = None |
225 | 228 |
|
226 | 229 | @readonly |
227 | 230 | def Value(self) -> str: |
228 | 231 | return self._value |
229 | 232 |
|
| 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 | + |
230 | 249 | 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 + "\"" |
232 | 263 |
|
233 | 264 |
|
234 | 265 | @export |
235 | 266 | class BinaryBitStringLiteral(BitStringLiteral): |
236 | 267 | _base: ClassVar[BitStringBase] = BitStringBase.Binary |
237 | 268 |
|
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 | 269 |
|
247 | 270 | @export |
248 | 271 | class OctalBitStringLiteral(BitStringLiteral): |
249 | 272 | _base: ClassVar[BitStringBase] = BitStringBase.Octal |
250 | 273 |
|
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 | 274 |
|
260 | 275 | @export |
261 | 276 | class DecimalBitStringLiteral(BitStringLiteral): |
262 | 277 | _base: ClassVar[BitStringBase] = BitStringBase.Decimal |
263 | 278 |
|
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 | 279 |
|
273 | 280 | @export |
274 | 281 | class HexadecimalBitStringLiteral(BitStringLiteral): |
275 | 282 | _base: ClassVar[BitStringBase] = BitStringBase.Hexadecimal |
276 | 283 |
|
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 | 284 |
|
286 | 285 | @export |
287 | 286 | class ParenthesisExpression: #(Protocol): |
|
0 commit comments