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

Fix Lisp comments #493

Merged
merged 7 commits into from
Jun 7, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- Fix Lisp comments (#493)
- Add docstring to lisp (#490)
- Add full support for comments in lisp (#489)
- Add parenthesis matching to editor (#488)
Expand Down
6 changes: 6 additions & 0 deletions src/usr/lisp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@ fn test_lisp() {
// args
eval!("(variable list* (function args (append args '())))");
assert_eq!(eval!("(list* 1 2 3)"), "(1 2 3)");

// comments
assert_eq!(eval!("# comment"), "()");
assert_eq!(eval!("# comment\n# comment"), "()");
assert_eq!(eval!("(+ 1 2 3) # comment"), "6");
assert_eq!(eval!("(+ 1 2 3) # comment\n# comment"), "6");
}
21 changes: 11 additions & 10 deletions src/usr/lisp/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,27 @@ fn parse_quasiquote(input: &str) -> IResult<&str, Exp> {
Ok((input, Exp::List(list)))
}

use nom::sequence::pair;
use alloc::format;

fn parse_comment(input: &str) -> IResult<&str, ()> {
value((), pair(char('#'), is_not("\n")))(input)
fn parse_comment(input: &str) -> IResult<&str, &str> {
preceded(multispace0, preceded(char('#'), is_not("\n")))(input)
}

fn parse_exp(input: &str) -> IResult<&str, Exp> {
let (input, _) = opt(parse_comment)(input)?;
let (input, _) = opt(many0(parse_comment))(input)?;
delimited(multispace0, alt((
parse_num, parse_bool, parse_str, parse_list, parse_quote, parse_quasiquote, parse_unquote_splice, parse_unquote, parse_splice, parse_sym
)), multispace0)(input)
)), alt((parse_comment, multispace0)))(input)
}

pub fn parse(input: &str)-> Result<(String, Exp), Err> {
match parse_exp(input) {
Ok((input, exp)) => Ok((input.to_string(), exp)),
Err(Error(err)) if !err.input.is_empty() => {
let line = err.input.lines().next().unwrap();
could_not!("parse '{}'", line)
Err(Error(err)) => {
if err.input.is_empty() {
Ok(("".to_string(), Exp::List(vec![Exp::Sym("quote".to_string()), Exp::List(vec![])])))
} else {
let line = err.input.lines().next().unwrap();
could_not!("parse '{}'", line)
}
}
_ => could_not!("parse input"),
}
Expand Down