Skip to content

Commit

Permalink
add subtraction to rust interpreter (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenrothkopf authored May 31, 2023
1 parent b6fcdfb commit b63a15e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crates/frontend/tests/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.)]);
}
1 change: 1 addition & 0 deletions crates/frontend/tests/sub.rose
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def sub(x: R, y: R): R = x - y
8 changes: 7 additions & 1 deletion crates/interp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ pub fn interp(module: &Module, function: Defn, args: Vec<Val>) -> Vec<Val> {
}
}
}
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() {
Expand Down

0 comments on commit b63a15e

Please sign in to comment.