implement basic control flow, error handling from functions, many tests

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-02-27 22:53:54 -08:00
parent ae365ad63c
commit 09e3546ba6
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
14 changed files with 315 additions and 488 deletions

View file

@ -21,35 +21,56 @@ use crate::sym::{SymTable, Symbol, ValueType, Args, UserFn};
use std::env;
use std::rc::Rc;
fn store_stdlib(env: bool, syms: &mut SymTable) -> Result<(), String> {
syms.insert("def".to_string(), Symbol {
name: String::from("export"),
args: Args::Lazy(2),
conditional_branches: false,
value: ValueType::Internal(Rc::new( move |ast: &Seg, syms: &mut SymTable| -> Ctr {
_store_callback(ast, syms, env)
},
)),
});
pub mod control;
pub mod append;
//pub mod str;
/// static_stdlib
/// inserts all stdlib functions that can be inserted without
/// any kind of further configuration data into a symtable
pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
syms.insert("append".to_string(), Symbol {
name: String::from("append"),
args: Args::Infinite,
conditional_branches: false,
value: ValueType::Internal(Rc::new(_append_callback)),
value: ValueType::Internal(Rc::new(append::append_callback)),
});
syms.insert("expand".to_string(), Symbol {
name: String::from("expand"),
args: Args::Strict(vec![Type::Seg]),
conditional_branches: false,
value: ValueType::Internal(Rc::new(append::expand_callback)),
});
syms.insert("if".to_string(), Symbol {
name: String::from("if"),
args: Args::Lazy(3),
conditional_branches: true,
value: ValueType::Internal(Rc::new(control::if_callback)),
});
Ok(())
}
/// dynamic_stdlib
/// takes configuration data and uses it to insert dynamic
/// callbacks with configuration into a symtable
pub fn dynamic_stdlib(env: bool, syms: &mut SymTable) -> Result<(), String> {
syms.insert("def".to_string(), Symbol {
name: String::from("export"),
args: Args::Lazy(2),
conditional_branches: false,
value: ValueType::Internal(Rc::new( move |ast: &Seg, syms: &mut SymTable| -> Result<Ctr, String> {
_store_callback(ast, syms, env)
},
)),
});
fn _append_callback (_ast: &Seg, _syms: &mut SymTable) -> Ctr {
// if car is a list, append cdr
// otherwise create a list out of all arguments
todo!()
Ok(())
}
fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
let is_var = ast.len() == 2;
if let Ctr::Symbol(ref identifier) = *ast.car {
match &*ast.cdr {
@ -65,9 +86,9 @@ fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
env::set_var(identifier.clone(), val.car.to_string());
}
} else {
eprintln!("impossible args to export")
return Err("impossible args to export".to_string());
},
Err(e) => eprintln!("couldnt eval symbol: {}", e),
Err(e) => return Err(format!("couldnt eval symbol: {}", e)),
},
Ctr::Seg(data_tree) if !is_var => {
if let Ctr::Seg(ref args) = *data_tree.car {
@ -80,8 +101,7 @@ fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
false
}
}) {
eprintln!("all arguments defined for function must be of type symbol");
return Ctr::None;
return Err("all arguments defined for function must be of type symbol".to_string());
};
if let Ctr::Seg(ref bodies) = *data_tree.cdr {
@ -98,12 +118,10 @@ fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
conditional_branches: false,
});
} else {
eprintln!("expected one or more function bodies in function definition");
return Ctr::None;
return Err("expected one or more function bodies in function definition".to_string());
}
} else {
eprintln!("expected list of arguments in function definition");
return Ctr::None;
return Err("expected list of arguments in function definition".to_string());
}
}
Ctr::None => {
@ -112,11 +130,11 @@ fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
env::remove_var(identifier);
}
},
_ => eprintln!("args not in standard form"),
_ => return Err("args not in standard form".to_string()),
}
} else {
eprintln!("first argument to export must be a symbol");
return Err("first argument to export must be a symbol".to_string());
}
Ctr::None
Ok(Ctr::None)
}