add echo function

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

View file

@ -28,14 +28,8 @@ pub fn lex(document: &String) -> Result<Box<Seg>, String> {
return Err("document may only contain ascii characters".to_string());
}
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;
}
// finish a singlet token, or do nothing
let document_normal = document.clone() + " ";
let tree = process(&document_normal);
// TODO: Make multiple forms of Ok()
@ -159,7 +153,11 @@ fn process(document: &String) -> Result<Box<Seg>, String> {
return Err("Empty token".to_string());
}
let mut current_seg = ref_stack.pop().unwrap();
let mut return_singlet = false;
let mut current_seg = ref_stack.pop().unwrap_or_else(|| {
return_singlet = true;
Seg::new()
});
let obj;
if is_str {
obj = Box::from(Ctr::String(token));
@ -185,7 +183,7 @@ fn process(document: &String) -> Result<Box<Seg>, String> {
current_seg.append(obj.clone());
}
if alloc_list {
if alloc_list || return_singlet {
// return if we have finished the document
if ref_stack.is_empty() {
return Ok(Box::new(current_seg));