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

Add dotted lists to Lisp #427

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/usr/lisp/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ pub fn expand(exp: &Exp, env: &mut Rc<RefCell<Env>>) -> Result<Exp, Err> {
match (&list[1], &list[2]) {
(Exp::List(args), Exp::List(_)) => {
ensure_length_gt!(args, 0);
let name = args[0].clone();
let args = Exp::List(args[1..].to_vec());
let (name, args) = if args.len() == 3 && args[0] == Exp::Sym("cons".to_string()) {
(args[1].clone(), args[2].clone())
} else {
(args[0].clone(), Exp::List(args[1..].to_vec()))
};
let body = expand(&list[2], env)?;
Ok(Exp::List(vec![
Exp::Sym("define".to_string()), name, Exp::List(vec![
Expand Down
9 changes: 9 additions & 0 deletions src/usr/lisp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,16 @@ fn test_lisp() {
eval!("(set-10 foo)");
assert_eq!(eval!("foo"), "10");

// dotted pair
assert_eq!(eval!("(cons 1 (cons 2 (cons 3 '())))"), "(1 2 3)");
assert_eq!(eval!("(cons 1 (2 . (3 . '())))"), "(1 2 3)");
assert_eq!(eval!("(cons 1 (list 2 3))"), "(1 2 3)");
assert_eq!(eval!("'(cons 1 (cons 2 (cons 3 '())))"), "(cons 1 (cons 2 (cons 3 (quote ()))))");
assert_eq!(eval!("'(1 . (2 . (3 . '())))"), "(cons 1 (cons 2 (cons 3 (quote ()))))");

// args
eval!("(define list* (function args (append args '())))");
assert_eq!(eval!("(list* 1 2 3)"), "(1 2 3)");
eval!("(define (list* . args) (append args '())))");
assert_eq!(eval!("(list* 1 2 3)"), "(1 2 3)");
}
21 changes: 20 additions & 1 deletion src/usr/lisp/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ use nom::combinator::opt;
use nom::combinator::recognize;
use nom::combinator::value;
use nom::multi::many0;
use nom::multi::many1;
use nom::sequence::delimited;
use nom::sequence::preceded;
use nom::sequence::tuple;
use nom::sequence::separated_pair;

fn is_symbol_letter(c: char) -> bool {
let chars = "<>=-+*/%^?:";
Expand Down Expand Up @@ -61,6 +63,22 @@ fn parse_list(input: &str) -> IResult<&str, Exp> {
Ok((input, Exp::List(list)))
}

fn parse_dotted_list(input: &str) -> IResult<&str, Exp> {
let (input, (mut list, cdr)) = delimited(
char('('),
separated_pair(many1(parse_exp), char('.'), parse_exp),
char(')')
)(input)?;
let car = list.pop().unwrap();
let cons = Exp::List(vec![Exp::Sym("cons".to_string()), car, cdr]);
if list.is_empty() {
Ok((input, cons))
} else {
list.push(cons);
Ok((input, Exp::List(list)))
}
}

fn parse_quote(input: &str) -> IResult<&str, Exp> {
let (input, list) = preceded(char('\''), parse_exp)(input)?;
let list = vec![Exp::Sym("quote".to_string()), list];
Expand All @@ -87,7 +105,8 @@ fn parse_quasiquote(input: &str) -> IResult<&str, Exp> {

fn parse_exp(input: &str) -> IResult<&str, Exp> {
delimited(multispace0, alt((
parse_num, parse_bool, parse_str, parse_list, parse_quote, parse_unquote_splicing, parse_unquote, parse_quasiquote, parse_sym
parse_num, parse_bool, parse_str, parse_dotted_list, parse_list, parse_quote,
parse_unquote_splicing, parse_unquote, parse_quasiquote, parse_sym
)), multispace0)(input)
}

Expand Down