flesh/src/segment.rs

264 lines
6.9 KiB
Rust
Raw Normal View History

/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use std::fmt;
use std::marker::PhantomData;
use std::ops::Index;
// Container
#[derive(Debug, Default)]
pub enum Ctr<'a> {
Symbol(String),
String(String),
Integer(i128),
Float(f64),
Bool(bool),
Seg(Seg<'a>),
#[default]
2022-01-16 22:02:40 -08:00
None,
}
// Type of Container
2022-01-16 22:02:40 -08:00
#[derive(PartialEq, Clone)]
pub enum Type {
Symbol,
String,
Integer,
Float,
Bool,
Seg,
2022-01-16 22:02:40 -08:00
None,
}
/* Segment
* Holds two Containers.
* Basic building block for more complex data structures.
* I was going to call it Cell and then I learned about
* how important RefCells were in Rust
*/
#[derive(Debug)]
pub struct Seg<'a> {
/* "Contents of Address Register"
* Historical way of referring to the first value in a cell.
*/
pub car: Box<Ctr<'a>>,
/* "Contents of Decrement Register"
* Historical way of referring to the second value in a cell.
*/
pub cdr: Box<Ctr<'a>>,
/* 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 ()>
}
static NOTHING: Ctr = Ctr::None;
impl Ctr<'_> {
pub fn to_type(&self) -> Type {
match self {
Ctr::Symbol(_s) => Type::Symbol,
Ctr::String(_s) => Type::String,
Ctr::Integer(_s) => Type::Integer,
Ctr::Float(_s) => Type::Float,
Ctr::Bool(_s) => Type::Bool,
Ctr::Seg(_s) => Type::Seg,
2022-01-16 22:02:40 -08:00
Ctr::None => Type::None,
}
}
}
impl<'a> Seg<'a> {
/* 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>>) {
if let Ctr::None = &*(self.car) {
self.car = obj;
return
}
if let Ctr::Seg(s) = &mut *(self.cdr) {
s.append(obj);
return
}
if let Ctr::None = &mut *(self.cdr) {
self.cdr = Box::new(Ctr::Seg(Seg::from_mono(obj)));
// pray for memory lost to the void
}
}
/* applies a function across a list in standard form
* function must take a Ctr and return a bool
* short circuits on the first false returned.
* also returns false on a non standard form list
*/
pub fn circuit<F: FnMut(&Ctr) -> bool>(&self, func: &mut F) -> bool {
if func(&self.car) {
match &*(self.cdr) {
Ctr::None => true,
Ctr::Seg(l) => l.circuit(func),
_ => false,
}
} else {
false
}
}
pub fn from_mono(arg: Box<Ctr<'a>>) -> Seg<'a> {
return 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{
car,
cdr,
_lifetime_variance_determinant: PhantomData,
}
}
/* recurs over ast assumed to be list in standard form
* returns length
*/
pub fn len(&self) -> u128 {
let mut len = 0;
self.circuit(&mut |_c: &Ctr| -> bool { len += 1; true });
len
}
pub fn new() -> Seg<'a> {
return Seg{
car: Box::new(Ctr::None),
cdr: Box::new(Ctr::None),
_lifetime_variance_determinant: PhantomData,
}
}
}
fn seg_to_string(s: &Seg, parens: bool) -> String {
let mut string = String::new();
if parens { string.push('('); }
match *(s.car) {
Ctr::None => string.push_str("<nil>"),
_ => string.push_str(&s.car.to_string()),
}
string.push(' ');
match &*(s.cdr) {
Ctr::Seg(inner) => string.push_str(&seg_to_string(&inner, false)),
Ctr::None => {string.pop();},
_ => string.push_str(&s.cdr.to_string()),
}
if parens { string.push(')'); }
string
}
impl<'a> Clone for Seg<'a> {
fn clone(&self) -> Seg<'a> {
return Seg{
car: self.car.clone(),
cdr: self.cdr.clone(),
_lifetime_variance_determinant: PhantomData,
}
}
}
impl<'a> Index<usize> for Seg<'a> {
type Output = Ctr<'a>;
fn index(&self, idx: usize) -> &Self::Output {
if idx == 0 {
return &self.car;
}
if let Ctr::Seg(ref s) = *self.cdr {
return s.index(idx - 1)
}
return &NOTHING;
}
}
impl<'a> Clone for Ctr<'a> {
fn clone(&self) -> Ctr<'a> {
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::Seg(s) => Ctr::Seg(s.clone()),
Ctr::None => Ctr::None,
}
}
}
impl fmt::Display for Ctr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Ctr::Symbol(s) => write!(f, "{}", s),
Ctr::String(s) => write!(f, "\'{}\'", s),
Ctr::Integer(s) => write!(f, "{}", s),
Ctr::Float(s) => write!(f, "{}", s),
Ctr::Bool(s) => {
if *s {
write!(f, "T")
} else {
write!(f, "F")
}
},
Ctr::Seg(s) => write!(f, "{}", s),
Ctr::None => Ok(()),
}
}
}
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",
}
ret.to_owned()
}
}