big temp status

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-01-27 17:45:19 -08:00
parent 45453f819f
commit 5261efbc65
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
12 changed files with 960 additions and 224 deletions

View file

@ -14,22 +14,19 @@
* 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::cell::RefCell;
use std::rc::Rc;
// Recursive data type for a tree of Segments
pub type Ast = Rc<RefCell<Seg>>;
// Container
#[derive(Clone, Debug)]
pub enum Ctr {
#[derive(Debug, Clone, Default)]
pub enum Ctr <'a> {
Symbol(String),
String(String),
Integer(i128),
Float(f64),
Bool(bool),
Seg(Ast),
Seg(Seg<'a>),
#[default]
None,
}
@ -48,23 +45,21 @@ 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)]
pub struct Seg {
#[derive(Clone, Debug, Default)]
pub struct Seg <'a> {
/* "Contents of Address Register"
* Historical way of referring to the first value in a cell.
*/
pub car: Ctr,
pub car: &mut Ctr<'a>,
/* "Contents of Decrement Register"
* Historical way of referring to the second value in a cell.
*/
pub cdr: Ctr,
pub cdr: &mut Ctr<'a>,
}
impl Ctr {
impl Ctr<'_> {
pub fn to_type(&self) -> Type {
match self {
Ctr::Symbol(_s) => Type::Symbol,
@ -76,10 +71,65 @@ impl Ctr {
Ctr::None => Type::None,
}
}
}
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),
}
if parens {
String::from("(" + string + ")")
}
}
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 Iterator for Seg<'_> {
fn next(&self) -> Option<&Seg> {
if let Ctr::Seg(s) = self.cdr {
Ok(s)
} else {
None()
}
}
}
impl Type {
pub fn to_str(&self) -> String {
pub fn to_string(&self) -> String {
let ret: &str;
match self {
Type::Symbol => ret = "symbol",
@ -95,84 +145,16 @@ impl Type {
}
}
/* Prints a Syntax Tree as a string
*/
pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
let mut string = String::new();
let mut prn_space = true;
let seg = c.borrow();
match &seg.car {
Ctr::Symbol(s) => string.push_str(&s),
Ctr::String(s) => {
string.push('\'');
string.push_str(&s);
string.push('\'');
}
Ctr::Integer(i) => string = string + &i.to_string(),
Ctr::Float(f) => string = string + &f.to_string(),
Ctr::Bool(b) => string = string + &b.to_string(),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => prn_space = false,
}
if prn_space {
string.push(' ');
}
match &seg.cdr {
Ctr::Symbol(s) => string.push_str(&s),
Ctr::String(s) => {
string.push('\'');
string.push_str(&s);
string.push('\'');
}
Ctr::Integer(i) => string = string + &i.to_string(),
Ctr::Float(f) => string = string + &f.to_string(),
Ctr::Bool(b) => string = string + &b.to_string(),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), false).as_str()),
Ctr::None => {
if prn_space {
string.pop();
}
}
}
// TODO: maybe a better way to do this
if with_parens {
let mut extra = String::from("(");
extra.push_str(&string);
extra.push(')');
string = extra
}
return string;
}
pub fn ast_to_string(c: Ast) -> String {
ast_as_string(c.clone(), true)
}
/* NOTE: "Standard form" is used here to refer to a list of segments
* that resembles a typical linked list. This means that Car may hold whatever,
* but Cdr must either be Seg or None.
*/
/* Initializes a new ast node with segment car and cdr passed in
*/
pub fn new_ast(car: Ctr, cdr: Ctr) -> Ast {
Rc::new(RefCell::new(Seg { car: car, cdr: cdr }))
}
/* 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>(tree: Ast, func: &mut F) -> bool {
let inner = tree.borrow();
if func(&inner.car) {
match &inner.cdr {
pub fn circuit<F: Fn(&Ctr)>(list: &Seg, func: &mut F) -> bool {
if func(&list.car) {
match list.cdr {
Ctr::None => true,
Ctr::Seg(c) => circuit(c.clone(), func),
Ctr::Seg(l) => circuit(l, func),
_ => false,
}
} else {
@ -183,55 +165,46 @@ pub fn circuit<F: FnMut(&Ctr) -> bool>(tree: Ast, func: &mut F) -> bool {
/* recurs over ast assumed to be list in standard form
* returns length
*/
pub fn list_len(tree: Ast) -> u128 {
match &tree.borrow().cdr {
Ctr::Seg(c) => list_len(c.clone()) + 1,
_ => 1,
}
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(tree: Ast, idx: u128) -> Ctr {
let inner = tree.borrow();
pub fn list_idx<'a>(list: &Seg, idx: u128) -> Ctr<'a> {
if idx > 0 {
match &inner.cdr {
Ctr::None => Ctr::None,
Ctr::Seg(c) => list_idx(c.clone(), idx - 1),
_ => {
if idx == 1 {
inner.cdr.clone()
} else {
Ctr::None
}
}
if let Ctr::Seg(s) = list.car {
list_idx(s, idx - 1)
} else if idx == 1 {
list.cdr
} else {
Ctr::None
}
} else {
match inner.car {
Ctr::None => Ctr::None,
_ => inner.car.clone(),
}
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(tree: Ast, obj: Ctr) {
let mut inner = tree.borrow_mut();
match &inner.car {
Ctr::None => {
inner.car = obj;
}
_ => match &inner.cdr {
Ctr::None => {
inner.cdr = Ctr::Seg(new_ast(obj, Ctr::None));
}
Ctr::Seg(tr) => {
list_append(tr.clone(), obj);
}
_ => (),
},
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})
}
}