refmt
This commit is contained in:
parent
f805290a4b
commit
be73b0b828
17 changed files with 588 additions and 675 deletions
|
|
@ -15,15 +15,14 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::rc::Rc;
|
||||
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)]
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Ctr {
|
||||
Symbol(String),
|
||||
String(String),
|
||||
|
|
@ -31,12 +30,11 @@ pub enum Ctr {
|
|||
Float(f64),
|
||||
Bool(bool),
|
||||
Seg(Ast),
|
||||
None
|
||||
None,
|
||||
}
|
||||
|
||||
// Type of Container
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub enum Type {
|
||||
Symbol,
|
||||
String,
|
||||
|
|
@ -44,7 +42,7 @@ pub enum Type {
|
|||
Float,
|
||||
Bool,
|
||||
Seg,
|
||||
None
|
||||
None,
|
||||
}
|
||||
|
||||
/* Segment
|
||||
|
|
@ -53,8 +51,7 @@ pub enum Type {
|
|||
* I was going to call it Cell and then I learned about
|
||||
* how important RefCells were in Rust
|
||||
*/
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Seg {
|
||||
/* "Contents of Address Register"
|
||||
* Historical way of referring to the first value in a cell.
|
||||
|
|
@ -64,7 +61,7 @@ pub struct Seg {
|
|||
/* "Contents of Decrement Register"
|
||||
* Historical way of referring to the second value in a cell.
|
||||
*/
|
||||
pub cdr: Ctr
|
||||
pub cdr: Ctr,
|
||||
}
|
||||
|
||||
impl Ctr {
|
||||
|
|
@ -76,7 +73,7 @@ impl Ctr {
|
|||
Ctr::Float(_s) => Type::Float,
|
||||
Ctr::Bool(_s) => Type::Bool,
|
||||
Ctr::Seg(_s) => Type::Seg,
|
||||
Ctr::None => Type::None
|
||||
Ctr::None => Type::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -91,7 +88,7 @@ impl Type {
|
|||
Type::Float => ret = "float",
|
||||
Type::Bool => ret = "bool",
|
||||
Type::Seg => ret = "segment",
|
||||
Type::None => ret = "none"
|
||||
Type::None => ret = "none",
|
||||
}
|
||||
|
||||
ret.to_owned()
|
||||
|
|
@ -105,17 +102,17 @@ pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
|
|||
let mut prn_space = true;
|
||||
let seg = c.borrow();
|
||||
match &seg.car {
|
||||
Ctr::Symbol(s) => string.push_str(&s),
|
||||
Ctr::String(s) => {
|
||||
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
|
||||
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 {
|
||||
|
|
@ -123,17 +120,17 @@ pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
|
|||
}
|
||||
|
||||
match &seg.cdr {
|
||||
Ctr::Symbol(s) => string.push_str(&s),
|
||||
Ctr::String(s) => {
|
||||
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 => {
|
||||
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();
|
||||
}
|
||||
|
|
@ -147,7 +144,7 @@ pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
|
|||
extra.push(')');
|
||||
string = extra
|
||||
}
|
||||
return string
|
||||
return string;
|
||||
}
|
||||
|
||||
pub fn ast_to_string(c: Ast) -> String {
|
||||
|
|
@ -162,10 +159,7 @@ pub fn ast_to_string(c: Ast) -> String {
|
|||
/* 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
|
||||
}))
|
||||
Rc::new(RefCell::new(Seg { car: car, cdr: cdr }))
|
||||
}
|
||||
|
||||
/* applies a function across a list in standard form
|
||||
|
|
@ -173,13 +167,13 @@ pub fn new_ast(car: Ctr, cdr: Ctr) -> Ast {
|
|||
* 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{
|
||||
pub fn circuit<F: FnMut(&Ctr) -> bool>(tree: Ast, func: &mut F) -> bool {
|
||||
let inner = tree.borrow();
|
||||
if func(&inner.car) {
|
||||
match &inner.cdr {
|
||||
Ctr::None => true,
|
||||
Ctr::Seg(c) => circuit(c.clone(), func),
|
||||
_ => false
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
|
|
@ -192,7 +186,7 @@ pub fn circuit<F: FnMut(&Ctr)-> bool>(tree: Ast, func: &mut F) -> bool{
|
|||
pub fn list_len(tree: Ast) -> u128 {
|
||||
match &tree.borrow().cdr {
|
||||
Ctr::Seg(c) => list_len(c.clone()) + 1,
|
||||
_ => 1
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -216,9 +210,7 @@ pub fn list_idx(tree: Ast, idx: u128) -> Ctr {
|
|||
} else {
|
||||
match inner.car {
|
||||
Ctr::None => Ctr::None,
|
||||
_ => {
|
||||
inner.car.clone()
|
||||
}
|
||||
_ => inner.car.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -231,17 +223,15 @@ pub fn list_append(tree: Ast, obj: Ctr) {
|
|||
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);
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
_ => match &inner.cdr {
|
||||
Ctr::None => {
|
||||
inner.cdr = Ctr::Seg(new_ast(obj, Ctr::None));
|
||||
}
|
||||
Ctr::Seg(tr) => {
|
||||
list_append(tr.clone(), obj);
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue