rustfmt
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
ecbc47d4fe
commit
bc09cb07b1
17 changed files with 236 additions and 217 deletions
146
src/stl.rs
146
src/stl.rs
|
|
@ -15,40 +15,49 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::segment::{Ctr, Seg, Type};
|
||||
use crate::eval::eval;
|
||||
use crate::sym::{SymTable, Symbol, ValueType, Args, UserFn};
|
||||
use crate::segment::{Ctr, Seg, Type};
|
||||
use crate::sym::{Args, SymTable, Symbol, UserFn, ValueType};
|
||||
use std::env;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub mod control;
|
||||
pub mod append;
|
||||
pub mod control;
|
||||
//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::append_callback)),
|
||||
});
|
||||
syms.insert(
|
||||
"append".to_string(),
|
||||
Symbol {
|
||||
name: String::from("append"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
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(
|
||||
"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)),
|
||||
});
|
||||
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(())
|
||||
}
|
||||
|
|
@ -58,43 +67,52 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
|
|||
/// callbacks with configuration into a symtable
|
||||
pub fn dynamic_stdlib(syms: &mut SymTable) -> Result<(), String> {
|
||||
//get CFG_RELISH_ENV from syms
|
||||
let env_cfg_user_form = syms.call_symbol(
|
||||
&"CFG_RELISH_ENV".to_string(), &Seg::new(), true)
|
||||
let env_cfg_user_form = syms
|
||||
.call_symbol(&"CFG_RELISH_ENV".to_string(), &Seg::new(), true)
|
||||
.unwrap_or_else(|_: String| Box::new(Ctr::None))
|
||||
.to_string()
|
||||
.ne("");
|
||||
|
||||
syms.insert("def".to_string(), Symbol {
|
||||
name: String::from("define"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: true,
|
||||
value: ValueType::Internal(Rc::new( move |ast: &Seg, syms: &mut SymTable| -> Result<Ctr, String> {
|
||||
_store_callback(ast, syms, env_cfg_user_form)
|
||||
syms.insert(
|
||||
"def".to_string(),
|
||||
Symbol {
|
||||
name: String::from("define"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: true,
|
||||
value: ValueType::Internal(Rc::new(
|
||||
move |ast: &Seg, syms: &mut SymTable| -> Result<Ctr, String> {
|
||||
_store_callback(ast, syms, env_cfg_user_form)
|
||||
},
|
||||
)),
|
||||
},
|
||||
)),
|
||||
});
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
|
||||
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 {
|
||||
Ctr::Seg(data_tree) if is_var => match eval(&Box::new(data_tree), syms) {
|
||||
Ok(seg) => if let Ctr::Seg(ref val) = *seg {
|
||||
syms.insert(identifier.clone(), Symbol{
|
||||
value: ValueType::VarForm(val.car.clone()),
|
||||
name: identifier.clone(),
|
||||
args: Args::None,
|
||||
conditional_branches: false,
|
||||
});
|
||||
if env_cfg {
|
||||
env::set_var(identifier.clone(), val.car.to_string());
|
||||
Ok(seg) => {
|
||||
if let Ctr::Seg(ref val) = *seg {
|
||||
syms.insert(
|
||||
identifier.clone(),
|
||||
Symbol {
|
||||
value: ValueType::VarForm(val.car.clone()),
|
||||
name: identifier.clone(),
|
||||
args: Args::None,
|
||||
conditional_branches: false,
|
||||
},
|
||||
);
|
||||
if env_cfg {
|
||||
env::set_var(identifier.clone(), val.car.to_string());
|
||||
}
|
||||
} else {
|
||||
return Err("impossible args to export".to_string());
|
||||
}
|
||||
} else {
|
||||
return Err("impossible args to export".to_string());
|
||||
},
|
||||
}
|
||||
Err(e) => return Err(format!("couldnt eval symbol: {}", e)),
|
||||
},
|
||||
Ctr::Seg(data_tree) if !is_var => {
|
||||
|
|
@ -108,24 +126,29 @@ fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr
|
|||
false
|
||||
}
|
||||
}) {
|
||||
return Err("all arguments defined for function must be of type symbol".to_string());
|
||||
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{
|
||||
value: ValueType::FuncForm(UserFn{
|
||||
ast: Box::new(bodies.clone()),
|
||||
arg_syms: arg_list.clone(),
|
||||
}),
|
||||
name: identifier.clone(),
|
||||
args: Args::Strict(arg_list
|
||||
.into_iter()
|
||||
.map(Type::from)
|
||||
.collect()),
|
||||
conditional_branches: false,
|
||||
});
|
||||
syms.insert(
|
||||
identifier.clone(),
|
||||
Symbol {
|
||||
value: ValueType::FuncForm(UserFn {
|
||||
ast: Box::new(bodies.clone()),
|
||||
arg_syms: arg_list.clone(),
|
||||
}),
|
||||
name: identifier.clone(),
|
||||
args: Args::Strict(arg_list.into_iter().map(Type::from).collect()),
|
||||
conditional_branches: false,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return Err("expected one or more function bodies in function definition".to_string());
|
||||
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());
|
||||
|
|
@ -136,12 +159,11 @@ fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr
|
|||
if env_cfg {
|
||||
env::remove_var(identifier);
|
||||
}
|
||||
},
|
||||
_ => return Err("args not in standard form".to_string()),
|
||||
}
|
||||
_ => return Err("args not in standard form".to_string()),
|
||||
}
|
||||
} else {
|
||||
return Err("first argument to export must be a symbol".to_string());
|
||||
return Err("first argument to export must be a symbol".to_string());
|
||||
}
|
||||
Ok(Ctr::None)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue