From b63a15eead7501bc365a2b7151d72c3b97bfa8d2 Mon Sep 17 00:00:00 2001 From: Raven Rothkopf <97990788+ravenrothkopf@users.noreply.github.com> Date: Wed, 31 May 2023 11:39:56 -0400 Subject: [PATCH] add subtraction to rust interpreter (#7) --- crates/frontend/tests/interp.rs | 8 ++++++++ crates/frontend/tests/sub.rose | 1 + crates/interp/src/lib.rs | 8 +++++++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 crates/frontend/tests/sub.rose diff --git a/crates/frontend/tests/interp.rs b/crates/frontend/tests/interp.rs index c648fde..1767edc 100644 --- a/crates/frontend/tests/interp.rs +++ b/crates/frontend/tests/interp.rs @@ -9,3 +9,11 @@ fn test_add() { let answer = interp(&module, Defn(0), vec![Val::F64(2.), Val::F64(2.)]); assert_eq!(answer, vec![Val::F64(4.)]); } + +#[test] +fn test_sub() { + let src = include_str!("sub.rose"); + let module = parse(src).unwrap(); + let answer = interp(&module, Defn(0), vec![Val::F64(2.), Val::F64(2.)]); + assert_eq!(answer, vec![Val::F64(0.)]); +} diff --git a/crates/frontend/tests/sub.rose b/crates/frontend/tests/sub.rose new file mode 100644 index 0000000..47b363d --- /dev/null +++ b/crates/frontend/tests/sub.rose @@ -0,0 +1 @@ +def sub(x: R, y: R): R = x - y \ No newline at end of file diff --git a/crates/interp/src/lib.rs b/crates/interp/src/lib.rs index 3322cb1..f3432ca 100644 --- a/crates/interp/src/lib.rs +++ b/crates/interp/src/lib.rs @@ -68,7 +68,13 @@ pub fn interp(module: &Module, function: Defn, args: Vec) -> Vec { } } } - Binop::SubReal => todo!(), + Binop::SubReal => { + if let Val::F64(b) = stack.pop().unwrap() { + if let Val::F64(a) = stack.pop().unwrap() { + stack.push(Val::F64(a - b)); + } + } + } Binop::MulReal => { if let Val::F64(b) = stack.pop().unwrap() { if let Val::F64(a) = stack.pop().unwrap() {