WIP commit:
* Fix up project structures * combine vars and funcs table * make a place for old code that may be useful to reference * singleton pattern for sym table Commentary: When this change is finally finished I promise to use feature branches from here on out
This commit is contained in:
parent
b680e3ca9a
commit
ca4c557d95
32 changed files with 1092 additions and 616 deletions
247
src/segment.rs
247
src/segment.rs
|
|
@ -15,11 +15,12 @@
|
|||
* 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, Clone, Default)]
|
||||
pub enum Ctr <'a> {
|
||||
#[derive(Debug, Default)]
|
||||
pub enum Ctr<'a> {
|
||||
Symbol(String),
|
||||
String(String),
|
||||
Integer(i128),
|
||||
|
|
@ -45,20 +46,31 @@ pub enum Type {
|
|||
/* 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(Clone, Debug, Default)]
|
||||
pub struct Seg <'a> {
|
||||
#[derive(Debug)]
|
||||
pub struct Seg<'a> {
|
||||
/* "Contents of Address Register"
|
||||
* Historical way of referring to the first value in a cell.
|
||||
*/
|
||||
pub car: &mut Ctr<'a>,
|
||||
pub car: Box<Ctr<'a>>,
|
||||
|
||||
/* "Contents of Decrement Register"
|
||||
* Historical way of referring to the second value in a cell.
|
||||
*/
|
||||
pub cdr: &mut Ctr<'a>,
|
||||
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 {
|
||||
|
|
@ -74,25 +86,140 @@ impl Ctr<'_> {
|
|||
|
||||
}
|
||||
|
||||
fn seg_to_string(s: &Seg, parens: bool) -> String {
|
||||
let mut string = String::new();
|
||||
match s.car {
|
||||
Ctr::None => string.push_str("<nil>"),
|
||||
_ => string.push_str(s.car),
|
||||
}
|
||||
string.push(' ');
|
||||
match s.cdr {
|
||||
Ctr::Seg(inner) => string.push_str(seg_to_string(inner, false)),
|
||||
Ctr::None => {},
|
||||
_ => string.push_str(s.cdr),
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if parens {
|
||||
String::from("(" + string + ")")
|
||||
/* 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Ctr <'_> {
|
||||
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),
|
||||
|
|
@ -100,14 +227,14 @@ impl fmt::Display for Ctr <'_> {
|
|||
Ctr::Integer(s) => write!(f, "{}", s),
|
||||
Ctr::Float(s) => write!(f, "{}", s),
|
||||
Ctr::Bool(s) => {
|
||||
if s {
|
||||
if *s {
|
||||
write!(f, "T")
|
||||
} else {
|
||||
write!(f, "F")
|
||||
}
|
||||
},
|
||||
Ctr::Seg(s) => write!(f, "{}", s),
|
||||
Ctr::None => Ok(),
|
||||
Ctr::None => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -118,16 +245,6 @@ impl fmt::Display for Seg<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Iterator for Seg<'_> {
|
||||
fn next(&self) -> Option<&Seg> {
|
||||
if let Ctr::Seg(s) = self.cdr {
|
||||
Ok(s)
|
||||
} else {
|
||||
None()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Type {
|
||||
pub fn to_string(&self) -> String {
|
||||
let ret: &str;
|
||||
|
|
@ -144,67 +261,3 @@ impl Type {
|
|||
ret.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
/* 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: Fn(&Ctr)>(list: &Seg, func: &mut F) -> bool {
|
||||
if func(&list.car) {
|
||||
match list.cdr {
|
||||
Ctr::None => true,
|
||||
Ctr::Seg(l) => circuit(l, func),
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/* recurs over ast assumed to be list in standard form
|
||||
* returns length
|
||||
*/
|
||||
pub fn list_len(list: &Seg) -> u128 {
|
||||
let mut len = 0;
|
||||
circuit(list, &circuit(&mut |c: &Ctr| -> bool { len += 1; true }))
|
||||
}
|
||||
|
||||
/* recurs over tree assumed to be list in standard form
|
||||
* returns clone of ctr at index provided
|
||||
*
|
||||
* TODO: return result (or option?)
|
||||
*/
|
||||
pub fn list_idx<'a>(list: &Seg, idx: u128) -> Ctr<'a> {
|
||||
if idx > 0 {
|
||||
if let Ctr::Seg(s) = list.car {
|
||||
list_idx(s, idx - 1)
|
||||
} else if idx == 1 {
|
||||
list.cdr
|
||||
} else {
|
||||
Ctr::None
|
||||
}
|
||||
} else {
|
||||
list.car
|
||||
}
|
||||
}
|
||||
|
||||
/* recurs over tree assumed to be list in standard form
|
||||
* appends object to end of list
|
||||
*
|
||||
* TODO: return result
|
||||
*/
|
||||
pub fn list_append<'a>(list: &Seg, obj: Ctr) {
|
||||
if let Ctr::None = list.car {
|
||||
list.car = obj;
|
||||
return
|
||||
}
|
||||
|
||||
if let Ctr::Seg(s) = list.cdr {
|
||||
list_append(s, obj)
|
||||
}
|
||||
|
||||
if let Ctr::None = list.cdr {
|
||||
list.cdr = Ctr::Seg(&Seg{car:obj, cdr:Ctr::None})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue