Fix writing pairs
This commit is contained in:
		
							parent
							
								
									c3a2247fdd
								
							
						
					
					
						commit
						d41fe37d6a
					
				
					 1 changed files with 18 additions and 13 deletions
				
			
		| 
						 | 
					@ -17,7 +17,7 @@ use std::fmt;
 | 
				
			||||||
use std::ops::Deref;
 | 
					use std::ops::Deref;
 | 
				
			||||||
use number::Number;
 | 
					use number::Number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug, PartialEq)]
 | 
				
			||||||
pub enum ObjectPtr {
 | 
					pub enum ObjectPtr {
 | 
				
			||||||
    /// Absence of a value. A null pointer.
 | 
					    /// Absence of a value. A null pointer.
 | 
				
			||||||
    Null,
 | 
					    Null,
 | 
				
			||||||
| 
						 | 
					@ -25,7 +25,7 @@ pub enum ObjectPtr {
 | 
				
			||||||
    Ptr(Box<Object>),
 | 
					    Ptr(Box<Object>),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug, PartialEq)]
 | 
				
			||||||
pub enum Object {
 | 
					pub enum Object {
 | 
				
			||||||
    Bool(bool),
 | 
					    Bool(bool),
 | 
				
			||||||
    ByteVector(Vec<u8>),
 | 
					    ByteVector(Vec<u8>),
 | 
				
			||||||
| 
						 | 
					@ -67,20 +67,13 @@ impl fmt::Display for Object {
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Object::Number(ref n) => {
 | 
					            Object::Number(ref n) => {
 | 
				
			||||||
                write!(f, "{}", n)
 | 
					                // TODO: Implement Display for Number
 | 
				
			||||||
 | 
					                write!(f, "{:?}", n)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Object::Pair(ref car, ref cdr) => {
 | 
					            Object::Pair(ref car, ref cdr) => {
 | 
				
			||||||
                // TODO: There are rules for printing pairs...
 | 
					                write!(f, "(").and_then(|_| Object::fmt_pair(car, cdr, f))
 | 
				
			||||||
                // Print a dot before the cdr iff it's anything but Null or another Pair.
 | 
					                              .and_then(|_| write!(f, ")"))
 | 
				
			||||||
                // Going to need a recursive helper to avoid printing ( and ) for every pair.
 | 
					 | 
				
			||||||
                write!(f, "({}", car).and_then(|_| match cdr {
 | 
					 | 
				
			||||||
                    &ObjectPtr::Null => write!(f, ")"),
 | 
					 | 
				
			||||||
                    &ObjectPtr::Ptr(ref ptr) => match ptr.deref() {
 | 
					 | 
				
			||||||
                        &Object::Pair(_, _) => write!(f, "{}", ptr),
 | 
					 | 
				
			||||||
                        _ => write!(f, " . {})", ptr)
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Object::String(ref st) => {
 | 
					            Object::String(ref st) => {
 | 
				
			||||||
| 
						 | 
					@ -98,3 +91,15 @@ impl fmt::Display for Object {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Object {
 | 
				
			||||||
 | 
					    fn fmt_pair(car: &ObjectPtr, cdr: &ObjectPtr, f: &mut fmt::Formatter) -> fmt::Result {
 | 
				
			||||||
 | 
					        write!(f, "{}", car).and_then(|r| match cdr {
 | 
				
			||||||
 | 
					            &ObjectPtr::Null => Ok(r),  // Don't write anything.
 | 
				
			||||||
 | 
					            &ObjectPtr::Ptr(ref ptr) => match ptr.deref() {
 | 
				
			||||||
 | 
					                &Object::Pair(ref next_car, ref next_cdr) => Object::fmt_pair(next_car, next_cdr, f),
 | 
				
			||||||
 | 
					                _ => write!(f, " . {}", ptr)
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue