2025-07-24 19:44:43 +00:00
|
|
|
/* Mycelium Scheme
|
|
|
|
|
* Copyright (C) 2025 Ava Affine
|
|
|
|
|
*
|
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
use core::ops::{Index, Deref, DerefMut};
|
|
|
|
|
use core::ptr::NonNull;
|
2025-07-24 19:44:43 +00:00
|
|
|
|
|
|
|
|
use alloc::rc::Rc;
|
|
|
|
|
use alloc::vec::Vec;
|
2025-07-26 01:13:13 +00:00
|
|
|
use alloc::boxed::Box;
|
2025-07-24 19:44:43 +00:00
|
|
|
use alloc::string::String;
|
|
|
|
|
|
|
|
|
|
use organelle::Number;
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
/* NOTE
|
|
|
|
|
* decided not to implement a cache or a singleton heap manager
|
|
|
|
|
* because I did not want to involve a datatype that would add
|
|
|
|
|
* unneeded logic to where and how the Rcs get allocated or that
|
|
|
|
|
* would require relocation if more Rcs were allocated. Any
|
|
|
|
|
* ADT containing the source data referenced by Gc would add
|
|
|
|
|
* overhead without value.
|
|
|
|
|
*
|
|
|
|
|
* Meanwhile, just using allocated-at-site Rcs provides accurate
|
|
|
|
|
* reference counting garbage collection. We hack the Box::into_raw
|
|
|
|
|
* function to pass around heap allocated Rcs.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Gc
|
|
|
|
|
* This is a heap allocated Rc passed around such that it fits into
|
2025-07-27 07:18:14 +00:00
|
|
|
* a physical register. The pointer is to a Box<Rc<T>>, but custom
|
2025-07-26 01:13:13 +00:00
|
|
|
* deref implementation will ensure that deref always points to the
|
|
|
|
|
* encapsulated T
|
|
|
|
|
*/
|
|
|
|
|
#[repr(transparent)]
|
|
|
|
|
pub struct Gc<T>(NonNull<Rc<T>>);
|
|
|
|
|
|
2025-07-28 23:02:06 +00:00
|
|
|
impl<T> From<Rc<T>> for Gc<T> {
|
|
|
|
|
fn from(src: Rc<T>) -> Self {
|
2025-07-26 01:13:13 +00:00
|
|
|
Gc(NonNull::new(Box::into_raw(Box::new(src.clone())))
|
|
|
|
|
.expect("GC obj from rc nonnull ptr check"))
|
|
|
|
|
}
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:02:06 +00:00
|
|
|
impl<T> From<T> for Gc<T> {
|
|
|
|
|
fn from(value: T) -> Self {
|
2025-07-26 01:13:13 +00:00
|
|
|
Gc(NonNull::new(Box::into_raw(Box::new(Rc::from(value))))
|
|
|
|
|
.expect("GC obj from datum nonnull ptr check"))
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
impl<T: PartialEq> PartialEq for Gc<T> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().eq(other.deref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ne(&self, other: &Self) -> bool {
|
|
|
|
|
self.deref().ne(other.deref())
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
impl<T> Deref for Gc<T> {
|
|
|
|
|
type Target = T;
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
unsafe {
|
|
|
|
|
Rc::<T>::as_ptr(self.0.as_ref())
|
|
|
|
|
.as_ref()
|
|
|
|
|
.expect("GC obj deref inconsistent rc ptr")
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
impl<T> DerefMut for Gc<T> {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
unsafe {
|
|
|
|
|
(Rc::<T>::as_ptr(self.0.as_mut()) as *mut T)
|
|
|
|
|
.as_mut()
|
|
|
|
|
.expect("GC obj inconsistent rc ptr")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// takes a pointer to target Rc
|
|
|
|
|
macro_rules! shallow_copy_rc {
|
|
|
|
|
( $src:expr ) => {
|
|
|
|
|
unsafe {
|
|
|
|
|
NonNull::new(Box::into_raw(Box::new((*$src).clone())))
|
|
|
|
|
.expect("GC obj shallow copy nonnull ptr check")
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:02:06 +00:00
|
|
|
impl<T> Clone for Gc<T> {
|
2025-07-26 01:13:13 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Gc(shallow_copy_rc!(self.0.as_ptr()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
self.0 = shallow_copy_rc!(source.0.as_ptr());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for Gc<T> {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
2025-07-28 23:02:06 +00:00
|
|
|
let a = Box::<Rc<T>>::from_raw(self.0.as_ptr());
|
|
|
|
|
drop(a)
|
2025-07-26 01:13:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Clone> Gc<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn deep_copy(&self) -> Gc<T> {
|
|
|
|
|
Gc(unsafe {
|
|
|
|
|
NonNull::new(Box::into_raw(Box::new(Rc::from(
|
2025-07-28 23:36:26 +00:00
|
|
|
self.0.as_ref().clone()))))
|
2025-07-26 01:13:13 +00:00
|
|
|
.expect("GC obj deep copy nonnull ptr check")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:02:06 +00:00
|
|
|
#[derive(PartialEq)]
|
2025-07-26 01:13:13 +00:00
|
|
|
pub enum Datum {
|
|
|
|
|
Number(Number),
|
|
|
|
|
Bool(bool),
|
|
|
|
|
Cons(Cons),
|
|
|
|
|
Symbol(String),
|
|
|
|
|
Char(u8),
|
|
|
|
|
String(Vec<u8>),
|
2025-07-29 18:16:10 +00:00
|
|
|
Vector(Vec<Gc<Datum>>),
|
|
|
|
|
ByteVector(Vec<u8>),
|
2025-07-26 01:13:13 +00:00
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:02:06 +00:00
|
|
|
// implemented by hand to force deep copy on Cons datum
|
|
|
|
|
impl Clone for Datum {
|
|
|
|
|
fn clone(&self) -> Datum {
|
|
|
|
|
match self {
|
|
|
|
|
Datum::Number(n) => Datum::Number(n.clone()),
|
|
|
|
|
Datum::Bool(n) => Datum::Bool(n.clone()),
|
|
|
|
|
Datum::Cons(n) => Datum::Cons(n.deep_copy()),
|
|
|
|
|
Datum::Symbol(n) => Datum::Symbol(n.clone()),
|
|
|
|
|
Datum::Char(n) => Datum::Char(n.clone()),
|
|
|
|
|
Datum::String(n) => Datum::String(n.clone()),
|
|
|
|
|
Datum::Vector(n) =>
|
|
|
|
|
Datum::Vector(n.clone()),
|
|
|
|
|
Datum::ByteVector(n) =>
|
|
|
|
|
Datum::ByteVector(n.clone()),
|
|
|
|
|
Datum::None => Datum::None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
|
*self = source.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
|
pub struct Cons(pub Option<Gc<Datum>>, pub Option<Gc<Datum>>);
|
2025-07-24 19:44:43 +00:00
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
impl Cons {
|
|
|
|
|
pub fn deep_copy(&self) -> Cons {
|
2025-07-28 23:02:06 +00:00
|
|
|
macro_rules! car_cpy {
|
|
|
|
|
() => {
|
|
|
|
|
if let Some(ref car) = self.0 {
|
|
|
|
|
if let Datum::Cons(ref car) = **car {
|
|
|
|
|
Some(Datum::Cons(car.deep_copy()).into())
|
|
|
|
|
} else {
|
|
|
|
|
self.0.as_ref().map(|x| x.deep_copy())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(ref next) = self.1 {
|
|
|
|
|
if let Datum::Cons(ref next) = **next {
|
|
|
|
|
Cons(car_cpy!(), Some(Datum::Cons(next.deep_copy()).into()))
|
|
|
|
|
} else {
|
|
|
|
|
Cons(car_cpy!(), self.1.as_ref().map(|x| x.deep_copy()))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Cons(car_cpy!(), None)
|
|
|
|
|
}
|
2025-07-26 01:13:13 +00:00
|
|
|
}
|
2025-07-24 19:44:43 +00:00
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
pub fn subsl(&self, start: isize, end: isize) -> Cons {
|
2025-07-24 19:44:43 +00:00
|
|
|
if end - start == 1 {
|
2025-07-26 01:13:13 +00:00
|
|
|
return Cons(Some(self[start as usize].clone()), None)
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if end == 0 {
|
2025-07-26 01:13:13 +00:00
|
|
|
return Cons(
|
|
|
|
|
self.0.clone(),
|
|
|
|
|
None
|
2025-07-24 19:44:43 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
let Some(ref next) = self.1 else {
|
|
|
|
|
panic!("out of bounds subsl of cons list")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Datum::Cons(ref next) = **next else {
|
|
|
|
|
panic!("subsl of cons list not in standard form")
|
2025-07-24 19:44:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if start <= 0 {
|
2025-07-26 01:13:13 +00:00
|
|
|
Cons(self.0.clone(),
|
|
|
|
|
Some(Datum::Cons(next.subsl(start - 1, end - 1))
|
|
|
|
|
.into()))
|
2025-07-24 19:44:43 +00:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
next.subsl(start - 1, end - 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
2025-07-26 01:13:13 +00:00
|
|
|
let Some(_) = self.0 else {
|
|
|
|
|
return 0
|
2025-07-24 19:44:43 +00:00
|
|
|
};
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
let Some(ref next) = self.1 else {
|
|
|
|
|
return 1
|
|
|
|
|
};
|
2025-07-24 19:44:43 +00:00
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
let Datum::Cons(ref next) = **next else {
|
|
|
|
|
// weird list but okay
|
|
|
|
|
return 2
|
|
|
|
|
};
|
2025-07-24 19:44:43 +00:00
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
1 + next.len()
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
2025-07-27 07:18:14 +00:00
|
|
|
|
|
|
|
|
pub fn append(&mut self, arg: Gc<Datum>) {
|
|
|
|
|
let Some(_) = self.0 else {
|
|
|
|
|
self.0 = Some(arg);
|
|
|
|
|
return
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(next) = &mut self.1 {
|
|
|
|
|
if let Datum::Cons(next) = next.deref_mut() {
|
|
|
|
|
next.append(arg);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.1 = Some(Datum::Cons(Cons(Some(arg), None)).into());
|
|
|
|
|
}
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
|
|
|
|
|
impl Index<usize> for Cons {
|
|
|
|
|
type Output = Gc<Datum>;
|
2025-07-24 19:44:43 +00:00
|
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
|
|
|
if index == 0 {
|
2025-07-26 01:13:13 +00:00
|
|
|
if let Some(data) = &self.0 {
|
|
|
|
|
data
|
2025-07-24 19:44:43 +00:00
|
|
|
} else {
|
2025-07-26 01:13:13 +00:00
|
|
|
panic!("out of bounds indexing into cons list")
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-07-26 01:13:13 +00:00
|
|
|
let Some(ref next) = self.1 else {
|
|
|
|
|
panic!("out of bounds indexing into cons list")
|
2025-07-24 19:44:43 +00:00
|
|
|
};
|
|
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
let Datum::Cons(ref next) = **next else {
|
|
|
|
|
panic!("cons list not in standard form")
|
|
|
|
|
};
|
2025-07-24 19:44:43 +00:00
|
|
|
|
2025-07-26 01:13:13 +00:00
|
|
|
&next[index - 1]
|
2025-07-24 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-28 23:02:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2025-07-28 23:36:26 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct GcTester<'a>(pub &'a mut bool);
|
2025-07-28 23:02:06 +00:00
|
|
|
|
|
|
|
|
impl Clone for GcTester<'_> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
unsafe {
|
|
|
|
|
GcTester(Box::into_raw(Box::new(self.0.clone()))
|
|
|
|
|
.as_mut()
|
|
|
|
|
.expect("Gc Test obj mut ref fail"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clone_from(&mut self, _: &Self) {
|
|
|
|
|
unimplemented!("test impl")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for GcTester<'_> {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
*self.0 = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:36:26 +00:00
|
|
|
impl PartialEq for GcTester<'_> {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
*(self.0) == *(other.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ne(&self, other: &Self) -> bool {
|
|
|
|
|
*(self.0) != *(other.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:02:06 +00:00
|
|
|
#[test]
|
|
|
|
|
fn test_gc_basic_behavior() {
|
|
|
|
|
let mut flag = false;
|
|
|
|
|
let a = Into::<Gc<GcTester>>::into(GcTester(&mut flag));
|
2025-07-28 23:36:26 +00:00
|
|
|
assert!(!*(*a).0);
|
2025-07-28 23:02:06 +00:00
|
|
|
drop(a);
|
2025-07-28 23:36:26 +00:00
|
|
|
assert!(flag);
|
2025-07-28 23:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_gc_shallow_copy() {
|
|
|
|
|
let mut flag = false;
|
|
|
|
|
let a =
|
|
|
|
|
Into::<Gc<GcTester>>::into(GcTester(&mut flag)).clone();
|
2025-07-28 23:36:26 +00:00
|
|
|
assert!(!*(*a).0);
|
2025-07-28 23:02:06 +00:00
|
|
|
drop(a);
|
2025-07-28 23:36:26 +00:00
|
|
|
assert!(flag);
|
2025-07-28 23:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:36:26 +00:00
|
|
|
#[test]
|
|
|
|
|
fn test_gc_deep_copy() {
|
|
|
|
|
let mut flag = false;
|
|
|
|
|
let reference_holder =
|
|
|
|
|
Into::<Gc<GcTester>>::into(GcTester(&mut flag)).clone();
|
|
|
|
|
assert!(!*(*reference_holder).0);
|
|
|
|
|
let copied_data = reference_holder.deep_copy();
|
|
|
|
|
assert!(!*(*copied_data).0);
|
|
|
|
|
assert_eq!(*reference_holder, *copied_data);
|
|
|
|
|
drop(reference_holder);
|
|
|
|
|
assert!(!*(*copied_data).0);
|
|
|
|
|
}
|
2025-07-28 23:02:06 +00:00
|
|
|
}
|