Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when division by 0 #18

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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