2021-02-14 16:33:17 -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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-14 16:14:57 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
use std::cell::RefCell;
|
2021-03-16 22:43:46 -07:00
|
|
|
use crate::segment::{Ast, Ctr};
|
2021-05-25 09:37:23 -07:00
|
|
|
use crate::func::{FTable, func_call};
|
2021-02-14 16:33:17 -08:00
|
|
|
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(
|
2021-03-16 22:43:46 -07:00
|
|
|
ast: Ast,
|
|
|
|
|
vars: Rc<RefCell<VTable>>,
|
|
|
|
|
funcs: Rc<RefCell<FTable>>,
|
|
|
|
|
sym_loose: bool
|
2021-03-14 16:14:57 -07:00
|
|
|
) -> Result<Ast, String> {
|
2021-03-16 22:43:46 -07:00
|
|
|
eval_inner(ast, vars, funcs, sym_loose, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eval_inner(
|
|
|
|
|
ast: Ast,
|
|
|
|
|
vars: Rc<RefCell<VTable>>,
|
|
|
|
|
funcs: Rc<RefCell<FTable>>,
|
|
|
|
|
sym_loose: bool,
|
|
|
|
|
first_item: bool
|
|
|
|
|
) -> Result<Ast, String> {
|
2021-03-17 00:03:43 -07:00
|
|
|
/* LOGIC:
|
2021-03-16 22:43:46 -07:00
|
|
|
* 1. make new ast
|
|
|
|
|
* 2. call process_ctr on ast.car
|
|
|
|
|
* 3. set new.car to result
|
|
|
|
|
* 4. else call process_ctr on cdr
|
2021-05-31 23:06:56 -07:00
|
|
|
* (set first_item to false)
|
2021-03-16 22:43:46 -07:00
|
|
|
* (and set new.cdr to result)
|
|
|
|
|
* 5. return new ast
|
|
|
|
|
*/
|
2021-03-31 23:01:22 -07:00
|
|
|
let ret: Ast;
|
|
|
|
|
let mret = ret.clone().borrow_mut();
|
2021-05-31 23:06:56 -07:00
|
|
|
let ast_inner = ast.clone().borrow_mut();
|
|
|
|
|
|
|
|
|
|
match process_ctr(
|
|
|
|
|
ast_inner.car,
|
2021-03-31 23:01:22 -07:00
|
|
|
vars.clone(),
|
|
|
|
|
funcs.clone(),
|
|
|
|
|
sym_loose,
|
2021-05-31 23:06:56 -07:00
|
|
|
first_item,
|
|
|
|
|
mret.cdr
|
|
|
|
|
) {
|
|
|
|
|
Ok(ctr) => {
|
|
|
|
|
mret.car = ctr
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
return Err(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-31 23:01:22 -07:00
|
|
|
|
2021-05-31 23:06:56 -07:00
|
|
|
match process_ctr(
|
|
|
|
|
ast_inner.cdr,
|
|
|
|
|
vars.clone(),
|
|
|
|
|
funcs.clone(),
|
|
|
|
|
sym_loose,
|
|
|
|
|
false,
|
|
|
|
|
RefCell::new(Rc::clone(Ctr::None))
|
|
|
|
|
) {
|
|
|
|
|
Ok(ctr) => {
|
|
|
|
|
mret.cdr = ctr
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
return Err(err)
|
|
|
|
|
}
|
2021-03-31 23:01:22 -07:00
|
|
|
}
|
2021-03-16 22:43:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process_ctr(
|
|
|
|
|
ctr: Ctr,
|
|
|
|
|
vars: Rc<RefCell<VTable>>,
|
|
|
|
|
funcs: Rc<RefCell<FTable>>,
|
|
|
|
|
sym_loose: bool,
|
2021-05-25 09:37:23 -07:00
|
|
|
first_item: bool,
|
|
|
|
|
rest: Ast
|
2021-03-16 22:43:46 -07:00
|
|
|
) -> Result<Ctr, String> {
|
2021-03-17 00:03:43 -07:00
|
|
|
/* LOGIC:
|
2021-03-16 22:43:46 -07:00
|
|
|
* 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
|
2021-03-17 00:03:43 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
match ctr {
|
2021-05-31 23:06:56 -07:00
|
|
|
Ctr::Symbol(token) => {
|
2021-03-17 00:03:43 -07:00
|
|
|
let mut tok = token;
|
2021-05-31 23:06:56 -07:00
|
|
|
if let Some(s) = vars.borrow().get(token) {
|
2021-05-25 09:37:23 -07:00
|
|
|
|
|
|
|
|
// is function, or variable alias?
|
2021-05-31 23:06:56 -07:00
|
|
|
if first_item {
|
|
|
|
|
if let Ctr::Symbol(t) = s {
|
|
|
|
|
if let Some(f) = funcs.borrow().get(t) {
|
|
|
|
|
tok = t;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-25 09:37:23 -07:00
|
|
|
|
|
|
|
|
// is a basic value.
|
2021-03-17 00:03:43 -07:00
|
|
|
} else {
|
2021-05-25 09:37:23 -07:00
|
|
|
return Ok((*s).clone())
|
2021-03-17 00:03:43 -07:00
|
|
|
}
|
2021-03-31 23:01:22 -07:00
|
|
|
|
|
|
|
|
// else call function
|
2021-03-17 00:03:43 -07:00
|
|
|
} else if !first_item && !sym_loose {
|
|
|
|
|
return Err("variable not found")
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 23:06:56 -07:00
|
|
|
if first_item {
|
|
|
|
|
if let Some(f) = funcs.borrow().get(tok) {
|
|
|
|
|
match func_call(f, rest, vars.clone(), funcs.clone()) {
|
|
|
|
|
Ok(a) => Ok(Ctr::Seg(a.borrow().clone())),
|
|
|
|
|
Err(s) => Err(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Ctr::Seg(tree) => {
|
|
|
|
|
match eval(tree, vars, funcs, sym_loose) {
|
|
|
|
|
Ok(ast) => {
|
|
|
|
|
Ok(ast.borrow())
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
Err(e)
|
2021-05-25 09:37:23 -07:00
|
|
|
}
|
2021-03-17 00:03:43 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => Ok(ctr.clone())
|
|
|
|
|
}
|
2021-02-14 16:33:17 -08:00
|
|
|
}
|