Skip to content

Commit 05929b3

Browse files
committed
Add ellipsis type, test and do rustfmt.
1 parent e25742f commit 05929b3

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

parser/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl Expression {
260260
Lambda { .. } => "lambda",
261261
IfExpression { .. } => "conditional expression",
262262
True | False | None => "keyword",
263-
Ellipsis => "ellipsis"
263+
Ellipsis => "ellipsis",
264264
}
265265
}
266266
}

parser/src/lexer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,8 @@ where
10121012
let tok_start = self.get_pos();
10131013
self.next_char();
10141014
if let (Some('.'), Some('.')) = (&self.chr0, &self.chr1) {
1015-
self.next_char();self.next_char();
1015+
self.next_char();
1016+
self.next_char();
10161017
let tok_end = self.get_pos();
10171018
return Some(Ok((tok_start, Tok::Ellipsis, tok_end)));
10181019
} else {

tests/snippets/ellipsis.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
a = ...
4+
b = ...
5+
c = type(a)() # Test singleton behavior
6+
7+
assert a is b
8+
assert b is c

vm/src/obj/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod objbytes;
66
pub mod objcode;
77
pub mod objcomplex;
88
pub mod objdict;
9+
pub mod objellipsis;
910
pub mod objenumerate;
1011
pub mod objfilter;
1112
pub mod objfloat;

vm/src/obj/objellipsis.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::pyobject::{PyContext, PyFuncArgs, PyResult, TypeProtocol};
2+
use crate::vm::VirtualMachine;
3+
4+
pub fn init(context: &PyContext) {
5+
let ellipsis_type = &context.ellipsis_type;
6+
context.set_attr(ellipsis_type, "__new__", context.new_rustfunc(ellipsis_new));
7+
context.set_attr(
8+
ellipsis_type,
9+
"__repr__",
10+
context.new_rustfunc(ellipsis_repr),
11+
);
12+
}
13+
14+
fn ellipsis_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
15+
arg_check!(vm, args, required = [(_cls, None)]);
16+
Ok(vm.ctx.ellipsis())
17+
}
18+
19+
fn ellipsis_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
20+
arg_check!(vm, args, required = [(_cls, None)]);
21+
Ok(vm.new_str("Ellipsis".to_string()))
22+
}

vm/src/pyobject.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::obj::objbytes;
1919
use crate::obj::objcode;
2020
use crate::obj::objcomplex::{self, PyComplex};
2121
use crate::obj::objdict;
22+
use crate::obj::objellipsis;
2223
use crate::obj::objenumerate;
2324
use crate::obj::objfilter;
2425
use crate::obj::objfloat::{self, PyFloat};
@@ -115,6 +116,7 @@ pub struct PyContext {
115116
pub classmethod_type: PyObjectRef,
116117
pub code_type: PyObjectRef,
117118
pub dict_type: PyObjectRef,
119+
pub ellipsis_type: PyObjectRef,
118120
pub enumerate_type: PyObjectRef,
119121
pub filter_type: PyObjectRef,
120122
pub float_type: PyObjectRef,
@@ -208,6 +210,7 @@ impl PyContext {
208210
let bytearray_type = create_type("bytearray", &type_type, &object_type, &dict_type);
209211
let tuple_type = create_type("tuple", &type_type, &object_type, &dict_type);
210212
let iter_type = create_type("iter", &type_type, &object_type, &dict_type);
213+
let ellipsis_type = create_type("EllipsisType", &type_type, &object_type, &dict_type);
211214
let enumerate_type = create_type("enumerate", &type_type, &object_type, &dict_type);
212215
let filter_type = create_type("filter", &type_type, &object_type, &dict_type);
213216
let map_type = create_type("map", &type_type, &object_type, &dict_type);
@@ -224,11 +227,7 @@ impl PyContext {
224227
create_type("NoneType", &type_type, &object_type, &dict_type),
225228
);
226229

227-
// TODO: implement proper ellipsis class?
228-
let ellipsis = PyObject::new(
229-
PyObjectPayload::None,
230-
create_type("EllipsisType", &type_type, &object_type, &dict_type),
231-
);
230+
let ellipsis = PyObject::new(PyObjectPayload::None, ellipsis_type.clone());
232231

233232
let not_implemented = PyObject::new(
234233
PyObjectPayload::NotImplemented,
@@ -264,6 +263,7 @@ impl PyContext {
264263
false_value,
265264
tuple_type,
266265
iter_type,
266+
ellipsis_type,
267267
enumerate_type,
268268
filter_type,
269269
map_type,
@@ -308,6 +308,7 @@ impl PyContext {
308308
objsuper::init(&context);
309309
objtuple::init(&context);
310310
objiter::init(&context);
311+
objellipsis::init(&context);
311312
objenumerate::init(&context);
312313
objfilter::init(&context);
313314
objmap::init(&context);

vm/src/stdlib/ast.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,7 @@ fn expression_to_ast(ctx: &PyContext, expression: &ast::Expression) -> PyObjectR
395395

396396
node
397397
}
398-
ast::Expression::Ellipsis => {
399-
create_node(ctx, "Ellipsis")
400-
}
398+
ast::Expression::Ellipsis => create_node(ctx, "Ellipsis"),
401399
ast::Expression::List { elements } => {
402400
let node = create_node(ctx, "List");
403401

0 commit comments

Comments
 (0)