simplify eval, add member function to symtable to call a symbol

This commit is contained in:
Ava Hahn 2023-02-23 23:01:47 -08:00
parent e055f26e90
commit a1e19a19d9
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
5 changed files with 104 additions and 107 deletions

View file

@ -17,7 +17,7 @@
use crate::segment::{Ctr, Seg, Type};
use crate::eval::eval;
use crate::sym::{SYM_TABLE, Symbol, ValueType, Args, UserFn};
use crate::sym::{SymTable, Symbol, ValueType, Args, UserFn};
use std::env;
/*
@ -44,18 +44,17 @@ pub static LIB_STORE_NO_ENV: Symbol = Symbol {
};*/
// TODO : declare function if arg list is long enough
fn _store_callback (ast: &Seg, env_cfg: bool) -> Ctr {
let mut table_handle = SYM_TABLE.write().unwrap();
fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
let is_var = ast.len() == 2;
if let Ctr::Symbol(ref identifier) = *ast.car {
match &*ast.cdr {
Ctr::Seg(data_tree) if is_var => match eval(&Box::new(data_tree), true, true) {
Ctr::Seg(data_tree) if is_var => match eval(&Box::new(data_tree), syms) {
Ok(seg) => if let Ctr::Seg(ref val) = *seg {
table_handle.insert(identifier.clone(), Symbol{
syms.insert(identifier.clone(), Symbol{
value: ValueType::VarForm(val.car.clone()),
name: identifier.clone(),
args: Args::None,
has_undefined_symbols: false,
conditional_branches: false,
});
if env_cfg {
env::set_var(identifier.clone(), val.car.to_string());
@ -81,7 +80,7 @@ fn _store_callback (ast: &Seg, env_cfg: bool) -> Ctr {
};
if let Ctr::Seg(ref bodies) = *data_tree.cdr {
table_handle.insert(identifier.clone(), Symbol{
syms.insert(identifier.clone(), Symbol{
value: ValueType::FuncForm(UserFn{
ast: Box::new(bodies.clone()),
arg_syms: arg_list.clone(),
@ -91,7 +90,7 @@ fn _store_callback (ast: &Seg, env_cfg: bool) -> Ctr {
.into_iter()
.map(Type::from)
.collect()),
has_undefined_symbols: false,
conditional_branches: false,
});
} else {
eprintln!("expected one or more function bodies in function definition");
@ -103,7 +102,7 @@ fn _store_callback (ast: &Seg, env_cfg: bool) -> Ctr {
}
}
Ctr::None => {
table_handle.remove(&identifier.to_string());
syms.remove(&identifier.to_string());
if env_cfg {
env::remove_var(identifier);
}