Skip to content

Commit

Permalink
Merge pull request #18 from CS4215-OOGA/division_by_zero_error
Browse files Browse the repository at this point in the history
Throw error when division by 0
  • Loading branch information
JothamWong authored Apr 16, 2024
2 parents 6bbd3bd + b701a6d commit 2492269
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1970,3 +1970,12 @@ for i := 0; i < len(arr); i++ {
}
0;
`, 0, '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n', defaultNumWords);

// Test for division by zero
testProgram(`
func foo() int {
return 0;
}
5 / foo();
`, 'Division by 0 error!', '', defaultNumWords);
5 changes: 4 additions & 1 deletion src/vm/oogavm-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import {
Unassigned,
Undefined,
} from './oogavm-heap.js';
import { OogaError } from './oogavm-errors.js';
import { OogaError, RuntimeError } from './oogavm-errors.js';
import { unparse } from '../utils/utils.js';
import { appendHeap, appendStack } from '../server/debug.js';

Expand Down Expand Up @@ -353,6 +353,9 @@ function apply_binop(sym: string, left: any, right: any) {
case '*':
return left * right;
case '/':
if (right === 0) {
throw new RuntimeError("Division by 0 error!");
}
return left / right;
default:
// FIXME: Propagate error properly to the VM
Expand Down

0 comments on commit 2492269

Please sign in to comment.