diff --git a/src/error.zig b/src/error.zig index 09ed0ba..7aea0ed 100644 --- a/src/error.zig +++ b/src/error.zig @@ -15,6 +15,7 @@ pub const RuntimeError = error{ IncompatibleTypes, InvalidOperation, NullType, + DivisionByZero, }; pub const Error = struct { diff --git a/src/interpreter.zig b/src/interpreter.zig index 11b6955..b7694b6 100644 --- a/src/interpreter.zig +++ b/src/interpreter.zig @@ -89,7 +89,13 @@ pub const Interpreter = struct { .PLUS => obj{ .num = l + right.num }, .MINUS => obj{ .num = l - right.num }, .STAR => obj{ .num = l * right.num }, - .SLASH => obj{ .num = l / right.num }, + .SLASH => slash: { + if (right.num == 0) { + Error.printerr(expr.operator, "Division by zero"); + break :slash RuntimeErrors.DivisionByZero; + } + break :slash obj{ .num = l / right.num }; + }, .GREATER => obj{ .boolean = l > right.num }, .GREATER_EQUAL => obj{ .boolean = l >= right.num }, .LESS => obj{ .boolean = l < right.num },