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-06-05 17:46:13 -07:00
|
|
|
use crate::segment::{Ast, Ctr, new_ast};
|
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-06-22 00:50:37 -07:00
|
|
|
) -> Result<Ctr, String> {
|
|
|
|
|
let mut car = ast.borrow().clone().car;
|
|
|
|
|
let mut cdr = ast.borrow().clone().cdr;
|
|
|
|
|
let ret = new_ast(Ctr::None, Ctr::None);
|
|
|
|
|
let mut iter = ret.clone();
|
|
|
|
|
|
|
|
|
|
// doing an initial variable check here allows us
|
|
|
|
|
// to find functions passed in as variables
|
|
|
|
|
if let Ctr::Symbol(ref tok) = car {
|
|
|
|
|
if let Some(val) = vars.borrow().get(tok) {
|
|
|
|
|
car = (**val).clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// another check to detect if we may have a function call
|
|
|
|
|
if let Ctr::Symbol(ref tok) = car {
|
2021-11-02 19:18:11 -07:00
|
|
|
match cdr {
|
|
|
|
|
Ctr::Seg(ast) => {
|
|
|
|
|
if let Some(func) = funcs.borrow().get(tok) {
|
|
|
|
|
return func_call(func.clone(), ast.clone(), vars.clone(), funcs.clone())
|
|
|
|
|
} else {
|
|
|
|
|
return Err(format!("Couldnt find function: {}.", tok))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Ctr::None => {
|
|
|
|
|
if let Some(func) = funcs.borrow().get(tok) {
|
|
|
|
|
return func_call(func.clone(), new_ast(Ctr::None, Ctr::None), vars.clone(), funcs.clone())
|
|
|
|
|
} else {
|
|
|
|
|
return Err(format!("Couldnt find function: {}.", tok))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => return Err(format!("Arguments to function not a list!"))
|
2021-06-22 00:50:37 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut none = false;
|
|
|
|
|
while !none {
|
|
|
|
|
match car {
|
|
|
|
|
// if LIST: call eval inner on it with first_item=true
|
|
|
|
|
Ctr::Seg(ref inner) => {
|
|
|
|
|
match eval(inner.clone(), vars.clone(), funcs.clone(), sym_loose) {
|
|
|
|
|
Ok(res) => (*iter).borrow_mut().car = res,
|
|
|
|
|
Err(e) => return Err(format!("Evaluation error: {}", e))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// if SYMBOL: unwrap naively
|
|
|
|
|
Ctr::Symbol(ref tok) => {
|
|
|
|
|
if let Some(val) = vars.borrow().get(&tok.clone()) {
|
|
|
|
|
(*iter).borrow_mut().car = (**val).clone();
|
|
|
|
|
} else if sym_loose {
|
|
|
|
|
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
|
|
|
|
|
} else {
|
|
|
|
|
return Err(format!("Undefined variable: {}", tok))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// if OTHER: clone and set
|
|
|
|
|
_ => {
|
|
|
|
|
(*iter).borrow_mut().car = car.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match cdr {
|
|
|
|
|
// if SYMBOL: unwrap naively, then end
|
|
|
|
|
Ctr::Symbol(ref tok) => {
|
|
|
|
|
if let Some(val) = vars.borrow().get(&tok.clone()) {
|
|
|
|
|
(*iter).borrow_mut().car = (**val).clone();
|
|
|
|
|
} else if sym_loose {
|
|
|
|
|
(*iter).borrow_mut().car = Ctr::Symbol(tok.to_string());
|
|
|
|
|
} else {
|
|
|
|
|
return Err(format!("Undefined variable: {}", tok))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
none = true;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// if LIST:
|
|
|
|
|
// - iter.cdr = new_ast(None, None)
|
|
|
|
|
// - iter = iter.cdr
|
|
|
|
|
// - car = cdr.car
|
|
|
|
|
// - cdr = cdr.cdr
|
|
|
|
|
// - LOOP
|
|
|
|
|
Ctr::Seg(next) => {
|
|
|
|
|
let n = new_ast(Ctr::None, Ctr::None);
|
|
|
|
|
iter.borrow_mut().cdr = Ctr::Seg(n.clone());
|
|
|
|
|
iter = n;
|
|
|
|
|
car = next.borrow().clone().car;
|
|
|
|
|
cdr = next.borrow().clone().cdr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if OTHER: clone and set, and then end
|
|
|
|
|
_ => {
|
|
|
|
|
(*iter).borrow_mut().cdr = cdr.clone();
|
|
|
|
|
none = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Ctr::None = car {
|
|
|
|
|
if let Ctr::None = cdr {
|
|
|
|
|
none = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(Ctr::Seg(ret))
|
2021-03-16 22:43:46 -07:00
|
|
|
}
|