Eval enhancements. Rewrote store to be significantly better
This commit is contained in:
parent
20821057f2
commit
3848d3bcfa
6 changed files with 363 additions and 327 deletions
332
src/stl/decl.rs
332
src/stl/decl.rs
|
|
@ -39,7 +39,20 @@ pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
|||
if ast.len() > 1 {
|
||||
Err("do not eval more than one thing at a time".to_string())
|
||||
} else {
|
||||
let arguments: Ctr;
|
||||
match *ast.car {
|
||||
Ctr::Seg(ref s) => arguments = *eval(s, syms)?.clone(),
|
||||
Ctr::Symbol(ref sym) => {
|
||||
let intermediate = syms.call_symbol(sym, &Seg::new(), true)?;
|
||||
if let Ctr::Seg(ref s) = *intermediate {
|
||||
arguments = *eval(s, syms)?.clone()
|
||||
} else {
|
||||
arguments = *intermediate
|
||||
}
|
||||
},
|
||||
_ => arguments = *ast.car.clone()
|
||||
}
|
||||
match arguments {
|
||||
Ctr::Seg(ref s) => Ok(*eval(s, syms)?.clone()),
|
||||
Ctr::Symbol(ref sym) => {
|
||||
let intermediate = syms.call_symbol(sym, &Seg::new(), true)?;
|
||||
|
|
@ -49,7 +62,7 @@ pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
|||
Ok(*intermediate)
|
||||
}
|
||||
},
|
||||
_ => Ok(*ast.car.clone())
|
||||
_ => Ok(arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -87,131 +100,6 @@ CURRENT VALUE AND/OR BODY:
|
|||
Ok(Ctr::None)
|
||||
}
|
||||
|
||||
pub const STORE_DOCSTRING: &str = "allows user to define functions and variables.
|
||||
A call may take one of three forms:
|
||||
1. variable declaration:
|
||||
Takes a name, doc string, and a value.
|
||||
(def myvar 'my special variable' 'my var value')
|
||||
2. function declaration:
|
||||
Takes a name, doc string, list of arguments, and one or more bodies to evaluate.
|
||||
Result of evaluating the final body is returned.
|
||||
(def myfunc 'does a thing' (myarg1 myarg2) (dothing myarg1 myarg2) (add myarg1 myarg2))
|
||||
3. symbol un-definition:
|
||||
Takes just a name. Removes variable from table.
|
||||
(def useless-var)";
|
||||
|
||||
pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
|
||||
let is_var = ast.len() == 3;
|
||||
if let Ctr::Symbol(ref identifier) = *ast.car {
|
||||
match &*ast.cdr {
|
||||
// define a symbol
|
||||
Ctr::Seg(doc_tree) => {
|
||||
if let Ctr::String(ref doc) = *doc_tree.car {
|
||||
match &*doc_tree.cdr {
|
||||
// define a variable
|
||||
Ctr::Seg(data_tree) if is_var => {
|
||||
let eval_arg: &Seg;
|
||||
let outer_maybe_eval_seg: Seg;
|
||||
let mut expand = false;
|
||||
if let Ctr::Seg(ref eval_me) = *data_tree.car {
|
||||
eval_arg = eval_me;
|
||||
} else {
|
||||
outer_maybe_eval_seg = Seg::from_mono(data_tree.car.clone());
|
||||
eval_arg = &outer_maybe_eval_seg;
|
||||
expand = true;
|
||||
}
|
||||
match eval(eval_arg, syms) {
|
||||
Ok(ctr) => {
|
||||
let mut body = ctr;
|
||||
if expand {
|
||||
if let Ctr::Seg(ref s) = *body {
|
||||
body = s.car.clone();
|
||||
} else {
|
||||
return Err("impossible expansion".to_string())
|
||||
}
|
||||
}
|
||||
syms.insert(
|
||||
identifier.clone(),
|
||||
Symbol::from_ast(
|
||||
identifier, doc,
|
||||
&Seg::from_mono(body.clone()), None
|
||||
),
|
||||
);
|
||||
if env_cfg {
|
||||
env::set_var(identifier.clone(), body.to_string());
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(format!("couldnt eval symbol: {}", e)),
|
||||
}
|
||||
},
|
||||
|
||||
// define a function
|
||||
Ctr::Seg(data_tree) if !is_var => {
|
||||
if let Ctr::Seg(ref args) = *data_tree.car {
|
||||
let mut arg_list = vec![];
|
||||
if !args.circuit(&mut |c: &Ctr| -> bool {
|
||||
if let Ctr::Symbol(ref arg) = c {
|
||||
arg_list.push(arg.clone());
|
||||
true
|
||||
} else if let Ctr::None = c {
|
||||
// a user cannot type a None
|
||||
// this case represents no args
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
return Err(
|
||||
"all arguments defined for function must be of type symbol"
|
||||
.to_string(),
|
||||
);
|
||||
};
|
||||
|
||||
if let Ctr::Seg(ref bodies) = *data_tree.cdr {
|
||||
syms.insert(
|
||||
identifier.clone(),
|
||||
Symbol::from_ast(
|
||||
identifier, doc, bodies,
|
||||
Some(arg_list),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return Err(
|
||||
"expected one or more function bodies in function definition"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return Err(
|
||||
"expected list of arguments in function definition".to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// theres a name and a doc string but nothing else
|
||||
_ => return Err("have name and doc string, but no body.".to_string()),
|
||||
}
|
||||
} else {
|
||||
return Err("doc string is a required argument".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// undefine a symbol
|
||||
Ctr::None => {
|
||||
syms.remove(&identifier.to_string());
|
||||
if env_cfg {
|
||||
env::remove_var(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
_ => return Err("arguments not in standard form".to_string()),
|
||||
}
|
||||
} else {
|
||||
return Err("first argument to export must be a symbol".to_string());
|
||||
}
|
||||
Ok(Ctr::None)
|
||||
}
|
||||
|
||||
pub const ISSET_DOCSTRING: &str = "accepts a single argument: a symbol.
|
||||
returns true or false according to whether or not the symbol is found in the symbol table.";
|
||||
|
||||
|
|
@ -299,3 +187,195 @@ pub fn lambda_callback(
|
|||
Err("first argument should be a list of symbols".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub const GETDOC_DOCSTRING: &str = "accepts an unevaluated symbol, returns the doc string.
|
||||
Returns an error if symbol is undefined";
|
||||
|
||||
pub fn getdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
||||
if ast.len() != 1 {
|
||||
Err("get-doc only takes a single argument".to_string())
|
||||
} else {
|
||||
if let Ctr::Symbol(ref symbol) = *ast.car {
|
||||
if let Some(sym) = syms.get(symbol) {
|
||||
Ok(Ctr::String(sym.docs.clone()))
|
||||
} else {
|
||||
Err("undefined symbol".to_string())
|
||||
}
|
||||
|
||||
} else {
|
||||
Err("get-doc should only be called on a symbol".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const SETDOC_DOCSTRING: &str = "accepts a symbol and a doc string.
|
||||
Returns an error if symbol is undefined, otherwise sets the symbols docstring to the argument.";
|
||||
|
||||
pub fn setdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
||||
if ast.len() != 2 {
|
||||
Err("set-doc only takes two arguments".to_string())
|
||||
} else {
|
||||
if let Ctr::Symbol(ref symbol) = *ast.car {
|
||||
if let Some(mut sym) = syms.remove(symbol) {
|
||||
if let Ctr::Seg(ref doc_node) = *ast.cdr {
|
||||
if let Ctr::String(ref doc) = *doc_node.car {
|
||||
sym.docs = doc.clone();
|
||||
syms.insert(sym.name.clone(), sym);
|
||||
Ok(Ctr::None)
|
||||
} else {
|
||||
syms.insert(sym.name.clone(), sym);
|
||||
Err("second arg must be a string".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("impossible: not a second arg".to_string())
|
||||
}
|
||||
} else {
|
||||
Err("undefined symbol".to_string())
|
||||
}
|
||||
|
||||
} else {
|
||||
Err("first argument must be a symbol".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const STORE_DOCSTRING: &str = "allows user to define functions and variables.
|
||||
A call may take one of three forms:
|
||||
1. variable declaration:
|
||||
Takes a name, doc string, and a value.
|
||||
(def myvar 'my special variable' 'my var value')
|
||||
2. function declaration:
|
||||
Takes a name, doc string, list of arguments, and one or more bodies to evaluate.
|
||||
Result of evaluating the final body is returned.
|
||||
(def myfunc 'does a thing' (myarg1 myarg2) (dothing myarg1 myarg2) (add myarg1 myarg2))
|
||||
3. symbol un-definition:
|
||||
Takes just a name. Removes variable from table.
|
||||
(def useless-var)
|
||||
|
||||
Additionally, passing a tree as a name will trigger def to evaluate the tree and try to derive
|
||||
a value from it. If it does not return a ";
|
||||
|
||||
|
||||
pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
|
||||
let is_var = ast.len() == 3;
|
||||
let name: String;
|
||||
let docs: String;
|
||||
|
||||
match *ast.car {
|
||||
Ctr::String(ref s) => name = s.clone(),
|
||||
Ctr::Symbol(ref s) => name = s.clone(),
|
||||
Ctr::Seg(ref s) => match *eval(s, syms)? {
|
||||
Ctr::String(ref s) => name = s.clone(),
|
||||
Ctr::Symbol(ref s) => name = s.clone(),
|
||||
_ => return Err("new symbol name doesnt make sense".to_string()),
|
||||
},
|
||||
_ => return Err("new symbol name doesnt make sense".to_string()),
|
||||
}
|
||||
|
||||
// remove var case
|
||||
if ast.len() == 1 {
|
||||
syms.remove(&name);
|
||||
if env_cfg {
|
||||
env::remove_var(name);
|
||||
}
|
||||
|
||||
return Ok(Ctr::None)
|
||||
} else {
|
||||
if ast.len() < 3 || ast.len() > 4 {
|
||||
return Err("expected 3 or 4 args".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
let mut iter: &Seg;
|
||||
if let Ctr::Seg(ref s) = *ast.cdr {
|
||||
iter = s;
|
||||
} else {
|
||||
return Err("not enough args".to_string())
|
||||
}
|
||||
|
||||
match *iter.car {
|
||||
Ctr::String(ref s) => docs = s.clone(),
|
||||
Ctr::Symbol(ref s) => {
|
||||
if let Ctr::String(doc) = *syms.call_symbol(&s, &Seg::new(), true)? {
|
||||
docs = doc.clone();
|
||||
} else {
|
||||
return Err("docs argument does not evaluate to a string".to_string())
|
||||
}
|
||||
},
|
||||
_ => return Err("docs argument does not evaluate to a string".to_string())
|
||||
}
|
||||
|
||||
if let Ctr::Seg(ref s) = *iter.cdr {
|
||||
iter = s;
|
||||
} else {
|
||||
return Err("not enough args".to_string())
|
||||
}
|
||||
|
||||
let mut outer_scope_val: Seg = Seg::new();
|
||||
let noseg = Seg::new(); // similarly, rust shouldnt need this either
|
||||
let mut args = &noseg;
|
||||
let mut var_val_form: &Seg = &outer_scope_val;
|
||||
let mut expand = false;
|
||||
match *iter.car {
|
||||
Ctr::Seg(ref s) if !is_var => args = s,
|
||||
Ctr::Seg(ref s) if is_var => var_val_form = s,
|
||||
_ if is_var => {
|
||||
expand = true;
|
||||
outer_scope_val = Seg::from_mono(Box::new(*iter.car.clone()));
|
||||
var_val_form = &outer_scope_val;
|
||||
},
|
||||
_ if !is_var => return Err("arg list must at least be a list".to_string()),
|
||||
_ => unimplemented!(), // rustc is haunted
|
||||
}
|
||||
|
||||
if is_var {
|
||||
let var_val: Ctr;
|
||||
let var_eval_result = *eval(var_val_form, syms)?;
|
||||
match var_eval_result {
|
||||
Ctr::Seg(ref s) if expand => var_val = *s.car.clone(),
|
||||
Ctr::Seg(ref s) if !expand => var_val = Ctr::Seg(s.clone()),
|
||||
_ => var_val = var_eval_result,
|
||||
}
|
||||
|
||||
let outer_seg = Seg::from_mono(Box::new(var_val.clone()));
|
||||
syms.insert(
|
||||
name.clone(),
|
||||
Symbol::from_ast(&name, &docs, &outer_seg, None),
|
||||
);
|
||||
if env_cfg {
|
||||
env::set_var(name.clone(), var_val.to_string());
|
||||
}
|
||||
return Ok(Ctr::None)
|
||||
}
|
||||
|
||||
println!("{}", args);
|
||||
|
||||
let mut arg_list = vec![];
|
||||
if !args.circuit(&mut |c: &Ctr| -> bool {
|
||||
if let Ctr::Symbol(s) = c {
|
||||
arg_list.push(s.clone());
|
||||
true
|
||||
} else if let Ctr::None = c {
|
||||
// no args case
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
return Err("all arguments defined for function must be of type symbol".to_string())
|
||||
}
|
||||
|
||||
if let Ctr::Seg(ref eval_bodies) = *iter.cdr {
|
||||
syms.insert(
|
||||
name.clone(),
|
||||
Symbol::from_ast(
|
||||
&name, &docs,
|
||||
eval_bodies,
|
||||
Some(arg_list),
|
||||
),
|
||||
);
|
||||
Ok(Ctr::None)
|
||||
} else {
|
||||
Err("expected one or more bodies to evaluate in function".to_string())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue