[types] Clean up Pair and Sym types

Reconfigure the top-level object types.
- Obj is an enum pointer type
- Object is a trait that all Scheme types should implement

Define Sym, a symbol type.
Define Pair, a pair/cons/list type.

Implement Display for all types above. Implement casting methods for the above.
This commit is contained in:
Eryn Wells 2018-08-23 17:05:29 -07:00
parent f197f1ba8b
commit d825d0ec8a
4 changed files with 181 additions and 104 deletions

41
types/src/pair.rs Normal file
View file

@ -0,0 +1,41 @@
/* types/src/pair.rs
* Eryn Wells <eryn@erynwells.me>
*/
use std::any::Any;
use std::fmt;
use super::*;
use object::Object;
pub struct Pair {
car: Obj,
cdr: Obj
}
impl Pair {
fn fmt_pair(&self, f: &mut fmt::Formatter) -> fmt::Result {
let r = write!(f, "{}", self.car);
r.and_then(|r| match self.cdr {
Obj::Null => Ok(r), // Don't write anything.
Obj::Ptr(ref next) => {
match next.as_pair() {
Some(next_pair) => write!(f, " ").and_then(|_| next_pair.fmt_pair(f)),
None => write!(f, " . {}", next)
}
}
})
}
}
impl Object for Pair {
fn as_any(&self) -> &Any { self }
fn as_pair(&self) -> Option<&Pair> { Some(self) }
fn as_sym(&self) -> Option<&Sym> { None }
}
impl fmt::Display for Pair {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(").and_then(|_| self.fmt_pair(f))
.and_then(|_| write!(f, ")"))
}
}