forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjrange.rs
More file actions
195 lines (172 loc) · 5.42 KB
/
objrange.rs
File metadata and controls
195 lines (172 loc) · 5.42 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use super::super::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objint;
use super::objtype;
use num_bigint::{BigInt, ToBigInt};
use num_traits::{One, Signed, ToPrimitive, Zero};
#[derive(Debug, Clone)]
pub struct RangeType {
// Unfortunately Rust's built in range type doesn't support things like indexing
// or ranges where start > end so we need to roll our own.
pub start: BigInt,
pub end: BigInt,
pub step: BigInt,
}
impl RangeType {
#[inline]
pub fn len(&self) -> usize {
((self.end.clone() - self.start.clone()) / self.step.clone())
.abs()
.to_usize()
.unwrap()
}
#[inline]
pub fn is_empty(&self) -> bool {
(self.start <= self.end && self.step.is_negative())
|| (self.start >= self.end && self.step.is_positive())
}
#[inline]
pub fn forward(&self) -> bool {
self.start < self.end
}
#[inline]
pub fn get(&self, index: BigInt) -> Option<BigInt> {
let result = self.start.clone() + self.step.clone() * index;
if self.forward() && !self.is_empty() && result < self.end {
Some(result)
} else if !self.forward() && !self.is_empty() && result > self.end {
Some(result)
} else {
None
}
}
}
pub fn init(context: &PyContext) {
let ref range_type = context.range_type;
context.set_attr(&range_type, "__new__", context.new_rustfunc(range_new));
context.set_attr(&range_type, "__iter__", context.new_rustfunc(range_iter));
context.set_attr(&range_type, "__len__", context.new_rustfunc(range_len));
context.set_attr(
&range_type,
"__getitem__",
context.new_rustfunc(range_getitem),
);
}
fn range_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(cls, None), (first, Some(vm.ctx.int_type()))],
optional = [
(second, Some(vm.ctx.int_type())),
(step, Some(vm.ctx.int_type()))
]
);
let start = if let Some(_) = second {
objint::get_value(first)
} else {
BigInt::zero()
};
let end = if let Some(pyint) = second {
objint::get_value(pyint)
} else {
objint::get_value(first)
};
let step = if let Some(pyint) = step {
objint::get_value(pyint)
} else {
BigInt::one()
};
if step.is_zero() {
Err(vm.new_value_error("range with 0 step size".to_string()))
} else {
Ok(PyObject::new(
PyObjectPayload::Range {
range: RangeType { start, end, step },
},
cls.clone(),
))
}
}
fn range_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(range, Some(vm.ctx.range_type()))]);
Ok(PyObject::new(
PyObjectPayload::Iterator {
position: 0,
iterated_obj: range.clone(),
},
vm.ctx.iter_type(),
))
}
fn range_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.range_type()))]);
let len = match zelf.borrow().payload {
PyObjectPayload::Range { ref range } => range.len(),
_ => unreachable!(),
};
Ok(vm.ctx.new_int(len.to_bigint().unwrap()))
}
fn range_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, Some(vm.ctx.range_type())), (subscript, None)]
);
let zrange = if let PyObjectPayload::Range { ref range } = zelf.borrow().payload {
range.clone()
} else {
unreachable!()
};
match subscript.borrow().payload {
PyObjectPayload::Integer { ref value } => {
if let Some(int) = zrange.get(value.clone()) {
Ok(PyObject::new(
PyObjectPayload::Integer {
value: int.to_bigint().unwrap(),
},
vm.ctx.int_type(),
))
} else {
Err(vm.new_index_error("range object index out of range".to_string()))
}
}
PyObjectPayload::Slice { start, stop, step } => {
let new_start = if let Some(int) = start {
if let Some(i) = zrange.get(int.into()) {
i
} else {
zrange.start.clone()
}
} else {
zrange.start.clone()
};
let new_end = if let Some(int) = stop {
if let Some(i) = zrange.get(int.into()) {
i
} else {
zrange.end
}
} else {
zrange.end
};
let new_step = if let Some(int) = step {
(int as i64) * zrange.step
} else {
zrange.step
};
Ok(PyObject::new(
PyObjectPayload::Range {
range: RangeType {
start: new_start,
end: new_end,
step: new_step,
},
},
vm.ctx.range_type(),
))
}
_ => Err(vm.new_type_error("range indices must be integer or slice".to_string())),
}
}