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
//! `and` - builtin operator function
//!
//! CPython does some special casing around logical operators with test-and-jump opcodes.
//! rsnek just declares them to be binop methods for now.
//!
//! As per CPython, the `and` logical operator returns the reference to the object
//! that first test `False` in the expression or the last one to test `True`.
use std::borrow::Borrow;

use ::api::method::BooleanCast;
use ::api::result::ObjectResult;
use ::api::RtObject as ObjectRef;
use ::modules::builtins::Type;
use ::runtime::Runtime;
use ::runtime::traits::BooleanProvider;


pub fn logical_and<'a>(rt: &Runtime, lhs: &ObjectRef, rhs: &ObjectRef) -> ObjectResult {
    match lhs.op_bool(rt) {
        Ok(object) => {
            if object == rt.bool(false) {
                return Ok(lhs.clone())
            }
        },
        Err(err) => return Err(err)
    };

    Ok(rhs.clone())
}