Skip to content

Commit aa3d0bc

Browse files
committed
Handle division by 0
1 parent c40f8cb commit aa3d0bc

2 files changed

Lines changed: 5 additions & 11 deletions

File tree

src/init/main.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,8 @@ pub fn run_code(code: String) {
4848
pub fn main() -> Result<(), &'static str> {
4949
run_code(
5050
r#"
51-
fn prompt {
52-
print("> ");
53-
let s = get_string();
54-
print("You typed: ");
55-
print(s);
56-
println("");
57-
}
58-
5951
fn main {
60-
prompt();
61-
main();
62-
return Null;
52+
return 123 / 0;
6353
}
6454
"#
6555
.into(),

src/lang/vm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ impl<'a> VM<'a> {
8484
AstBinopOp::DIV => {
8585
if let Value::Number(left_num) = left {
8686
if let Value::Number(right_num) = right {
87+
if right_num == 0.0 {
88+
self.error = "division by 0".into();
89+
return Value::Null();
90+
}
8791
return Value::Number(left_num / right_num);
8892
}
8993
}

0 commit comments

Comments
 (0)