-
Notifications
You must be signed in to change notification settings - Fork 3
/
value.py
148 lines (116 loc) · 4.04 KB
/
value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import string
from abc import ABC
from typing import List
import timestamp
class Value(ABC):
def __init__(self, value, timestamp):
self.value = value
self.timestamp = timestamp
def __eq__(self, other):
return self.value == other.value
def __str__(self):
raise NotImplementedError
def merge(self, other):
raise NotImplementedError
def get_timestamp(self):
return self.timestamp
class ValueArray:
def __init__(self, values):
self.values = values
def __getitem__(self, key):
return self.values[key]
def __len__(self):
return len(self.values)
def print_ascii(self):
raise NotImplementedError
def print_tikz(self):
raise NotImplementedError
class BoolValue(Value):
def __init__(self, value, timestamp=timestamp.t_0):
self.timestamp = timestamp
if value == 'x':
self.value = 0
return
# todo: proper solution
int_value = int(value)
if int_value not in [0, 1]:
raise ValueError("Incorrect value for a bool signal: " + value)
self.value = int_value
def __str__(self):
return '█' if self.value == 1 else ' '
def merge(self, other):
return BoolValue(self.value | other.value, self.timestamp) # todo: ?
class AsciiValue(Value):
def __init__(self, value, timestamp=timestamp.t_0):
self.timestamp = timestamp
if 'x' in value:
self.value = 0
return
# todo: proper solution
# value should be in the form ?b?010110101...
int_val = int(value, 2)
hx = hex(int_val)[2:].ljust(2, '0')
text = bytearray.fromhex(hx).decode('ascii', errors='ignore')
text = ''.join(filter(lambda c: c in set(string.printable), text))
text = text.strip()
# text = text.replace('&', '\\&').replace('#', '\\#')
self.value = text
def __str__(self):
return self.value
class HexValue(Value):
def __init__(self, value, timestamp=timestamp.t_0):
self.timestamp = timestamp
if 'x' in value:
self.value = 0
return
# todo: proper solution
# value should be in the form ?b?010110101...
int_val = int(value, 2)
self.value = hex(int_val)
def __str__(self):
return self.value
class BoolArray(ValueArray):
def __init__(self, values: List[BoolValue]):
self.values = values
def print_ascii(self) -> str:
return "".join([str(value) for value in self.values])
class AsciiArray(ValueArray):
def __init__(self, values: List[AsciiValue]):
self.values = values
def print_ascii(self) -> str:
length = len(self.values)
boundary = 0
output = ""
for (index, value) in enumerate(self.values):
if value != self.values[boundary] or index == length - 1:
if index == length - 1:
index += 1
period = index - boundary
text = str(self.values[boundary])
if len(text) > period:
cutoff = text[:period - 1] + "…"
else:
cutoff = text.ljust(period)
output += cutoff
boundary = index
return output
class HexArray(ValueArray):
def __init__(self, values: List[AsciiValue]):
self.values = values
def print_ascii(self) -> str:
length = len(self.values)
boundary = 0
output = ""
for (index, value) in enumerate(self.values):
if value != self.values[boundary] or index == length - 1:
if index == length - 1:
index += 1
period = index - boundary
text = str(self.values[boundary])
if len(text) > period:
cutoff = text[:period - 1] + "…"
else:
cutoff = text.ljust(period)
output += cutoff
boundary = index
return output