big temp status

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-01-27 17:45:19 -08:00
parent 45453f819f
commit 5261efbc65
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
12 changed files with 960 additions and 224 deletions

View file

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::segment::{list_append, new_ast, Ast, Ctr};
use crate::segment::{list_append, Ctr, Seg};
const UNMATCHED_STR_DELIM: &str = "Unmatched string delimiter in input";
const UNMATCHED_LIST_DELIM: &str = "Unmatched list delimiter in input";
@ -23,7 +23,7 @@ const UNMATCHED_LIST_DELIM: &str = "Unmatched list delimiter in input";
/* takes a line of user input
* returns an unsimplified tree of tokens.
*/
pub fn lex(document: String) -> Result<Ast, String> {
pub fn lex<'a>(document: String) -> Result<Box<Seg<'a>>, String> {
if !document.is_ascii() {
return Err("document may only contain ascii characters".to_string());
}
@ -42,7 +42,7 @@ pub fn lex(document: String) -> Result<Ast, String> {
* Returns Ok(Rc<Seg>) if lexing passes
* Returns Err(String) if an error occurs
*/
fn process(document: String) -> Result<Ast, String> {
fn process<'a>(document: &'a String) -> Result<Box<Seg<'a>>, String> {
let doc_len = document.len();
if doc_len == 0 {
@ -120,7 +120,7 @@ fn process(document: String) -> Result<Ast, String> {
return Err("list started in middle of another token".to_string());
}
ref_stack.push(new_ast(Ctr::None, Ctr::None));
ref_stack.push(Box::new(Seg::new()));
delim_stack.push(')');
}
@ -152,13 +152,12 @@ fn process(document: String) -> Result<Ast, String> {
return Err("Empty token".to_string());
}
let mut current_seg_ref = ref_stack.pop().unwrap();
let mut current_seg = ref_stack.pop();
let mut obj;
if is_str {
obj = Ctr::String(token);
is_str = false;
token = String::new();
list_append(current_seg_ref.clone(), obj);
} else if token.len() > 0 {
if token == "true" {
obj = Ctr::Bool(true);
@ -175,22 +174,23 @@ fn process(document: String) -> Result<Ast, String> {
}
token = String::new();
list_append(current_seg_ref.clone(), obj);
}
list_append(current_seg, obj);
if alloc_list {
// return if we have finished the document
if ref_stack.len() == 0 {
return Ok(current_seg_ref);
return Ok(current_seg);
}
// shortening this will lead to naught but pain
obj = Ctr::Seg(current_seg_ref.clone());
current_seg_ref = ref_stack.pop().unwrap();
list_append(current_seg_ref.clone(), obj);
obj = Ctr::Seg(current_seg.into_raw());
current_seg = ref_stack.pop();
list_append(current_seg, obj);
}
ref_stack.push(current_seg_ref);
ref_stack.push(current_seg);
}
}