Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit05929b3

Browse files
committed
Add ellipsis type, test and do rustfmt.
1 parente25742f commit05929b3

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

‎parser/src/ast.rs‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 2 additions & 1 deletion
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
iflet(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
returnSome(Ok((tok_start,Tok::Ellipsis, tok_end)));
10181019
}else{

‎tests/snippets/ellipsis.py‎

Lines changed: 8 additions & 0 deletions
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+
assertaisb
8+
assertbisc

‎vm/src/obj/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod objbytes;
66
pubmod objcode;
77
pubmod objcomplex;
88
pubmod objdict;
9+
pubmod objellipsis;
910
pubmod objenumerate;
1011
pubmod objfilter;
1112
pubmod objfloat;

‎vm/src/obj/objellipsis.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
usecrate::pyobject::{PyContext,PyFuncArgs,PyResult,TypeProtocol};
2+
usecrate::vm::VirtualMachine;
3+
4+
pubfninit(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+
fnellipsis_new(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
15+
arg_check!(vm, args, required =[(_cls,None)]);
16+
Ok(vm.ctx.ellipsis())
17+
}
18+
19+
fnellipsis_repr(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
20+
arg_check!(vm, args, required =[(_cls,None)]);
21+
Ok(vm.new_str("Ellipsis".to_string()))
22+
}

‎vm/src/pyobject.rs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::obj::objbytes;
1919
usecrate::obj::objcode;
2020
usecrate::obj::objcomplex::{self,PyComplex};
2121
usecrate::obj::objdict;
22+
usecrate::obj::objellipsis;
2223
usecrate::obj::objenumerate;
2324
usecrate::obj::objfilter;
2425
usecrate::obj::objfloat::{self,PyFloat};
@@ -115,6 +116,7 @@ pub struct PyContext {
115116
pubclassmethod_type:PyObjectRef,
116117
pubcode_type:PyObjectRef,
117118
pubdict_type:PyObjectRef,
119+
pubellipsis_type:PyObjectRef,
118120
pubenumerate_type:PyObjectRef,
119121
pubfilter_type:PyObjectRef,
120122
pubfloat_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‎

Lines changed: 1 addition & 3 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp