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

@ -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),

View file

@ -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)?;

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())
}