add variable export function

This commit is contained in:
Aidan Hahn 2021-11-06 15:43:42 -07:00
parent 7ca42f18da
commit 0931fbdcf0
No known key found for this signature in database
GPG key ID: 327711E983899316
5 changed files with 102 additions and 6 deletions

View file

@ -18,8 +18,8 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::HashMap;
use crate::segment::{Ctr};
use crate::segment::{Ctr, Ast};
use crate::func::{Function, Operation, 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
@ -36,3 +36,27 @@ pub fn define(
drop(rc_segment);
}
}
pub fn get_export() -> Function {
return Function{
name: String::from("export"),
loose_syms: true,
eval_lazy: false,
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) => {
define(b, identifier.to_string(), Rc::new(inner.cdr.clone()));
return Ctr::None;
},
_ => {
eprintln!("first argument to export must be a symbol");
return Ctr::None;
}
}
}
)
};
}