Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw canonical ast separation #10

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
45 changes: 37 additions & 8 deletions src/sexpr_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,50 @@ impl Parser {
}
}

pub fn build_pair(expressions: Vec<Sexp>) -> Sexp {
pub fn build_pair(mut expressions: Vec<Sexp>) -> Sexp {
expressions.reverse();
Copy link
Member Author

Choose a reason for hiding this comment

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

Hacky yeah, because rust does not have foldr

Choose a reason for hiding this comment

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

Yep.

expressions
.iter()
.fold(Sexp::Unit, |pair_tree, el| Sexp::Pair(Box::new(el.clone()), Box::new(pair_tree)))
}

#[cfg(test)]
mod test {
// use crate::sexpr_parser::*;
use crate::sexpr_parser::*;
use crate::sexpr_parser::Sexp::*;
use crate::sexpr_parser::Atom::*;

#[test]
fn parse_unit_expression() {
let blob = "()";
let sexp = Parser::parse(blob.to_owned()).unwrap();
assert_eq!(sexp, vec![Unit])
}

#[test]
fn parse_atom_expression() {
let blob = "(atom)";
let sexp = Parser::parse(blob.to_owned()).unwrap();
assert_eq!(sexp, vec![Atom(N("atom".to_string()))])
}

// #[test]
// fn parse_expression() {
// let blob = "(Giuseppe Verdi Louis)";
// let sexp = Parser::parse(blob.to_owned()).unwrap();
// assert_eq!(sexp, vec![]);
// }
#[test]
fn parse_pair_atom_expression() {
let blob = "(Giuseppe Verdi)";
let sexp = Parser::parse(blob.to_owned()).unwrap();
assert_eq!(sexp, vec![
Pair(Box::new(Atom(N("Giuseppe".to_string()))), Box::new(Atom(N("Verdi".to_string()))))
])
}

#[test]
fn parse_multiple_atom_expression() {
let blob = "(Giuseppe Verdi Louis)";
let sexp = Parser::parse(blob.to_owned()).unwrap();
assert_eq!(sexp, vec![
Pair(Box::new(Atom(N("Giuseppe".to_string()))),
Box::new(Pair(Box::new(Atom(N("Verdi".to_string()))),
Box::new(Pair(Box::new(Atom(N("Louis".to_string()))), Box::new(Unit))))))
]);
}
}