/* 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 .
*/
use std::rc::Rc;
use std::cell::RefCell;
use crate::segment::{Ast, Ctr};
use crate::func::{FTable, func_call};
use crate::vars::VTable;
/* iterates over a syntax tree
* returns a NEW LIST of values
* representing the simplest possible form of the input
*/
pub fn eval(
ast: Ast,
vars: Rc>,
funcs: Rc>,
sym_loose: bool
) -> Result {
eval_inner(ast, vars, funcs, sym_loose, true)
}
fn eval_inner(
ast: Ast,
vars: Rc>,
funcs: Rc>,
sym_loose: bool,
first_item: bool
) -> Result {
/* LOGIC:
* 1. make new ast
* 2. call process_ctr on ast.car
* 3. set new.car to result
* 4. else call process_ctr on cdr
* (set first_item to false)
* (and set new.cdr to result)
* 5. return new ast
*/
let ret: Ast;
let mret = ret.clone().borrow_mut();
let ast_inner = ast.clone().borrow_mut();
match process_ctr(
ast_inner.car,
vars.clone(),
funcs.clone(),
sym_loose,
first_item,
true,
mret.cdr
) {
Ok(ctr) => {
mret.car = ctr;
},
Err(err) => {
return Err(err)
}
}
match process_ctr(
ast_inner.cdr,
vars.clone(),
funcs.clone(),
sym_loose,
false,
false,
Ctr::None
) {
Ok(ctr) => {
mret.cdr = ctr;
},
Err(err) => {
return Err(err)
}
}
return Ok(ret)
}
fn process_ctr(
ctr: Ctr,
vars: Rc>,
funcs: Rc>,
sym_loose: bool,
first_item: bool,
is_car: bool,
rest: Ctr
) -> Result {
/* LOGIC:
* 1. if symbol, unwrap (DEEP COPY)
* 2. if first_item and symbol, call function?
* (else: return var copy)
* 3. if list return result of eval()
* 4. finally, return a clone (shallow copy) of datum
*/
match ctr {
Ctr::Symbol(token) => {
let mut tok = token;
if let Some(s) = vars.borrow().get(&tok) {
// is function, or variable alias?
if first_item {
if let Ctr::Symbol(t) = *s.clone() {
if let Some(f) = funcs.borrow().get(&t) {
// leave this set for the function call case below
tok = t;
}
}
// is a basic value.
} else {
return Ok(*s.clone())
}
// variable not found case
} else if !first_item && !sym_loose {
return Err(format!("variable '{}' not found", tok).to_string())
}
// function call case
if first_item {
if let Some(f) = funcs.borrow().get(&tok) {
if let Ctr::Seg(args) = rest {
match func_call(*f, args, vars.clone(), funcs.clone()) {
Ok(a) => return Ok(a.clone()),
Err(s) => return Err(s)
}
} else {
return Err("evaluation: args to function not a list".to_string())
}
} else {
return Err(format!("function '{}' not defined", tok).to_string())
}
// sym_loose and not function call
} else {
return Ok(ctr.clone())
}
},
Ctr::Seg(tree) => {
// if list is_car then we need to set first_item to true
match eval_inner(tree, vars, funcs, sym_loose, is_car) {
Ok(ast) => {
Ok(Ctr::Seg(ast))
},
Err(e) => {
Err(e)
}
}
},
_ => Ok(ctr.clone())
}
}