big progress on rewrite
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
5261efbc65
commit
c7b30d027d
8 changed files with 1190 additions and 54 deletions
85
the_rewrite/src/vars.rs
Normal file
85
the_rewrite/src/vars.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
/* 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>>> {
|
||||
return self.0.get(&id);
|
||||
}
|
||||
|
||||
pub fn remove(&self, id: String) {
|
||||
self.0.remove(&id);
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
},
|
||||
)),
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue