34
34
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
35
35
import com .oracle .graal .python .builtins .PythonBuiltins ;
36
36
import com .oracle .graal .python .builtins .objects .PNone ;
37
+ import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
37
38
import com .oracle .graal .python .nodes .SpecialMethodNames ;
38
39
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
39
40
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
41
+ import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
40
42
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
41
43
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
44
+ import com .oracle .truffle .api .dsl .Cached ;
42
45
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
43
46
import com .oracle .truffle .api .dsl .ImportStatic ;
44
47
import com .oracle .truffle .api .dsl .NodeFactory ;
@@ -85,6 +88,10 @@ protected int get(PSlice self) {
85
88
protected Object getNone (@ SuppressWarnings ("unused" ) PSlice self ) {
86
89
return PNone .NONE ;
87
90
}
91
+
92
+ public static StartNode create () {
93
+ return SliceBuiltinsFactory .StartNodeFactory .create ();
94
+ }
88
95
}
89
96
90
97
@ Builtin (name = "stop" , fixedNumOfPositionalArgs = 1 , isGetter = true )
@@ -101,6 +108,10 @@ protected int get(PSlice self) {
101
108
protected Object getNone (@ SuppressWarnings ("unused" ) PSlice self ) {
102
109
return PNone .NONE ;
103
110
}
111
+
112
+ public static StopNode create () {
113
+ return SliceBuiltinsFactory .StopNodeFactory .create ();
114
+ }
104
115
}
105
116
106
117
@ Builtin (name = "step" , fixedNumOfPositionalArgs = 1 , isGetter = true )
@@ -117,5 +128,93 @@ protected int get(PSlice self) {
117
128
protected Object getNone (@ SuppressWarnings ("unused" ) PSlice self ) {
118
129
return PNone .NONE ;
119
130
}
131
+
132
+ public static StepNode create () {
133
+ return SliceBuiltinsFactory .StepNodeFactory .create ();
134
+ }
135
+ }
136
+
137
+ @ Builtin (name = "indices" , fixedNumOfPositionalArgs = 2 )
138
+ @ GenerateNodeFactory
139
+ @ ImportStatic (SequenceUtil .class )
140
+ abstract static class IndicesNode extends PythonBinaryBuiltinNode {
141
+
142
+ private Object [] adjustIndices (int length , int start , int stop , int step ) {
143
+ int _start = start ;
144
+ int _stop = stop ;
145
+
146
+ if (start < 0 ) {
147
+ _start += length ;
148
+
149
+ if (_start < 0 ) {
150
+ _start = (step < 0 ) ? -1 : 0 ;
151
+ }
152
+ } else if (start >= length ) {
153
+ _start = (step < 0 ) ? length - 1 : length ;
154
+ }
155
+
156
+ if (stop < 0 ) {
157
+ _stop += length ;
158
+
159
+ if (_stop < 0 ) {
160
+ _stop = (step < 0 ) ? -1 : 0 ;
161
+ }
162
+ } else if (_stop >= length ) {
163
+ _stop = (step < 0 ) ? length - 1 : length ;
164
+ }
165
+
166
+ // if (step < 0) {
167
+ // if (_stop < _start) {
168
+ // return (_start - _stop - 1) / (-step) + 1;
169
+ // }
170
+ // }
171
+ // else {
172
+ // if (_start < _stop) {
173
+ // return (_stop - _start - 1) / step + 1;
174
+ // }
175
+ // }
176
+
177
+ return new Object []{_start , _stop , step };
178
+ }
179
+
180
+ @ Specialization ()
181
+ protected PTuple get (PSlice self , int length ,
182
+ @ Cached ("create()" ) StartNode startNode ,
183
+ @ Cached ("create()" ) StopNode stopNode ,
184
+ @ Cached ("create()" ) StepNode stepNode ) {
185
+ Object start = startNode .execute (self );
186
+ Object stop = stopNode .execute (self );
187
+ Object step = stepNode .execute (self );
188
+
189
+ int _start = -1 ;
190
+ int _stop = -1 ;
191
+ int _step = -1 ;
192
+
193
+ if (step == PNone .NONE ) {
194
+ _step = 1 ;
195
+ } else if (step instanceof Integer ) {
196
+ _step = (int ) step ;
197
+ }
198
+
199
+ if (start == PNone .NONE ) {
200
+ _start = _step < 0 ? length - 1 : 0 ;
201
+ } else if (start instanceof Integer ) {
202
+ _start = (int ) start ;
203
+ if (_start < 0 ) {
204
+ _start += length ;
205
+ }
206
+ }
207
+
208
+ if (stop == PNone .NONE ) {
209
+ _stop = _step < 0 ? -1 : length ;
210
+ } else if (stop instanceof Integer ) {
211
+ _stop = (int ) stop ;
212
+ if (_stop < 0 ) {
213
+ _stop += length ;
214
+ }
215
+ }
216
+
217
+ return factory ().createTuple (adjustIndices (length , _start , _stop , _step ));
218
+ }
120
219
}
121
220
}
0 commit comments