2021-03-14 16:14:57 -07:00
|
|
|
/* 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/>.
|
|
|
|
|
*/
|
2023-01-27 17:45:19 -08:00
|
|
|
use std::fmt;
|
2023-02-15 23:27:00 -08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
use std::ops::Index;
|
2021-03-14 16:14:57 -07:00
|
|
|
|
|
|
|
|
// Container
|
2023-02-15 23:27:00 -08:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub enum Ctr<'a> {
|
2021-03-14 16:14:57 -07:00
|
|
|
Symbol(String),
|
|
|
|
|
String(String),
|
|
|
|
|
Integer(i128),
|
|
|
|
|
Float(f64),
|
|
|
|
|
Bool(bool),
|
2023-01-27 17:45:19 -08:00
|
|
|
Seg(Seg<'a>),
|
|
|
|
|
#[default]
|
2022-01-16 22:02:40 -08:00
|
|
|
None,
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Type of Container
|
2022-01-16 22:02:40 -08:00
|
|
|
#[derive(PartialEq, Clone)]
|
2021-03-14 16:14:57 -07:00
|
|
|
pub enum Type {
|
|
|
|
|
Symbol,
|
|
|
|
|
String,
|
|
|
|
|
Integer,
|
|
|
|
|
Float,
|
|
|
|
|
Bool,
|
|
|
|
|
Seg,
|
2022-01-16 22:02:40 -08:00
|
|
|
None,
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Segment
|
|
|
|
|
* Holds two Containers.
|
|
|
|
|
* Basic building block for more complex data structures.
|
2023-02-15 23:27:00 -08:00
|
|
|
* I was going to call it Cell and then I learned about
|
|
|
|
|
* how important RefCells were in Rust
|
2021-03-14 16:14:57 -07:00
|
|
|
*/
|
2023-02-15 23:27:00 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Seg<'a> {
|
2021-03-14 16:14:57 -07:00
|
|
|
/* "Contents of Address Register"
|
|
|
|
|
* Historical way of referring to the first value in a cell.
|
|
|
|
|
*/
|
2023-02-15 23:27:00 -08:00
|
|
|
pub car: Box<Ctr<'a>>,
|
2021-03-14 16:14:57 -07:00
|
|
|
|
|
|
|
|
/* "Contents of Decrement Register"
|
|
|
|
|
* Historical way of referring to the second value in a cell.
|
|
|
|
|
*/
|
2023-02-15 23:27:00 -08:00
|
|
|
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 ()>
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 23:27:00 -08:00
|
|
|
static NOTHING: Ctr = Ctr::None;
|
|
|
|
|
|
2023-01-27 17:45:19 -08:00
|
|
|
impl Ctr<'_> {
|
2021-03-14 16:14:57 -07:00
|
|
|
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,
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 23:27:00 -08:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 17:45:19 -08:00
|
|
|
fn seg_to_string(s: &Seg, parens: bool) -> String {
|
2021-03-14 16:14:57 -07:00
|
|
|
let mut string = String::new();
|
2023-02-15 23:27:00 -08:00
|
|
|
if parens { string.push('('); }
|
|
|
|
|
match *(s.car) {
|
2023-01-27 17:45:19 -08:00
|
|
|
Ctr::None => string.push_str("<nil>"),
|
2023-02-15 23:27:00 -08:00
|
|
|
_ => string.push_str(&s.car.to_string()),
|
2023-01-27 17:45:19 -08:00
|
|
|
}
|
|
|
|
|
string.push(' ');
|
2023-02-15 23:27:00 -08:00
|
|
|
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()),
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
2023-02-15 23:27:00 -08:00
|
|
|
if parens { string.push(')'); }
|
|
|
|
|
|
|
|
|
|
string
|
|
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
|
2023-02-15 23:27:00 -08:00
|
|
|
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,
|
|
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
2023-01-27 17:45:19 -08:00
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
|
2023-02-15 23:27:00 -08:00
|
|
|
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<'_> {
|
2023-01-27 17:45:19 -08:00
|
|
|
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) => {
|
2023-02-15 23:27:00 -08:00
|
|
|
if *s {
|
2023-01-27 17:45:19 -08:00
|
|
|
write!(f, "T")
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, "F")
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Ctr::Seg(s) => write!(f, "{}", s),
|
2023-02-15 23:27:00 -08:00
|
|
|
Ctr::None => Ok(()),
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-27 17:45:19 -08:00
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
|
2023-01-27 17:45:19 -08:00
|
|
|
impl fmt::Display for Seg<'_> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
write!(f, "{}", seg_to_string(self, true))
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 17:45:19 -08:00
|
|
|
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",
|
|
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
|
2023-01-27 17:45:19 -08:00
|
|
|
ret.to_owned()
|
|
|
|
|
}
|
2021-03-14 16:14:57 -07:00
|
|
|
}
|