fix the many-body-script and lex-singlet problems

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-03-01 12:20:43 -08:00
parent bc09cb07b1
commit 914bf1303f
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
3 changed files with 20 additions and 27 deletions

View file

@ -28,7 +28,15 @@ pub fn lex(document: &String) -> Result<Box<Seg>, 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<Box<Seg>, 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<Box<Seg>, 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<Box<Seg>, 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::<i128>() {
obj = Box::from(Ctr::Integer(i));
} else if let Ok(f) = token.parse::<f64>() {
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())
}