/* 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; 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 * 3. if cdr is an AST * - set first_item to false * - set new.cdr to result of process_ctr(ast.ctr) * 4. else call process_ctr on cdr * (and set new.cdr to result) * 5. return new ast */ } fn process_ctr( ctr: Ctr, vars: Rc>, funcs: Rc>, sym_loose: bool, first_item: bool ) -> 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 { Symbol(token) => { let mut tok = token; if let Some(s) = vt.borrow().get(token) { if first_item { if let String(t) = s { tok = t; } // else improper var type so ignore } else { // TODO: DEEP COPY. return } } else if !first_item && !sym_loose { return Err("variable not found") } if first_item && let Some(f) = ft.borrow().get(tok) { // TODO: call function. set return } }, Seg(tree) => eval_inner(tree, vars, funcs, sym_loose, first_item), _ => Ok(ctr.clone()) } }