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

improved count_parenthesis() #503

Merged
merged 1 commit into from
May 20, 2024
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
19 changes: 17 additions & 2 deletions elisp/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ fn read(exp: &[Expression], env: &Environment, stream: &mut dyn BufRead) -> Resu
}
expression.push(buffer.trim().to_string());
let lisp = expression.join(" ");

if !count_parenthesis(&lisp) {
let (left, right) = count_parenthesis(&lisp);
if left > right {
continue;
}
let token = tokenize(&lisp);
Expand Down Expand Up @@ -186,6 +186,14 @@ mod tests {
writeln!(file, "(define fuga (+ foo hoge))").unwrap();
writeln!(file, "(define a 100)(define b 200)(define c 300)").unwrap();
writeln!(file, "(define d 100)").unwrap();
writeln!(file, "(define s1 \"(\"))").unwrap();
writeln!(file, "(define s2 \")\"))").unwrap();
writeln!(file, "(define c1 #\\()").unwrap();
writeln!(file, "(define c2 #\\))").unwrap();
writeln!(file, "(define (testf a b)\n(+ a b))").unwrap();
writeln!(file, "(define (teststr)\"(\")").unwrap();
writeln!(file, "(define (testchr)\n#\\()").unwrap();

file.flush().unwrap();

let env = lisp::Environment::new();
Expand All @@ -195,6 +203,13 @@ mod tests {
assert_eq!(do_lisp_env("hoge", &env), "200");
assert_eq!(do_lisp_env("fuga", &env), "300");
assert_eq!(do_lisp_env("(+ a b c)", &env), "600");
assert_eq!(do_lisp_env("s1", &env), "\"(\"");
assert_eq!(do_lisp_env("s2", &env), "\")\"");
assert_eq!(do_lisp_env("c1", &env), "#\\(");
assert_eq!(do_lisp_env("c2", &env), "#\\)");
assert_eq!(do_lisp_env("(testf 10 20)", &env), "30");
assert_eq!(do_lisp_env("(teststr)", &env), "\"(\"");
assert_eq!(do_lisp_env("(testchr)", &env), "#\\(");
}
#[test]
fn display() {
Expand Down
46 changes: 32 additions & 14 deletions elisp/src/lisp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,8 @@ pub fn repl(
}
program.push(buffer.trim().to_string());
let lisp = program.join(" ");
if !count_parenthesis(&lisp) {
let (left, right) = count_parenthesis(&lisp);
if left > right {
continue;
}
break lisp;
Expand All @@ -736,25 +737,43 @@ pub fn repl(
}
Ok(())
}
pub fn count_parenthesis(program: &str) -> bool {
pub fn count_parenthesis(program: &str) -> (i32, i32) {
#[derive(PartialEq)]
enum CharMode {
Init,
Whitespace,
Sharp,
Backslash,
}
let mut left = 0;
let mut right = 0;
let mut search = true;
let mut str_mode = false;
let mut char_mode = CharMode::Whitespace;
let mut pre = ' ';

for c in program.chars() {
if c == '"' && search {
search = false;
} else if c == '"' && !search {
search = true;
}
if c == '(' && search {
if str_mode {
if pre != '\\' && c == '"' {
str_mode = false;
}
} else if char_mode == CharMode::Backslash {
char_mode = CharMode::Init;
} else if pre != '\\' && c == '"' {
str_mode = true;
} else if c.is_whitespace() && char_mode == CharMode::Init {
char_mode = CharMode::Whitespace;
} else if c == '#' && char_mode == CharMode::Whitespace {
char_mode = CharMode::Sharp;
} else if c == '\\' && char_mode == CharMode::Sharp {
char_mode = CharMode::Backslash;
} else if c == '(' {
left += 1;
}
if c == ')' && search {
} else if c == ')' {
right += 1;
}
pre = c;
}
left <= right
(left, right)
}
pub fn do_core_logic(program: &str, env: &Environment) -> ResultExpression {
let mut token = tokenize(program);
Expand Down Expand Up @@ -907,14 +926,13 @@ pub(crate) fn tokenize(program: &str) -> Vec<String> {
token.quote_mode = false;
}
}
' ' | '\r' | '\n' | '\t' => {}
_ => {
if c == '#'
&& i + 1 < program.chars().count()
&& program.chars().nth(i + 1).unwrap() == '('
{
vector_mode = true;
} else {
} else if !c.is_whitespace() {
set_token_name!(i, c);
}
}
Expand Down