finish eval referencing issues, update func tests

This commit is contained in:
Aidan 2021-06-05 17:46:13 -07:00
parent 4712d5466c
commit a6abc993a5
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 17 additions and 19 deletions

View file

@ -17,7 +17,7 @@
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use crate::segment::{Ast, Ctr}; use crate::segment::{Ast, Ctr, new_ast};
use crate::func::{FTable, func_call}; use crate::func::{FTable, func_call};
use crate::vars::VTable; use crate::vars::VTable;
@ -50,21 +50,19 @@ fn eval_inner(
* (and set new.cdr to result) * (and set new.cdr to result)
* 5. return new ast * 5. return new ast
*/ */
let ret: Ast; let ret = new_ast(Ctr::None, Ctr::None);
let mret = ret.clone().borrow_mut();
let ast_inner = ast.clone().borrow_mut();
match process_ctr( match process_ctr(
ast_inner.car, ast.borrow().clone().car,
vars.clone(), vars.clone(),
funcs.clone(), funcs.clone(),
sym_loose, sym_loose,
first_item, first_item,
true, true,
mret.cdr ret.borrow().clone().cdr
) { ) {
Ok(ctr) => { Ok(ctr) => {
mret.car = ctr; ret.borrow_mut().car = ctr;
}, },
Err(err) => { Err(err) => {
return Err(err) return Err(err)
@ -72,7 +70,7 @@ fn eval_inner(
} }
match process_ctr( match process_ctr(
ast_inner.cdr, ast.borrow().clone().cdr,
vars.clone(), vars.clone(),
funcs.clone(), funcs.clone(),
sym_loose, sym_loose,
@ -81,7 +79,7 @@ fn eval_inner(
Ctr::None Ctr::None
) { ) {
Ok(ctr) => { Ok(ctr) => {
mret.cdr = ctr; ret.borrow_mut().cdr = ctr;
}, },
Err(err) => { Err(err) => {
return Err(err) return Err(err)
@ -108,23 +106,23 @@ fn process_ctr(
* 4. finally, return a clone (shallow copy) of datum * 4. finally, return a clone (shallow copy) of datum
*/ */
match ctr { match ctr.clone() {
Ctr::Symbol(token) => { Ctr::Symbol(token) => {
let mut tok = token; let mut tok = token;
if let Some(s) = vars.borrow().get(&tok) { if let Some(s) = vars.borrow().get(&tok) {
// is function, or variable alias? // is function, or variable alias?
if first_item { if first_item {
if let Ctr::Symbol(t) = *s.clone() { if let Ctr::Symbol(t) = &*(s.clone()) {
if let Some(f) = funcs.borrow().get(&t) { if let Some(_f) = funcs.borrow().get(t) {
// leave this set for the function call case below // leave this set for the function call case below
tok = t; tok = t.clone();
} }
} }
// is a basic value. // is a basic value.
} else { } else {
return Ok(*s.clone()) return Ok((*s.clone()).clone())
} }
// variable not found case // variable not found case
@ -136,7 +134,7 @@ fn process_ctr(
if first_item { if first_item {
if let Some(f) = funcs.borrow().get(&tok) { if let Some(f) = funcs.borrow().get(&tok) {
if let Ctr::Seg(args) = rest { if let Ctr::Seg(args) = rest {
match func_call(*f, args, vars.clone(), funcs.clone()) { match func_call(f.clone(), args, vars.clone(), funcs.clone()) {
Ok(a) => return Ok(a.clone()), Ok(a) => return Ok(a.clone()),
Err(s) => return Err(s) Err(s) => return Err(s)
} }

View file

@ -152,7 +152,7 @@ pub fn func_call(
Operation::Internal(f) => Ok((f)(n_args, vars, funcs)), Operation::Internal(f) => Ok((f)(n_args, vars, funcs)),
Operation::External(f) => { Operation::External(f) => {
for n in 0..f.arg_syms.len() { for n in 0..f.arg_syms.len() {
vars.borrow().insert( vars.borrow_mut().insert(
f.arg_syms[n].clone(), f.arg_syms[n].clone(),
Rc::new(list_idx(n_args.clone(), n as u128)) Rc::new(list_idx(n_args.clone(), n as u128))
); );
@ -160,11 +160,11 @@ pub fn func_call(
let result = eval(f.ast.clone(), vars.clone(), funcs, called_func.loose_syms); let result = eval(f.ast.clone(), vars.clone(), funcs, called_func.loose_syms);
for n in 0..f.arg_syms.len() { for n in 0..f.arg_syms.len() {
vars.borrow().remove(&f.arg_syms[n].clone()); vars.borrow_mut().remove(&f.arg_syms[n].clone());
} }
match result { match result {
Ok(r) => Ok(r.borrow().car), Ok(r) => Ok(r.borrow().clone().car),
Err(e) => Err(e) Err(e) => Err(e)
} }
} }

View file

@ -13,7 +13,7 @@ mod func_tests {
eval_lazy: true, eval_lazy: true,
args: Args::Strict(vec![Type::Bool]), args: Args::Strict(vec![Type::Bool]),
function: Operation::Internal( function: Operation::Internal(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ast { |a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow(); let inner = a.borrow();
let mut is_bool = false; let mut is_bool = false;
if let Ctr::Bool(_) = &inner.car { if let Ctr::Bool(_) = &inner.car {