/// Searches for an object in the locals and globals provided. 
/// If found, returns a mutable reference to the object.
/// If not found, throws an error and returns None. 
Searches for an object in the locals and globals provided.
If found, returns a mutable reference to the object.
If not found, throws an error and returns None.
Searches for an object in the locals and globals provided . If found returns a mutable reference to the object . If not found throws an error and returns None .
fn find_mut_ref<'a>(
    id: &usize, 
    locals: &'a mut HashMap<usize, Object>, 
    globals: &'a mut HashMap<usize, Object>,
    identifiers: &HashMap<usize, String>
) -> Option<&'a mut Object> {
    // This is rearranged to avoid borrowing locals/globals as mutable twice.
    // Instead of doing that, we check for a Reference, and overwrite the 
    // current object ID to the ID being pointed to and call this function again appropriately.
    let (referenced, glob, ref_id) = if let Some(obj) = locals.get_mut(id) {
        if let Type::Reference(reference) = obj.obj_type {
            (true, false, reference.pointer)
        }
        else {
            (false, false, *id)
        }
    }
    else if let Some(obj) = globals.get_mut(id) {
        if let Type::Reference(reference) = obj.obj_type {
            (true, true, reference.pointer)
        }
        else {
            (false, true, *id)
        }
    }
    else {
        // Not found
        throw_error(
            ErrorType::ObjectNotDefinedError, 
            format!("Object {} is not defined in the local or global scopes", id),
            Some((&[*id], identifiers))
        );
        return None;
    };
    // Evaluate references
    if referenced {
        find_mut_ref(&ref_id, locals, globals, identifiers)
    }
    else {
        // Get the object normally
        if glob {
            globals.get_mut(&ref_id)
        }
        else {
            locals.get_mut(&ref_id)
        }
    }
}
fn find_mut_ref < ' a > ( id : & usize , locals : & ' a mut HashMap < usize , Object > , globals : & ' a mut HashMap < usize , Object > , identifiers : & HashMap < usize , String > ) -> Option < & ' a mut Object > { // This is rearranged to avoid borrowing locals/globals as mutable twice. // Instead of doing that, we check for a Reference, and overwrite the  // current object ID to the ID being pointed to and call this function again appropriately. let ( referenced , glob , ref_id ) = if let Some ( obj ) = locals . get_mut ( id ) { if let Type :: Reference ( reference ) = obj . obj_type { ( true , false , reference . pointer ) } else { ( false , false , * id ) } } else if let Some ( obj ) = globals . get_mut ( id ) { if let Type :: Reference ( reference ) = obj . obj_type { ( true , true , reference . pointer ) } else { ( false , true , * id ) } } else { // Not found throw_error ( ErrorType :: ObjectNotDefinedError , format ! ( " " id ) , Some ( ( & [ * id ] , identifiers ) ) ) ; return None ; } ; // Evaluate references if referenced { find_mut_ref ( & ref_id , locals , globals , identifiers ) } else { // Get the object normally if glob { globals . get_mut ( & ref_id ) } else { locals . get_mut ( & ref_id ) } } }
Searches for an object in the locals and globals provided.
Searches for an object in the locals and globals provided .
id
&usize
locals
&'a mut HashMap<usize, Object>
globals
&'a mut HashMap<usize, Object>
identifiers
&HashMap<usize, String>