Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
#[derive(Clone, PartialEq, Debug)]
pub enum Node {
Atom(Atom),
List(List),
Type(i64)
Atom(&'static str),
Pair(Box<Node>, Box<Node>),
Type(Type),
}

pub type Atom = &'static str;

// =====================================
// List
// =====================================
pub type List = Vec<Node>;
#[derive(Clone, PartialEq, Debug)]
pub enum Type {
Universe(i64),
Atom,
Pair(Box<Type>, Box<Type>)
}

pub fn cons(list: List, atom: Atom) -> Node {
let mut cloned_list = list.clone();
cloned_list.push(Node::Atom(atom));
Node::List(cloned_list)
pub fn cons(node: Node, atom: Node) -> Node {
Node::Pair(Box::new(node), Box::new(atom))
}

pub fn car(list: List) -> Node {
list.first().unwrap().clone()
pub fn car(node: Node) -> Result<Node, &'static str> {
Comment thread
hch12907 marked this conversation as resolved.
Outdated
match node {
Node::Pair(a, _) => Result::Ok(*a),
_ => Result::Err("Can only apply car to Pair")
}
}

pub fn cdr(list: List) -> Node {
let mut cloned_list = list.clone();
cloned_list.remove(0);
Node::List(cloned_list)
pub fn cdr(node: Node) -> Result<Node, &'static str> {
match node {
Node::Pair(_, b) => Result::Ok(*b),
_ => Result::Err("Cannot apply cdr to Pair")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to use a dedicated Error enum instead of a string for Result::Err... but I digress

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, for this case Option<T> is better as your return type

}
}

pub fn type_of(node: Node) -> Node {
pub fn type_of(node: Node) -> Type {
match node {
Node::Atom(_) => Node::Type(0),
Node::List(_) => Node::Type(0),
Node::Type(n) => Node::Type(n + 1)
Node::Atom(_) => Type::Atom,
Node::Pair(a, b) => Type::Pair(Box::new(type_of(*a)), Box::new(type_of(*b))),
Node::Type(Type::Universe(n)) => Type::Universe(n + 1),
Node::Type(_) => Type::Universe(0)
}
}