From ddfac28b8cf264fd0426e7cbbcb54e8973a97552 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 3 Apr 2017 14:51:39 -0400 Subject: [PATCH] Rearrange the types modules a bunch --- src/types/bool.rs | 25 +++++++++++++++ src/types/char.rs | 26 +++++++++++++++ src/types/mod.rs | 78 ++++++++------------------------------------- src/types/object.rs | 15 +++++++++ src/types/value.rs | 24 ++++++++++++++ 5 files changed, 103 insertions(+), 65 deletions(-) create mode 100644 src/types/bool.rs create mode 100644 src/types/char.rs create mode 100644 src/types/object.rs create mode 100644 src/types/value.rs diff --git a/src/types/bool.rs b/src/types/bool.rs new file mode 100644 index 0000000..f4411ac --- /dev/null +++ b/src/types/bool.rs @@ -0,0 +1,25 @@ +/* types/bool.rs + * Eryn Wells + */ + +use std::any::Any; +use super::value::{Value, ValueEq}; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Bool(bool); + +impl Bool { + pub fn new(v: bool) -> Bool { Bool(v) } +} + +impl Value for Bool { + fn as_value(&self) -> &Value { self } +} + +impl ValueEq for Bool { + fn eq(&self, other: &Value) -> bool { + other.as_any().downcast_ref::().map_or(false, |x| x == self) + } + + fn as_any(&self) -> &Any { self } +} diff --git a/src/types/char.rs b/src/types/char.rs new file mode 100644 index 0000000..dda3905 --- /dev/null +++ b/src/types/char.rs @@ -0,0 +1,26 @@ +/* types/char.rs + * Eryn Wells + */ + +use std::any::Any; +use super::value::{Value, ValueEq}; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Char(char); + +impl Char { + pub fn new(v: char) -> Char { Char(v) } +} + +impl Value for Char { + fn as_value(&self) -> &Value { self } +} + +impl ValueEq for Char { + fn eq(&self, other: &Value) -> bool { + other.as_any().downcast_ref::().map_or(false, |x| x == self) + } + + fn as_any(&self) -> &Any { self } +} + diff --git a/src/types/mod.rs b/src/types/mod.rs index 8ff2160..44b9e86 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,68 +1,16 @@ -/* mod.rs +/* types/mod.rs * Eryn Wells */ -use std::fmt::Debug; -use std::any::Any; - +pub use self::bool::Bool; +pub use self::char::Char; pub use self::number::Number; +use self::value::Value; +pub mod bool; +pub mod char; pub mod number; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Boolean(bool); - -impl Boolean { - pub fn new(v: bool) -> Boolean { Boolean(v) } -} - -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Character(char); - -impl Character { - pub fn new(v: char) -> Character { Character(v) } -} - -pub trait Value: Debug + ValueEq { - fn as_value(&self) -> &Value; -} - -/// A trait on value types that makes it easier to compare values of disparate types. The methods -/// provided by this trait are used by the PartialEq implementation on Values. -pub trait ValueEq { - fn eq(&self, other: &Value) -> bool; - fn as_any(&self) -> &Any; -} - -impl<'lhs,'rhs> PartialEq for Value+'lhs { - fn eq(&self, other: &(Value+'rhs)) -> bool { - ValueEq::eq(self, other) - } -} - -impl Value for Boolean { - fn as_value(&self) -> &Value { self } -} - -impl ValueEq for Boolean { - fn eq(&self, other: &Value) -> bool { - other.as_any().downcast_ref::().map_or(false, |x| x == self) - } - - fn as_any(&self) -> &Any { self } -} - -impl Value for Character { - fn as_value(&self) -> &Value { self } -} - -impl ValueEq for Character { - fn eq(&self, other: &Value) -> bool { - other.as_any().downcast_ref::().map_or(false, |x| x == self) - } - - fn as_any(&self) -> &Any { self } -} +mod value; #[cfg(test)] mod tests { @@ -70,19 +18,19 @@ mod tests { #[test] fn booleans_are_equal() { - assert_eq!(Boolean(true), Boolean(true)); - assert_eq!(Boolean(false), Boolean(false)); - assert_ne!(Boolean(true), Boolean(false)); + assert_eq!(Bool(true), Bool(true)); + assert_eq!(Bool(false), Bool(false)); + assert_ne!(Bool(true), Bool(false)); } #[test] fn equal_chars_are_equal() { - assert_eq!(Character('a'), Character('a')); - assert_eq!(Character('a').as_value(), Character('a').as_value()); + assert_eq!(Char('a'), Char('a')); + assert_eq!(Char('a').as_value(), Char('a').as_value()); } #[test] fn booleans_and_chars_are_not_equal() { - assert_ne!(Boolean(true).as_value(), Character('a').as_value()); + assert_ne!(Bool(true).as_value(), Char('a').as_value()); } } diff --git a/src/types/object.rs b/src/types/object.rs new file mode 100644 index 0000000..be0f944 --- /dev/null +++ b/src/types/object.rs @@ -0,0 +1,15 @@ +/* types/object.rs + * Eryn Wells + */ + +/// `Object` is the top-level type in Scheme's world. It is abstract -- no value is of `Object` +/// type -- but all types must implement it. +pub trait Object: IsBool + IsChar { } + +pub trait IsBool { + fn is_bool() -> bool { false } +} + +pub trait isChar { + fn is_char() -> bool { false } +} diff --git a/src/types/value.rs b/src/types/value.rs new file mode 100644 index 0000000..c4e8423 --- /dev/null +++ b/src/types/value.rs @@ -0,0 +1,24 @@ +/* types/value.rs + * Eryn Wells + */ + +use std::fmt::Debug; +use std::any::Any; + +pub trait Value: Debug + ValueEq { + fn as_value(&self) -> &Value; +} + +/// A trait on value types that makes it easier to compare values of disparate types. The methods +/// provided by this trait are used by the PartialEq implementation on Values. +pub trait ValueEq { + fn eq(&self, other: &Value) -> bool; + fn as_any(&self) -> &Any; +} + +impl<'lhs,'rhs> PartialEq for Value+'lhs { + fn eq(&self, other: &(Value+'rhs)) -> bool { + ValueEq::eq(self, other) + } +} +