-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranges.py
122 lines (104 loc) · 3.74 KB
/
ranges.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
from objects import Object
class Subscriptable(Object):
'''An object which has integer indexable elements.'''
def __init__(self, prototype):
Object.__init__(self, prototype=prototype)
def GetPySubscriptable(self):
raise NotImplementedError
def GetAttr(self, index):
try:
index_int = int(index)
return self.GetIndex(index_int)
except ValueError:
return Object.GetAttr(self, index)
def SetAttr(self, index, value):
try:
index_int = int(index)
self.SetIndex(index_int, value)
except ValueError:
# String non-integer index
return Object.SetAttr(self, index, value)
def Slice(self, lindex, rindex):
if lindex:
if rindex:
return self.__class__(self.GetPySubscriptable()[lindex:rindex])
else:
return self.__class__(self.GetPySubscriptable()[lindex:])
else:
if rindex:
return self.__class__(self.GetPySubscriptable()[:rindex])
else:
return self.__class__(self.GetPySubscriptable()[:])
class RangeReference(object):
def __init__(self,
base_expression,
startindex_expression,
endindex_expression):
self.base_expression = base_expression
self.startindex_expression = startindex_expression
self.endindex_expression = endindex_expression
self.state = 0
self.startindex = None
self.endindex = None
def Reset(self):
self.state = 0
self.base_expression.Reset()
if self.startindex_expression:
self.startindex_expression.Reset()
if self.endindex_expression:
self.endindex_expression.Reset()
def Eval(self, context, sub_value):
# TODO this can probably be cleaned up
self.state += 1
if self.state == 1:
return ("eval", context, self.base_expression)
elif self.state == 2:
self.evaluated_base = sub_value
if not isinstance(self.evaluated_base, Subscriptable):
raise TypeError("Attempted slice of non-subscriptable type")
if self.startindex_expression:
return ("eval", context, self.startindex_expression)
elif self.endindex_expression:
return ("eval", context, self.endindex_expression)
else:
return ("result", None, self)
elif self.state == 3:
try:
if self.startindex_expression:
self.startindex = int(sub_value.num)
if self.endindex_expression:
return ("eval", context, self.endindex_expression)
else:
return ("result", None, self)
elif self.endindex_expression:
self.endindex = int(sub_value.num)
return ("result", None, self)
except:
raise TypeError("Subscripts must be integers")
elif self.state == 4:
try:
if self.endindex_expression:
self.endindex = int(sub_value.num)
return ("result", None, self)
except:
raise TypeError("Subscripts must be integers")
def Get(self, context):
return self.evaluated_base.Slice(self.startindex, self.endindex)
def Set(self, val, context):
self.evaluated_base.SetSlice(self.startindex, self.endindex, val)
class WholeRangeReference(object):
def __init__(self, expression):
self.expression = expression
self.state = 0
def Reset(self):
self.state = 0
self.expression.Reset()
def Eval(self, context, sub_value):
self.state += 1
if self.state == 1:
return ("eval", context, self.expression)
else:
self.evaled_base = sub_value
return ("result", None, self)
def Get(self, context):
return self.evaled_base.Slice()