significant refactor and simplification

This commit is contained in:
Ava Hahn 2023-02-17 21:00:07 -08:00
parent ca4c557d95
commit 7555a90328
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
8 changed files with 501 additions and 422 deletions

View file

@ -20,13 +20,13 @@ use std::ops::Index;
// Container
#[derive(Debug, Default)]
pub enum Ctr<'a> {
pub enum Ctr {
Symbol(String),
String(String),
Integer(i128),
Float(f64),
Bool(bool),
Seg(Seg<'a>),
Seg(Seg),
#[default]
None,
}
@ -49,29 +49,29 @@ pub enum Type {
* I was going to call it Cell and then I learned about
* how important RefCells were in Rust
*/
#[derive(Debug)]
pub struct Seg<'a> {
#[derive(Debug, Default)]
pub struct Seg {
/* "Contents of Address Register"
* Historical way of referring to the first value in a cell.
*/
pub car: Box<Ctr<'a>>,
pub car: Box<Ctr>,
/* "Contents of Decrement Register"
* Historical way of referring to the second value in a cell.
*/
pub cdr: Box<Ctr<'a>>,
pub cdr: Box<Ctr>,
/* Stupid hack that makes rust look foolish.
* Needed to determine variance of lifetime.
* How this is an acceptable solution I have
* not a single clue.
*/
_lifetime_variance_determinant: PhantomData<&'a ()>
_lifetime_variance_determinant: PhantomData<()>
}
static NOTHING: Ctr = Ctr::None;
impl Ctr<'_> {
impl Ctr {
pub fn to_type(&self) -> Type {
match self {
Ctr::Symbol(_s) => Type::Symbol,
@ -86,14 +86,14 @@ impl Ctr<'_> {
}
impl<'a> Seg<'a> {
impl Seg {
/* recurs over tree assumed to be list in standard form
* appends object to end of list
*
* TODO: figure out how not to call CLONE on a CTR via obj arg
* TODO: return result
*/
pub fn append<'b>(&mut self, obj: Box<Ctr<'a>>) {
pub fn append(&mut self, obj: Box<Ctr>) {
if let Ctr::None = &*(self.car) {
self.car = obj;
return
@ -127,16 +127,16 @@ impl<'a> Seg<'a> {
}
}
pub fn from_mono(arg: Box<Ctr<'a>>) -> Seg<'a> {
return Seg{
pub fn from_mono(arg: Box<Ctr>) -> Seg {
Seg {
car: arg,
cdr: Box::new(Ctr::None),
_lifetime_variance_determinant: PhantomData,
}
}
pub fn from(car: Box<Ctr<'a>>, cdr: Box<Ctr<'a>>) -> Seg<'a> {
return Seg{
pub fn from(car: Box<Ctr>, cdr: Box<Ctr>) -> Seg {
Seg {
car,
cdr,
_lifetime_variance_determinant: PhantomData,
@ -152,8 +152,12 @@ impl<'a> Seg<'a> {
len
}
pub fn new() -> Seg<'a> {
return Seg{
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn new() -> Seg {
Seg {
car: Box::new(Ctr::None),
cdr: Box::new(Ctr::None),
_lifetime_variance_determinant: PhantomData,
@ -170,7 +174,7 @@ fn seg_to_string(s: &Seg, parens: bool) -> String {
}
string.push(' ');
match &*(s.cdr) {
Ctr::Seg(inner) => string.push_str(&seg_to_string(&inner, false)),
Ctr::Seg(inner) => string.push_str(&seg_to_string(inner, false)),
Ctr::None => {string.pop();},
_ => string.push_str(&s.cdr.to_string()),
}
@ -179,9 +183,9 @@ fn seg_to_string(s: &Seg, parens: bool) -> String {
string
}
impl<'a> Clone for Seg<'a> {
fn clone(&self) -> Seg<'a> {
return Seg{
impl Clone for Seg {
fn clone(&self) -> Seg {
Seg {
car: self.car.clone(),
cdr: self.cdr.clone(),
_lifetime_variance_determinant: PhantomData,
@ -189,8 +193,8 @@ impl<'a> Clone for Seg<'a> {
}
}
impl<'a> Index<usize> for Seg<'a> {
type Output = Ctr<'a>;
impl Index<usize> for Seg {
type Output = Ctr;
fn index(&self, idx: usize) -> &Self::Output {
if idx == 0 {
@ -201,25 +205,25 @@ impl<'a> Index<usize> for Seg<'a> {
return s.index(idx - 1)
}
return &NOTHING;
&NOTHING
}
}
impl<'a> Clone for Ctr<'a> {
fn clone(&self) -> Ctr<'a> {
impl Clone for Ctr {
fn clone(&self) -> Ctr {
match self {
Ctr::Symbol(s) => Ctr::Symbol(s.clone()),
Ctr::String(s) => Ctr::String(s.clone()),
Ctr::Integer(s) => Ctr::Integer(s.clone()),
Ctr::Float(s) => Ctr::Float(s.clone()),
Ctr::Bool(s) => Ctr::Bool(s.clone()),
Ctr::Integer(s) => Ctr::Integer(*s),
Ctr::Float(s) => Ctr::Float(*s),
Ctr::Bool(s) => Ctr::Bool(*s),
Ctr::Seg(s) => Ctr::Seg(s.clone()),
Ctr::None => Ctr::None,
}
}
}
impl fmt::Display for Ctr<'_> {
impl fmt::Display for Ctr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Ctr::Symbol(s) => write!(f, "{}", s),
@ -239,25 +243,38 @@ impl fmt::Display for Ctr<'_> {
}
}
impl fmt::Display for Seg<'_> {
impl fmt::Display for Seg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", seg_to_string(self, true))
}
}
impl Type {
pub fn to_string(&self) -> String {
let ret: &str;
match self {
Type::Symbol => ret = "symbol",
Type::String => ret = "string",
Type::Integer => ret = "integer",
Type::Float => ret = "float",
Type::Bool => ret = "bool",
Type::Seg => ret = "segment",
Type::None => ret = "none",
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ret: &str = match self {
Type::Symbol => "symbol",
Type::String => "string",
Type::Integer => "integer",
Type::Float => "float",
Type::Bool => "bool",
Type::Seg => "segment",
Type::None => "none",
};
ret.to_owned()
write!(f, "{}", ret)
}
}
impl std::convert::From<String> for Type {
fn from(value: String) -> Self {
match value.as_str() {
"symbol" => Type::Symbol,
"string" => Type::String,
"integer" => Type::Integer,
"float" => Type::Float,
"bool" => Type::Bool,
"segment" => Type::Seg,
_ => Type::None,
}
}
}