Skip to content

Commit babb8ed

Browse files
committed
Deleted comparison based on objectkind
1 parent c742909 commit babb8ed

File tree

7 files changed

+160
-151
lines changed

7 files changed

+160
-151
lines changed

parser/src/python.lalrpop

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -416,17 +416,17 @@ WithItem: ast::WithItem = {
416416
};
417417

418418
FuncDef: ast::LocatedStatement = {
419-
<d:Decorator*> <loc:@L> "def" <i:Identifier> <a:Parameters> ":" <s:Suite> => {
420-
ast::LocatedStatement {
421-
location: loc,
422-
node: ast::Statement::FunctionDef {
423-
name: i,
424-
args: a,
425-
body: s,
426-
decorator_list: d,
427-
}
428-
}
429-
},
419+
<d:Decorator*> <loc:@L> "def" <i:Identifier> <a:Parameters> ":" <s:Suite> => {
420+
ast::LocatedStatement {
421+
location: loc,
422+
node: ast::Statement::FunctionDef {
423+
name: i,
424+
args: a,
425+
body: s,
426+
decorator_list: d,
427+
}
428+
}
429+
},
430430
};
431431

432432
Parameters: ast::Parameters = {
@@ -648,28 +648,38 @@ Expression: ast::Expression = {
648648
};
649649

650650
XorExpression: ast::Expression = {
651-
<e1:XorExpression> "^" <e2:AndExpression> => ast::Expression::Binop { a: Box::new(e1), op: ast::Operator::BitXor, b: Box::new(e2) },
652-
<e:AndExpression> => e,
651+
<e1:XorExpression> "^" <e2:AndExpression> => ast::Expression::Binop { a: Box::new(e1), op: ast::Operator::BitXor, b: Box::new(e2) },
652+
AndExpression,
653653
};
654654

655655
AndExpression: ast::Expression = {
656-
<e1:AndExpression> "&" <e2:ArithmaticExpression> => ast::Expression::Binop { a: Box::new(e1), op: ast::Operator::BitAnd, b: Box::new(e2) },
657-
<e:ArithmaticExpression> => e,
656+
<e1:AndExpression> "&" <e2:ShiftExpression> => ast::Expression::Binop { a: Box::new(e1), op: ast::Operator::BitAnd, b: Box::new(e2) },
657+
ShiftExpression,
658+
};
659+
660+
ShiftExpression: ast::Expression = {
661+
<e1:ShiftExpression> <op:ShiftOp> <e2:ArithmaticExpression> => ast::Expression::Binop { a: Box::new(e1), op: op, b: Box::new(e2) },
662+
ArithmaticExpression,
663+
};
664+
665+
ShiftOp: ast::Operator = {
666+
"<<" => ast::Operator::LShift,
667+
">>" => ast::Operator::RShift,
658668
};
659669

660670
ArithmaticExpression: ast::Expression = {
661-
<a:ArithmaticExpression> <op:AddOp> <b:Term> => ast::Expression::Binop { a: Box::new(a), op: op, b: Box::new(b) },
662-
Term,
671+
<a:ArithmaticExpression> <op:AddOp> <b:Term> => ast::Expression::Binop { a: Box::new(a), op: op, b: Box::new(b) },
672+
Term,
663673
};
664674

665675
AddOp: ast::Operator = {
666-
"+" => ast::Operator::Add,
667-
"-" => ast::Operator::Sub,
676+
"+" => ast::Operator::Add,
677+
"-" => ast::Operator::Sub,
668678
};
669679

670680
Term: ast::Expression = {
671-
<a:Term> <op:MulOp> <b:Factor> => ast::Expression::Binop { a: Box::new(a), op: op, b: Box::new(b) },
672-
Factor,
681+
<a:Term> <op:MulOp> <b:Factor> => ast::Expression::Binop { a: Box::new(a), op: op, b: Box::new(b) },
682+
Factor,
673683
};
674684

675685
MulOp: ast::Operator = {
@@ -687,12 +697,12 @@ Factor: ast::Expression = {
687697
};
688698

689699
Power: ast::Expression = {
690-
<e:AtomExpr> <e2:("**" Factor)?> => {
691-
match e2 {
692-
None => e,
693-
Some(x) => ast::Expression::Binop { a: Box::new(e), op: ast::Operator::Pow, b: Box::new(x.1) },
694-
}
695-
}
700+
<e:AtomExpr> <e2:("**" Factor)?> => {
701+
match e2 {
702+
None => e,
703+
Some(x) => ast::Expression::Binop { a: Box::new(e), op: ast::Operator::Pow, b: Box::new(x.1) },
704+
}
705+
}
696706
};
697707

698708
AtomExpr: ast::Expression = {
@@ -703,57 +713,57 @@ AtomExpr: ast::Expression = {
703713
};
704714

705715
Subscript: ast::Expression = {
706-
<e:Test> => e,
707-
<e1:Test?> ":" <e2:Test?> <e3:SliceOp?> => {
708-
let s1 = e1.unwrap_or(ast::Expression::None);
709-
let s2 = e2.unwrap_or(ast::Expression::None);
710-
let s3 = e3.unwrap_or(ast::Expression::None);
711-
ast::Expression::Slice { elements: vec![s1, s2, s3] }
712-
}
716+
<e:Test> => e,
717+
<e1:Test?> ":" <e2:Test?> <e3:SliceOp?> => {
718+
let s1 = e1.unwrap_or(ast::Expression::None);
719+
let s2 = e2.unwrap_or(ast::Expression::None);
720+
let s3 = e3.unwrap_or(ast::Expression::None);
721+
ast::Expression::Slice { elements: vec![s1, s2, s3] }
722+
}
713723
};
714724

715725
SliceOp: ast::Expression = {
716726
":" <e:Test?> => e.unwrap_or(ast::Expression::None)
717727
}
718728

719729
Atom: ast::Expression = {
720-
<s:String> => ast::Expression::String { value: s },
721-
<n:Number> => ast::Expression::Number { value: n },
722-
<i:Identifier> => ast::Expression::Identifier { name: i },
723-
"[" <e:TestListComp?> "]" => {
730+
<s:String> => ast::Expression::String { value: s },
731+
<n:Number> => ast::Expression::Number { value: n },
732+
<i:Identifier> => ast::Expression::Identifier { name: i },
733+
"[" <e:TestListComp?> "]" => {
724734
let elements = e.unwrap_or(Vec::new());
725735
ast::Expression::List { elements }
726-
},
727-
"[" <e:TestListComp2> "]" => {
736+
},
737+
"[" <e:TestListComp2> "]" => {
728738
// List comprehension:
729739
e
730-
},
731-
"(" <e:TestList?> <trailing_comma:","?> ")" => {
732-
match e {
733-
None => ast::Expression::Tuple { elements: Vec::new() },
734-
Some(elements) => {
735-
if elements.len() == 1 && trailing_comma.is_none() {
736-
// This is "(e)", which is equivalent to "e"
737-
elements.into_iter().next().unwrap()
738-
} else {
739-
ast::Expression::Tuple { elements }
740+
},
741+
"(" <e:TestList?> <trailing_comma:","?> ")" => {
742+
match e {
743+
None => ast::Expression::Tuple { elements: Vec::new() },
744+
Some(elements) => {
745+
if elements.len() == 1 && trailing_comma.is_none() {
746+
// This is "(e)", which is equivalent to "e"
747+
elements.into_iter().next().unwrap()
748+
} else {
749+
ast::Expression::Tuple { elements }
750+
}
740751
}
741752
}
742-
}
743-
},
744-
"(" <e:Test> <c:CompFor> ")" => {
753+
},
754+
"(" <e:Test> <c:CompFor> ")" => {
745755
ast::Expression::Comprehension {
746756
kind: Box::new(ast::ComprehensionKind::GeneratorExpression { element: e }),
747757
generators: c,
748758
}
749-
},
750-
"{" <e:TestDict?> "}" => ast::Expression::Dict { elements: e.unwrap_or(Vec::new()) },
751-
"{" <e:TestDictComp> "}" => e,
752-
"{" <e:TestSet> "}" => ast::Expression::Set { elements: e },
753-
"{" <e:TestSetComp> "}" => e,
754-
"True" => ast::Expression::True,
755-
"False" => ast::Expression::False,
756-
"None" => ast::Expression::None,
759+
},
760+
"{" <e:TestDict?> "}" => ast::Expression::Dict { elements: e.unwrap_or(Vec::new()) },
761+
"{" <e:TestDictComp> "}" => e,
762+
"{" <e:TestSet> "}" => ast::Expression::Set { elements: e },
763+
"{" <e:TestSetComp> "}" => e,
764+
"True" => ast::Expression::True,
765+
"False" => ast::Expression::False,
766+
"None" => ast::Expression::None,
757767
};
758768

759769
TestListComp: Vec<ast::Expression> = {
@@ -971,6 +981,8 @@ extern {
971981
"//" => lexer::Tok::DoubleSlash,
972982
"^" => lexer::Tok::CircumFlex,
973983
"|" => lexer::Tok::Vbar,
984+
"<<" => lexer::Tok::LeftShift,
985+
">>" => lexer::Tok::RightShift,
974986
"/" => lexer::Tok::Slash,
975987
"(" => lexer::Tok::Lpar,
976988
")" => lexer::Tok::Rpar,

tests/snippets/generators.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ def make_numbers():
1212

1313
assert r == [1, 2, 42, 3]
1414

15+
r = list(x for x in [1, 2, 3])
16+
assert r == [1, 2, 3]

vm/src/builtins.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,12 @@ fn builtin_min(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
354354
required = [(x, Some(vm.ctx.int_type())), (y, Some(vm.ctx.int_type()))]
355355
);
356356

357-
use std::cmp::Ordering;
358-
359-
let order = x.cmp(y);
357+
let order = vm.call_method(x, "__gt__", vec![y.clone()])?;
360358

361-
match order {
362-
Ordering::Less | Ordering::Equal => Ok(x.clone()),
363-
_ => Ok(y.clone()),
359+
if objbool::get_value(&order) {
360+
Ok(y.clone())
361+
} else {
362+
Ok(x.clone())
364363
}
365364
}
366365

vm/src/compile.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ impl Compiler {
997997
vec![],
998998
None,
999999
self.source_path.clone(),
1000-
name,
1000+
name.clone(),
10011001
));
10021002

10031003
// Create empty object of proper type:
@@ -1047,7 +1047,9 @@ impl Compiler {
10471047
match kind {
10481048
ast::ComprehensionKind::GeneratorExpression { element } => {
10491049
self.compile_expression(element)?;
1050+
self.mark_generator();
10501051
self.emit(Instruction::YieldValue);
1052+
self.emit(Instruction::Pop);
10511053
}
10521054
ast::ComprehensionKind::List { element } => {
10531055
self.compile_expression(element)?;
@@ -1095,9 +1097,7 @@ impl Compiler {
10951097

10961098
// List comprehension function name:
10971099
self.emit(Instruction::LoadConst {
1098-
value: bytecode::Constant::String {
1099-
value: String::from("<listcomp>"),
1100-
},
1100+
value: bytecode::Constant::String { value: name },
11011101
});
11021102

11031103
// Turn code object into function object:

vm/src/frame.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ impl Frame {
336336
bytecode::Instruction::YieldValue => {
337337
let value = self.pop_value();
338338
Some(Ok(ExecutionResult::Yield(value)))
339-
// unimplemented!("TODO: implement generators: {:?}", value);
340339
}
341340
bytecode::Instruction::SetupLoop { start, end } => {
342341
self.push_block(Block::Loop {
@@ -902,14 +901,21 @@ impl Frame {
902901
&bytecode::BinaryOperator::Subtract => vm._sub(a_ref, b_ref),
903902
&bytecode::BinaryOperator::Add => vm._add(a_ref, b_ref),
904903
&bytecode::BinaryOperator::Multiply => vm._mul(a_ref, b_ref),
904+
&bytecode::BinaryOperator::MatrixMultiply => {
905+
vm.call_method(&a_ref, "__matmul__", vec![b_ref])
906+
}
905907
&bytecode::BinaryOperator::Power => vm._pow(a_ref, b_ref),
906908
&bytecode::BinaryOperator::Divide => vm._div(a_ref, b_ref),
909+
&bytecode::BinaryOperator::FloorDivide => {
910+
vm.call_method(&a_ref, "__floordiv__", vec![b_ref])
911+
}
907912
&bytecode::BinaryOperator::Subscript => self.subscript(vm, a_ref, b_ref),
908913
&bytecode::BinaryOperator::Modulo => vm._modulo(a_ref, b_ref),
914+
&bytecode::BinaryOperator::Lshift => vm.call_method(&a_ref, "__lshift__", vec![b_ref]),
915+
&bytecode::BinaryOperator::Rshift => vm.call_method(&a_ref, "__rshift__", vec![b_ref]),
909916
&bytecode::BinaryOperator::Xor => vm._xor(a_ref, b_ref),
910917
&bytecode::BinaryOperator::Or => vm._or(a_ref, b_ref),
911918
&bytecode::BinaryOperator::And => vm._and(a_ref, b_ref),
912-
_ => panic!("NOT IMPL {:?}", op),
913919
};
914920
match result {
915921
Ok(value) => {
@@ -960,35 +966,19 @@ impl Frame {
960966
}
961967

962968
fn _lt(&mut self, vm: &mut VirtualMachine, a: PyObjectRef, b: PyObjectRef) -> PyResult {
963-
let b2 = &*b.borrow();
964-
let a2 = &*a.borrow();
965-
let result_bool = a2 < b2;
966-
let result = vm.ctx.new_bool(result_bool);
967-
Ok(result)
969+
vm.call_method(&a, "__lt__", vec![b])
968970
}
969971

970972
fn _le(&mut self, vm: &mut VirtualMachine, a: PyObjectRef, b: PyObjectRef) -> PyResult {
971-
let b2 = &*b.borrow();
972-
let a2 = &*a.borrow();
973-
let result_bool = a2 <= b2;
974-
let result = vm.ctx.new_bool(result_bool);
975-
Ok(result)
973+
vm.call_method(&a, "__le__", vec![b])
976974
}
977975

978976
fn _gt(&mut self, vm: &mut VirtualMachine, a: PyObjectRef, b: PyObjectRef) -> PyResult {
979-
let b2 = &*b.borrow();
980-
let a2 = &*a.borrow();
981-
let result_bool = a2 > b2;
982-
let result = vm.ctx.new_bool(result_bool);
983-
Ok(result)
977+
vm.call_method(&a, "__gt__", vec![b])
984978
}
985979

986980
fn _ge(&mut self, vm: &mut VirtualMachine, a: PyObjectRef, b: PyObjectRef) -> PyResult {
987-
let b2 = &*b.borrow();
988-
let a2 = &*a.borrow();
989-
let result_bool = a2 >= b2;
990-
let result = vm.ctx.new_bool(result_bool);
991-
Ok(result)
981+
vm.call_method(&a, "__ge__", vec![b])
992982
}
993983

994984
fn _id(&self, a: PyObjectRef) -> usize {

0 commit comments

Comments
 (0)