WIP Commit for stackoverflow

This commit is contained in:
Aidan Hahn 2021-12-30 21:14:24 -08:00
parent a967ac5c3e
commit 4cadb1a7eb
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 47 additions and 39 deletions

View file

@ -21,7 +21,7 @@ use std::collections::HashMap;
use std::env;
use crate::segment::{Ctr, Ast, ast_to_string};
use crate::eval::eval;
use crate::func::{Function, Operation, Args, FTable};
use crate::func::{Function, Operation, InternalOperation, Args, FTable};
/* Mapping between a string token and a tree of Segments
* The string token can be found in any Ctr::Symbol value
* it is expected that the trees stored are already evaluated
@ -45,53 +45,58 @@ pub fn get_export(env_cfg: bool) -> Function {
loose_syms: true,
eval_lazy: true,
args: Args::Lazy(2),
function: Operation::Internal(
|a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow_mut();
match &inner.car {
Ctr::Symbol(identifier) => {
match &inner.cdr {
Ctr::Seg(tree) => {
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 => ()
}
}
},
function: Operation::Internal(get_export_callback(env_cfg)),
};
}
_ => eprintln!("impossible args to export")
fn get_export_callback(env_cfg: bool) -> InternalOperation {
|a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow_mut();
match &inner.car {
Ctr::Symbol(identifier) => {
match &inner.cdr {
Ctr::Seg(tree) => {
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 => ()
}
}
},
Err(e) => eprintln!("couldnt eval symbol: {}", e)
_ => eprintln!("impossible args to export")
}
},
Ctr::None => {
(*b).borrow_mut().remove(identifier);
},
_ => eprintln!("args not in standard form")
Err(e) => eprintln!("couldnt eval symbol: {}", e)
}
},
_ => eprintln!("first argument to export must be a symbol")
}
Ctr::None => {
(*b).borrow_mut().remove(identifier);
if env_cfg {
// TODO: unset variable
}
},
return Ctr::None;
}
)
};
_ => eprintln!("args not in standard form")
}
},
_ => eprintln!("first argument to export must be a symbol")
}
return Ctr::None;
}
}