cleanup and break out functions
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
e6bb732f18
commit
6dcb804d34
2 changed files with 33 additions and 30 deletions
|
|
@ -74,7 +74,7 @@ pub struct ExternalOperation<'a> {
|
||||||
* or a syntax tree to eval with the arguments
|
* or a syntax tree to eval with the arguments
|
||||||
*/
|
*/
|
||||||
pub enum Operation<'a> {
|
pub enum Operation<'a> {
|
||||||
Internal(Box<dyn for<'b> Fn(&'b Seg, &'b mut VTable, &'b mut FTable) -> Ctr<'b>>),
|
Internal(Box<dyn Fn(&'a Seg, &'a mut VTable, &'a mut FTable) -> Ctr<'a>>),
|
||||||
External(ExternalOperation<'a>),
|
External(ExternalOperation<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,7 +88,6 @@ pub enum Args {
|
||||||
Strict(Vec<Type>),
|
Strict(Vec<Type>),
|
||||||
}
|
}
|
||||||
|
|
||||||
// function which does not need args checked
|
|
||||||
pub struct Function<'a> {
|
pub struct Function<'a> {
|
||||||
pub function: Operation<'a>,
|
pub function: Operation<'a>,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
||||||
|
|
@ -51,42 +51,46 @@ impl<'a> VTable<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns a callback for the stdlib var export function with env_sync on or off
|
// returns a callback for the stdlib var export function with env_sync on or off
|
||||||
pub fn get_export<'a> (env_cfg: bool) -> Function<'a> {
|
pub fn get_export<'a>(env_cfg: bool) -> Function<'a>{
|
||||||
return Function {
|
return Function {
|
||||||
name: String::from("export"),
|
name: String::from("export"),
|
||||||
loose_syms: true,
|
loose_syms: true,
|
||||||
eval_lazy: true,
|
eval_lazy: true,
|
||||||
args: Args::Lazy(2),
|
args: Args::Lazy(2),
|
||||||
function: Operation::Internal(Box::new(
|
function: Operation::Internal(Box::new(
|
||||||
|ast: &Seg, vars: &mut VTable, funcs: &mut FTable| -> Ctr {
|
move |ast: &Seg, vars: &mut VTable, funcs: &mut FTable| -> Ctr {
|
||||||
if let Ctr::Symbol(identifier) = *ast.car {
|
_export_callback(ast, vars, funcs, env_cfg)
|
||||||
match *ast.cdr {
|
|
||||||
Ctr::Seg(ref data_tree) => match eval(&Box::new(data_tree), vars, funcs, false) {
|
|
||||||
Ok(seg) => match *seg {
|
|
||||||
Ctr::Seg(val) => {
|
|
||||||
vars.insert(identifier, val.car);
|
|
||||||
if env_cfg {
|
|
||||||
env::set_var(identifier, val.car.to_string())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => eprintln!("impossible args to export"),
|
|
||||||
},
|
|
||||||
Err(e) => eprintln!("couldnt eval symbol: {}", e),
|
|
||||||
},
|
|
||||||
Ctr::None => {
|
|
||||||
vars.remove(identifier);
|
|
||||||
if env_cfg {
|
|
||||||
env::remove_var(identifier);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => eprintln!("args not in standard form"),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
eprintln!("first argument to export must be a symbol");
|
|
||||||
}
|
|
||||||
return Ctr::None;
|
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn _export_callback<'a> (ast: &'a Seg, vars: &'a mut VTable, funcs: &'a mut FTable, env_cfg: bool) -> Ctr<'a> {
|
||||||
|
if let Ctr::Symbol(ref identifier) = *ast.car {
|
||||||
|
match *ast.cdr {
|
||||||
|
Ctr::Seg(data_tree) => match eval(&Box::new(data_tree), vars, funcs, false) {
|
||||||
|
Ok(seg) => match *seg {
|
||||||
|
Ctr::Seg(val) => {
|
||||||
|
vars.insert(identifier.clone(), val.car);
|
||||||
|
if env_cfg {
|
||||||
|
env::set_var(identifier.clone(), val.car.to_string())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => eprintln!("impossible args to export"),
|
||||||
|
},
|
||||||
|
Err(e) => eprintln!("couldnt eval symbol: {}", e),
|
||||||
|
},
|
||||||
|
Ctr::None => {
|
||||||
|
vars.remove(identifier.to_string());
|
||||||
|
if env_cfg {
|
||||||
|
env::remove_var(identifier.to_string());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => eprintln!("args not in standard form"),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eprintln!("first argument to export must be a symbol");
|
||||||
|
}
|
||||||
|
return Ctr::None;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue