diff --git a/src/func.rs b/src/func.rs index b570757..7e6ac27 100644 --- a/src/func.rs +++ b/src/func.rs @@ -26,7 +26,7 @@ use crate::eval::eval; pub type FTable = HashMap>>; // Standardized function signature for stdlib functions -pub type InternalOperation = fn(Ast, Rc>, Rc>) -> Ctr; +pub type InternalOperation = impl Fn(Ast, Rc>, Rc>) -> Ctr; pub struct ExternalOperation { // Un-evaluated abstract syntax tree // TODO: Intermediate evaluation to simplify branches with no argument in them diff --git a/src/vars.rs b/src/vars.rs index 24fb12d..833bfb7 100644 --- a/src/vars.rs +++ b/src/vars.rs @@ -19,7 +19,7 @@ use std::cell::RefCell; use std::rc::Rc; use std::collections::HashMap; use std::env; -use crate::segment::{Ctr, Ast}; +use crate::segment::{Ctr, Ast, ast_to_string}; use crate::eval::eval; use crate::func::{Function, Operation, Args, FTable}; /* Mapping between a string token and a tree of Segments @@ -52,40 +52,45 @@ pub fn get_export(env_cfg: bool) -> Function { Ctr::Symbol(identifier) => { match &inner.cdr { Ctr::Seg(tree) => { - if let Ok(seg) = eval(tree.clone(), b.clone(), c.clone(), false) { - match seg { - Ctr::Seg(val) => { - let val_tmp = val.borrow().clone(); - define(b, identifier.to_string(), Rc::new(val_tmp.car)); - if env_cfg { - // set var in env - // gotta distill value - // env::set_var(identifier, VALUE) - } - }, + match eval(tree.clone(), b.clone(), c.clone(), false) { + Ok(seg) => { + match seg { + Ctr::Seg(val) => { + let val_tmp = val.borrow().clone(); + define(b, identifier.to_string(), Rc::new(val_tmp.car)); + if env_cfg { + match val_tmp.car { + Ctr::Symbol(s) => env::set_var(identifier, s), + Ctr::String(s) => env::set_var(identifier, s), + Ctr::Integer(i) => env::set_var(identifier, format!("{}", i)), + Ctr::Float(f) => env::set_var(identifier, format!("{}", f)), + Ctr::Bool(b) => env::set_var(identifier, format!("{}", b)), + Ctr::Seg(c) => env::set_var(identifier, ast_to_string(c)), + Ctr::None => () + } + } + }, - _ => { - eprintln!("impossible args to export"); + _ => eprintln!("impossible args to export") } - } - }, + }, - Ctr::None => { - // UNSET VAR LOGIC + Err(e) => eprintln!("couldnt eval symbol: {}", e) } }, - _ => { - eprintln!("args not in standard form"); - } + Ctr::None => { + (*b).borrow_mut().remove(identifier); + }, + + _ => eprintln!("args not in standard form") } - return Ctr::None; }, - _ => { - eprintln!("first argument to export must be a symbol"); - return Ctr::None; - } + + _ => eprintln!("first argument to export must be a symbol") } + + return Ctr::None; } ) };