2023-01-30 23:37:28 -08:00
|
|
|
/* relish: versatile lisp shell
|
|
|
|
|
* Copyright (C) 2021 Aidan Hahn
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use crate::eval::eval;
|
|
|
|
|
use crate::func::{Args, FTable, Function, Operation};
|
|
|
|
|
use crate::segment::{Seg, Ctr};
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::env;
|
|
|
|
|
/* 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
|
|
|
|
|
*/
|
|
|
|
|
pub struct VTable<'a>(HashMap<String, Box<Ctr<'a>>>);
|
|
|
|
|
|
|
|
|
|
impl<'a> VTable<'a> {
|
|
|
|
|
// WARNING: make sure var_tree is properly evaluated before storing
|
|
|
|
|
pub fn insert(&'a mut self, identifier: String, data: Box<Ctr<'a>>) {
|
|
|
|
|
if let Some(datum) = self.0.insert(identifier, data) {
|
|
|
|
|
drop(datum);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&'a self, id: String) -> Option<Box<Ctr<'a>>> {
|
2023-02-01 19:59:46 -08:00
|
|
|
match self.0.get(&id) {
|
|
|
|
|
Some(s) => Some(s.clone()),
|
|
|
|
|
None => None,
|
|
|
|
|
}
|
2023-01-30 23:37:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn remove(&self, id: String) {
|
|
|
|
|
self.0.remove(&id);
|
|
|
|
|
}
|
2023-02-01 19:59:46 -08:00
|
|
|
|
|
|
|
|
pub fn new() -> VTable<'a> {
|
|
|
|
|
VTable{0: HashMap::<String, Box<Ctr>>::new()}
|
|
|
|
|
}
|
2023-01-30 23:37:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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> {
|
|
|
|
|
return Function {
|
|
|
|
|
name: String::from("export"),
|
|
|
|
|
loose_syms: true,
|
|
|
|
|
eval_lazy: true,
|
|
|
|
|
args: Args::Lazy(2),
|
|
|
|
|
function: Operation::Internal(Box::new(
|
|
|
|
|
|ast: &Seg, vars: &mut VTable, funcs: &mut FTable| -> Ctr {
|
|
|
|
|
if let Ctr::Symbol(identifier) = *ast.car {
|
|
|
|
|
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;
|
|
|
|
|
},
|
|
|
|
|
)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|