-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.py
151 lines (118 loc) · 4.3 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
149
150
151
class Value():
def __init__(self, val, t):
if val is None:
self.val = None
elif t == 'int':
self.val = int(val)
elif t == 'double':
self.val = float(val)
elif t == 'boolean':
self.val = bool(val)
elif t == 'char':
self.val = str(val)[0]
elif t == 'String':
self.val = str(val)
else:
self.val = val
self.type = t
def __str__(self):
if self.val is None:
return 'null'
elif self.val == True and self.type == 'boolean':
return 'true'
elif self.val == False and self.type == 'boolean':
return 'false'
elif self.type == 'String':
return '"' + str(self.val) + '"'
elif self.type == 'char':
return "'" + str(self.val) + "'"
else:
return str(self.val)
def __bool__(self):
if self.val == None:
return False
elif self.type == 'int' or self.type == 'double':
return self.val != 0
elif self.type == 'boolean':
return self.val
elif self.type == 'String':
return len(self.val) > 0
else:
return bool(self.val)
def __len__(self):
if self.val == None or self.val == False:
return 0
elif self.type == 'int' or self.type == 'double':
return len(str(self.val))
elif self.type == 'boolean':
return 1
elif self.type == 'String':
return len(self.val)
else:
return len(self.val)
def __contains__(self, other):
return Value(self.val.__contains__(other), 'boolean')
def __getitem__(self, other):
return Value(self.val.__getitem__(other), 'Object')
def __setitem__(self, other, val):
return Value(self.val.__setitem__(other, val), 'Object')
def __delitem__(self, other):
return Value(self.val.__delitem__(other), 'Object')
def __lt__(self, other):
return Value(self.val.__lt__(other.val), 'boolean')
def __le__(self, other):
return Value(self.val.__le__(other.val), 'boolean')
def __eq__(self, other):
if not isinstance(other, Value):
return False
return Value(self.val == other.val and self.type == other.type, 'boolean')
def __ne__(self, other):
if not isinstance(other, Value):
return True
return Value(self.val != other.val or self.type != other.type, 'boolean')
def __ge__(self, other):
return Value(self.val.__ge__(other.val), 'boolean')
def __gt__(self, other):
return Value(self.val.__gt__(other.val), 'boolean')
def __and__(self, other):
return Value(self.val.__and__(other.val), 'boolean')
def __or__(self, other):
return Value(self.val.__or__(other.val), 'boolean')
def __xor__(self, other):
return Value(self.val.__xor__(other.val), 'boolean')
def __index__(self):
return self.val.__index__()
def __abs__(self):
return Value(self.val.__abs__(), self.type)
def __neg__(self):
return Value(self.val.__neg__(), self.type)
def __pos__(self):
return Value(self.val.__pos__(), self.type)
def __add__(self, other):
if isinstance(other, Value):
val = other.val
else:
val = str(other)
if self.type == 'String':
val = str(val)
return Value(self.val.__add__(val), self.type)
def __sub__(self, other):
return Value(self.val.__sub__(other.val), self.type)
def __mul__(self, other):
return Value(self.val.__mul__(other.val), self.type)
def __floordiv__(self, other):
return Value(self.val.__floordiv__(other.val), self.type)
def __truediv__(self, other):
return Value(self.val.__truediv__(other.val), self.type)
def __mod__(self, other):
return Value(self.val.__mod__(other.val), self.type)
def __pow__(self, other):
return Value(self.val.__pow__(other.val), self.type)
def __iadd__(self, other):
self.val = self.val.__add__(other.val)
return self
def __isub__(self, other):
self.val = self.val.__sub__(other.val)
return self
def __hash__(self):
return hash(self.val)