Merge pull request #1234 from RustPython/short-circuit-evaluation

Improve the situation regarding boolean operations.
This commit is contained in:
Windel Bouwman
2019-08-12 07:30:28 +02:00
committed by GitHub
8 changed files with 192 additions and 97 deletions

View File

@@ -496,7 +496,7 @@ impl Frame {
self.jump(*target);
Ok(None)
}
bytecode::Instruction::JumpIf { target } => {
bytecode::Instruction::JumpIfTrue { target } => {
let obj = self.pop_value();
let value = objbool::boolval(vm, obj)?;
if value {
@@ -514,6 +514,28 @@ impl Frame {
Ok(None)
}
bytecode::Instruction::JumpIfTrueOrPop { target } => {
let obj = self.last_value();
let value = objbool::boolval(vm, obj)?;
if value {
self.jump(*target);
} else {
self.pop_value();
}
Ok(None)
}
bytecode::Instruction::JumpIfFalseOrPop { target } => {
let obj = self.last_value();
let value = objbool::boolval(vm, obj)?;
if !value {
self.jump(*target);
} else {
self.pop_value();
}
Ok(None)
}
bytecode::Instruction::Raise { argc } => {
let cause = match argc {
2 => self.get_exception(vm, true)?,

View File

@@ -332,11 +332,8 @@ fn expression_to_ast(vm: &VirtualMachine, expression: &ast::Expression) -> PyRes
operand => expression_to_ast(vm, a)?,
})
}
BoolOp { a, op, b } => {
// Attach values:
let py_a = expression_to_ast(vm, a)?.into_object();
let py_b = expression_to_ast(vm, b)?.into_object();
let py_values = vm.ctx.new_tuple(vec![py_a, py_b]);
BoolOp { op, values } => {
let py_values = expressions_to_ast(vm, values)?;
let str_op = match op {
ast::BooleanOperator::And => "And",