Bring in the Rust implementation.

This commit is contained in:
Simon Forman
2022-01-15 17:28:31 -08:00
parent 839b376d73
commit 530deab19d
5 changed files with 280 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
mod stack;
use lazy_static::lazy_static;
use regex::Regex;
use std::io;
#[derive(Debug)]
enum JoyVal {
True,
False,
List(stack::List<JoyVal>),
Int(i64),
Sym(String),
}
fn tokenize(str: &str) -> JoyVal {
lazy_static! {
static ref RE: Regex = Regex::new(
r"(?x)
(?P<true>true)\b
| (?P<false>false)\b
| (?P<lbracket>\[)
| (?P<rbracket>\])
| (?P<int>\d+)
| (?P<atom>[^\s+\[\]]+) "
)
.unwrap();
}
let mut frame = vec![];
let mut stack = vec![];
for cap in RE.captures_iter(str) {
// Is this better than the if/elif/else chain below?
// More importantly, how would I go about figuring that out?
match cap.name("true") {
Some(_) => {
frame.push(JoyVal::True);
continue;
}
None => {}
}
// Is there a better way to do this?
//if cap.name("true") != None {
// println!("T");
//} else
if cap.name("false") != None {
frame.push(JoyVal::False);
} else if cap.name("lbracket") != None {
stack.push(frame);
frame = vec![];
} else if cap.name("rbracket") != None {
let v = frame;
frame = stack.pop().expect("Extra closing bracket.");
frame.push(JoyVal::List(stack::List::vec_to_list(v)));
} else if cap.name("int") != None {
frame.push(JoyVal::Int(cap[0].parse().unwrap()));
} else if cap.name("atom") != None {
frame.push(JoyVal::Sym(String::from(&cap[0])));
} else {
println!("wtf");
}
}
JoyVal::List(stack::List::vec_to_list(frame))
}
fn joy(mut s: stack::List<JoyVal>, mut e: stack::List<JoyVal>) -> stack::List<JoyVal> {
while !e.empty() {
let term = e.head().expect("How!?");
e = e.tail();
match term {
JoyVal::True => { s = s.push(JoyVal::True) }
JoyVal::False => { s = s.push(JoyVal::False) }
JoyVal::List(_) => { s = s.push(term) }
JoyVal::Int(_) => { s = s.push(term) }
JoyVal::Sym(_) => { s = s.push(term) }
}
}
s
}
fn main() {
println!("Enter a number:");
let mut n = String::new();
let m = io::stdin().read_line(&mut n).expect("WTF?");
println!("{:?}", tokenize(&n));
println!("Entered: {} {}", m, n);
}
+133
View File
@@ -0,0 +1,133 @@
// https://rust-unofficial.github.io/too-many-lists/third.html
use std::rc::Rc;
#[derive(Debug)]
pub struct List<T> {
head: Link<T>,
}
type Link<T> = Option<Rc<Node<T>>>;
#[derive(Debug)]
struct Node<T> {
elem: T,
next: Link<T>,
}
impl<T> List<T> {
pub fn new() -> Self {
List { head: None }
}
pub fn push(&self, elem: T) -> List<T> {
List {
head: Some(Rc::new(Node {
elem: elem,
next: self.head.clone(),
})),
}
}
pub fn tail(&self) -> List<T> {
List {
head: self.head.as_ref().and_then(|node| node.next.clone()),
}
}
pub fn head(&self) -> Option<&T> {
self.head.as_ref().map(|node| &node.elem)
}
pub fn iter(&self) -> Iter<'_, T> {
Iter {
next: self.head.as_deref(),
}
}
pub fn vec_to_list(v: Vec<T>) -> List<T> {
let mut list = List::new();
for t in v.into_iter().rev() {
list = list.push(t);
}
list
}
pub fn empty(&self) -> bool {
match self.head {
Some(_) => false,
None => true
}
}
}
impl<T> Drop for List<T> {
fn drop(&mut self) {
let mut head = self.head.take();
while let Some(node) = head {
if let Ok(mut node) = Rc::try_unwrap(node) {
head = node.next.take();
} else {
break;
}
}
}
}
pub struct Iter<'a, T> {
next: Option<&'a Node<T>>,
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.next.map(|node| {
self.next = node.next.as_deref();
&node.elem
})
}
}
#[cfg(test)]
mod test {
use super::List;
#[test]
fn basics() {
let list = List::new();
assert_eq!(list.head(), None);
assert_eq!(list.empty(), true);
let list = list.push(1).push(2).push(3);
assert_eq!(list.empty(), false);
assert_eq!(list.head(), Some(&3));
let list = list.tail();
assert_eq!(list.head(), Some(&2));
let list = list.tail();
assert_eq!(list.head(), Some(&1));
let list = list.tail();
assert_eq!(list.head(), None);
// Make sure empty tail works
let list = list.tail();
assert_eq!(list.head(), None);
}
#[test]
fn iter() {
let list = List::new().push(1).push(2).push(3);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
}
#[test]
fn v2l() {
let v = vec![1, 2, 3];
let list = List::vec_to_list(v);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
}
}