Skip to content

Commit

Permalink
Merge pull request #11 from cratelyn/assignments-deep-copy
Browse files Browse the repository at this point in the history
🟰 symbols assignments can be used
  • Loading branch information
cratelyn authored Nov 23, 2023
2 parents 06e2f66 + 8a5ace7 commit 64b81ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/j.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fn eval_(ast:B<N>,st:&mut ST)->R<O<A>>{use{M::*,D::*};
Left=>l.d_left(r), Right=>l.d_right(r)}}
N::Ym{ym,d,o}=>{rec(o).and_then(|a|ym.apply(d,a))}
N::Yd{yd,d,l,r}=>{let(l,r)=(rec(l)?,rec(r)?);yd.apply(d,l,r)}
N::S{sy} =>{st.get(&sy).ok_or(err!("undefined symbol: {sy:?}"))?;todo!("symbol value clone")}
N::S{sy} =>{st.get(&sy).ok_or(err!("undefined symbol: {sy:?}")).and_then(A::deep_copy)}
N::E{sy,e} =>{let(a)=rec(e)?;st.insert(sy,a);r!(Ok(None))}
}.map(O::Some)}
14 changes: 10 additions & 4 deletions tests/t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@
let(a)=eval_s("1 2 3 4 [ i. 4 1")?;eq!(a.as_slice()?,&[1,2,3,4]);ok!()}
} #[cfg(test)]mod symbol_assignment{use super::*;
#[test]fn assign_and_get_i()->R<()>{let(mut st)=ST::default();let(a)=eval("a =: 3",&mut st)?;
assert_eq!(st.get_s("a").unwrap().as_i().unwrap(),3);ok!()}
eq!(st.get_s("a").unwrap().as_i().unwrap(),3);ok!()}
#[test]fn assign_and_get_slice()->R<()>{let(mut st)=ST::default();let(a)=eval("a =: 3 2 1",&mut st)?;
assert_eq!(st.get_s("a").unwrap().as_slice().unwrap(),&[3,2,1]);ok!()}
eq!(st.get_s("a").unwrap().as_slice().unwrap(),&[3,2,1]);ok!()}
#[test]fn assign_and_get_expr()->R<()>{let(mut st)=ST::default();let(a)=eval("a =: 1 3 + 2 4",&mut st)?;
assert_eq!(st.get_s("a").unwrap().as_slice().unwrap(),&[3,7]);ok!()}
// TODO: tests exercising use of variables
eq!(st.get_s("a").unwrap().as_slice().unwrap(),&[3,7]);ok!()}
#[test]fn assign_and_eval_slice()->R<()>{let(mut st)=ST::default();let(eval_)=|s|eval(s,&mut st);
let(mut i)=["a =: 3 2 1","a"].into_iter().map(eval_); is!(i.next().unwrap()?.is_none());
eq!(i.next().unwrap()?.unwrap().as_slice()?,&[3,2,1]); ok!()}
#[test]fn assign_and_eval_expr()->R<()>{let(mut st)=ST::default();let(eval_)=|s|eval(s,&mut st);
let(mut i)=["a =: >: i. 5","i =: 3","i ] \\ a"].into_iter().map(eval_);
is!(i.next().unwrap()?.is_none()); is!(i.next().unwrap()?.is_none());
eq!(i.next().unwrap()?.unwrap().into_matrix()?,&[&[1,2,3],&[2,3,4],&[3,4,5]]); ok!()}
} #[cfg(test)]mod misc{use super::*;
#[test]fn empty_statement_evaluates_to_none()->R<()>{is!(eval("",&mut ST::default())?.is_none());ok!()}
#[test]fn slice_times_transposed_idot_2_3()->R<()>{
Expand Down

0 comments on commit 64b81ed

Please sign in to comment.