diff --git a/src/bin/main.rs b/src/bin/main.rs index 376c2fd..e0b3fc7 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -62,7 +62,8 @@ fn main() { match user_doc { Ok(line) => { rl.add_history_entry(line.as_str()); - let l = line.as_str().to_owned(); + let l = line.as_str().to_owned(); + match lex(&l) { Ok(a) => match eval(&a, &mut syms) { Ok(a) => println!("{}", a), diff --git a/src/config.rs b/src/config.rs index ed2d9d7..5b37bdf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -59,8 +59,14 @@ pub fn configure(filename: String, syms: &mut SymTable) -> Result<(), String> { }, ); - let config_document = - fs::read_to_string(filename).unwrap_or_else(|err: io::Error| err.to_string()); + let mut config_document = fs::read_to_string(filename) + .unwrap_or_else(|err: io::Error| { + eprintln!("{}", err.to_string()); + "".to_string() + }) + ")"; + + config_document = "(".to_string() + &config_document; + let config_tree = lex(&config_document)?; let config_result = eval(&config_tree, syms)?; diff --git a/src/lex.rs b/src/lex.rs index 91d32c7..eba3943 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -28,7 +28,15 @@ pub fn lex(document: &String) -> Result, String> { return Err("document may only contain ascii characters".to_string()); } - let tree = process(document); + let mut document_normal = document.clone(); + if !document_normal.ends_with(')') { + document_normal = document_normal + ")"; + } + if !document_normal.starts_with('(') { + document_normal = "(".to_string() + &document_normal; + } + + let tree = process(&document_normal); // TODO: Make multiple forms of Ok() // To represent the multiple passable outcomes @@ -151,7 +159,7 @@ fn process(document: &String) -> Result, String> { return Err("Empty token".to_string()); } - let mut current_seg = ref_stack.pop().unwrap_or(Seg::new()); + let mut current_seg = ref_stack.pop().unwrap(); let obj; if is_str { obj = Box::from(Ctr::String(token)); @@ -185,12 +193,6 @@ fn process(document: &String) -> Result, String> { let t = current_seg; current_seg = ref_stack.pop().unwrap(); - /* TODO: is there a way to do this that doesnt - * involve needlessly copying heap data? I am - * not sure what optimizations rustc performs - * but I assume this should not end up copying - * contained segments around. - */ current_seg.append(Box::from(Ctr::Seg(t))); } @@ -200,22 +202,6 @@ fn process(document: &String) -> Result, String> { if is_str { Err(UNMATCHED_STR_DELIM.to_string()) - } else if ref_stack.is_empty() { - let obj; - if token == "true" { - obj = Box::from(Ctr::Bool(true)); - } else if token == "false" { - obj = Box::from(Ctr::Bool(false)); - } else if let Ok(i) = token.parse::() { - obj = Box::from(Ctr::Integer(i)); - } else if let Ok(f) = token.parse::() { - obj = Box::from(Ctr::Float(f)); - } else if let Some(s) = tok_is_symbol(&token) { - obj = Box::from(Ctr::Symbol(s)); - } else { - return Err(format!("Unparsable token: {}", token)); - } - Ok(Box::new(Seg::from_mono(obj))) } else { Err(UNMATCHED_LIST_DELIM.to_string()) }