- fixed lexing of inline and postline comments

This commit is contained in:
Aidan 2021-01-25 20:55:16 -08:00
parent 34573a999e
commit bcb32b19d4
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 18 additions and 12 deletions

View file

@ -59,6 +59,7 @@ pub fn cons (l_ctr: Ctr, r_ctr: Ctr) -> Cell {
*/ */
pub fn cell_as_string(c: &Cell, with_parens: bool) -> String { pub fn cell_as_string(c: &Cell, with_parens: bool) -> String {
let mut string = String::new(); let mut string = String::new();
let mut prn_space = true;
match &c.car { match &c.car {
Ctr::SYMBOL(s) => string.push_str(&s), Ctr::SYMBOL(s) => string.push_str(&s),
Ctr::STRING(s) => { Ctr::STRING(s) => {
@ -70,10 +71,13 @@ pub fn cell_as_string(c: &Cell, with_parens: bool) -> String {
Ctr::FLOAT(f) => string = string + &f.to_string(), Ctr::FLOAT(f) => string = string + &f.to_string(),
Ctr::BOOL(b) => string = string + &b.to_string(), Ctr::BOOL(b) => string = string + &b.to_string(),
Ctr::CELL(c) => string.push_str(cell_as_string(&c, true).as_str()), Ctr::CELL(c) => string.push_str(cell_as_string(&c, true).as_str()),
Ctr::None => string.push_str("nil") Ctr::None => prn_space = false
} }
if prn_space {
string.push(' '); string.push(' ');
}
match &c.cdr { match &c.cdr {
Ctr::SYMBOL(s) => string.push_str(&s), Ctr::SYMBOL(s) => string.push_str(&s),
Ctr::STRING(s) => { Ctr::STRING(s) => {
@ -85,7 +89,11 @@ pub fn cell_as_string(c: &Cell, with_parens: bool) -> String {
Ctr::FLOAT(f) => string = string + &f.to_string(), Ctr::FLOAT(f) => string = string + &f.to_string(),
Ctr::BOOL(b) => string = string + &b.to_string(), Ctr::BOOL(b) => string = string + &b.to_string(),
Ctr::CELL(c) => string.push_str(cell_as_string(&c, false).as_str()), Ctr::CELL(c) => string.push_str(cell_as_string(&c, false).as_str()),
Ctr::None => string.push_str("nil") Ctr::None => {
if prn_space {
string.pop();
}
}
} }
// TODO: maybe a better way to do this // TODO: maybe a better way to do this

View file

@ -85,6 +85,7 @@ fn process(document: String) -> Result<Box<Cell>, String> {
needs_alloc = true; needs_alloc = true;
// reset comment line status // reset comment line status
if delim == '\n' { if delim == '\n' {
delim_stack.pop();
ign = false; ign = false;
continue; continue;
} }

View file

@ -4,10 +4,9 @@ mod lex_tests {
#[test] #[test]
fn test_lex_basic_pair() { fn test_lex_basic_pair() {
let document: &str = "(hello 'world')"; let document: &str = "(hello 'world')";
let output: &str = "(hello 'world' nil)";
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(box_cell) => { Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string()); assert_eq!(format!("{}", *box_cell), document);
}, },
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
@ -19,10 +18,9 @@ mod lex_tests {
#[test] #[test]
fn test_lex_basic_list() { fn test_lex_basic_list() {
let document: &str = "(hello 'world' 1 2 3)"; let document: &str = "(hello 'world' 1 2 3)";
let output: &str = "(hello 'world' 1 2 3 nil)";
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(box_cell) => { Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string()); assert_eq!(format!("{}", *box_cell), document);
}, },
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
@ -34,10 +32,9 @@ mod lex_tests {
#[test] #[test]
fn test_lex_complex_list() { fn test_lex_complex_list() {
let document: &str = "(hello 'world' (1 2 (1 2 3)) 1 2 3)"; let document: &str = "(hello 'world' (1 2 (1 2 3)) 1 2 3)";
let output: &str = "(hello 'world' (1 2 (1 2 3 nil) nil) 1 2 3 nil)";
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(box_cell) => { Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string()); assert_eq!(format!("{}", *box_cell), document);
}, },
Err(s) => { Err(s) => {
print!("{}\n", s); print!("{}\n", s);
@ -94,7 +91,7 @@ mod lex_tests {
#[test] #[test]
fn test_comment() { fn test_comment() {
let document: &str = "#!/bin/relish\n(one two)"; let document: &str = "#!/bin/relish\n(one two)";
let output: &str = "(one two nil)"; let output: &str = "(one two)";
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(box_cell) => { Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string()); assert_eq!(format!("{}", *box_cell), output.to_string());
@ -109,7 +106,7 @@ mod lex_tests {
#[test] #[test]
fn test_postline_comment() { fn test_postline_comment() {
let document: &str = "#!/bin/relish\n((one two)# another doc comment\n(three four))"; let document: &str = "#!/bin/relish\n((one two)# another doc comment\n(three four))";
let output: &str = "(one two nil)"; let output: &str = "((one two) (three four))";
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(box_cell) => { Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string()); assert_eq!(format!("{}", *box_cell), output.to_string());
@ -124,7 +121,7 @@ mod lex_tests {
#[test] #[test]
fn test_inline_comment() { fn test_inline_comment() {
let document: &str = "#!/bin/relish\n((one two)\n# another doc comment\nthree)"; let document: &str = "#!/bin/relish\n((one two)\n# another doc comment\nthree)";
let output: &str = "(one two nil)"; let output: &str = "((one two) three)";
match lex(document.to_string()) { match lex(document.to_string()) {
Ok(box_cell) => { Ok(box_cell) => {
assert_eq!(format!("{}", *box_cell), output.to_string()); assert_eq!(format!("{}", *box_cell), output.to_string());