# HG changeset patch # User Adam Chlipala # Date 1322923447 18000 # Node ID 15e0c935c91bb2c06d49bd7b009c3b5bcdc1d09a # Parent 705cb41ac7d0e8912a8dc654664dd0ac8901a270 Catching integer divisions by zero diff -r 705cb41ac7d0 -r 15e0c935c91b lib/js/urweb.js --- a/lib/js/urweb.js Wed Nov 30 15:28:56 2011 -0500 +++ b/lib/js/urweb.js Sat Dec 03 09:44:07 2011 -0500 @@ -19,9 +19,9 @@ function minus(x, y) { return x - y; } function times(x, y) { return x * y; } function div(x, y) { return x / y; } -function divInt(x, y) { var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); } +function divInt(x, y) { if (y == 0) er("Division by zero"); var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); } function mod(x, y) { return x % y; } -function modInt(x, y) { var n = x % y; return n < 0 ? Math.ceil(n) : Math.floor(n); } +function modInt(x, y) { if (y == 0) er("Division by zero"); var n = x % y; return n < 0 ? Math.ceil(n) : Math.floor(n); } function lt(x, y) { return x < y; } function le(x, y) { return x <= y; } diff -r 705cb41ac7d0 -r 15e0c935c91b src/cjr_print.sml --- a/src/cjr_print.sml Wed Nov 30 15:28:56 2011 -0500 +++ b/src/cjr_print.sml Sat Dec 03 09:44:07 2011 -0500 @@ -1655,7 +1655,7 @@ p_exp' true false env e1]) | EBinop (s, e1, e2) => - if Char.isAlpha (String.sub (s, size s - 1)) then + if s <> "fdiv" andalso Char.isAlpha (String.sub (s, size s - 1)) then box [string s, string "(", p_exp' false false env e1, @@ -1663,10 +1663,48 @@ space, p_exp' false false env e2, string ")"] + else if s = "/" orelse s = "%" then + box [string "({", + newline, + string "uw_Basis_int", + space, + string "dividend", + space, + string "=", + space, + p_exp env e1, + string ",", + space, + string "divisor", + space, + string "=", + space, + p_exp env e2, + string ";", + newline, + string "if", + space, + string "(divisor", + space, + string "==", + space, + string "0)", + newline, + box [string "uw_error(ctx, FATAL, \"", + string (ErrorMsg.spanToString loc), + string ": division by zero\");", + newline], + string "dividend", + space, + string s, + space, + string "divisor;", + newline, + string "})"] else parenIf par (box [p_exp' true false env e1, space, - string s, + string (if s = "fdiv" then "/" else s), space, p_exp' true false env e2]) diff -r 705cb41ac7d0 -r 15e0c935c91b src/jscomp.sml --- a/src/jscomp.sml Wed Nov 30 15:28:56 2011 -0500 +++ b/src/jscomp.sml Sat Dec 03 09:44:07 2011 -0500 @@ -706,6 +706,8 @@ | "*" => "times" | "/" => (case bi of Int => "divInt" | NotInt => "div") | "%" => (case bi of Int => "modInt" | NotInt => "mod") + | "fdiv" => "div" + | "fmod" => "mod" | "<" => "lt" | "<=" => "le" | "strcmp" => "strcmp" diff -r 705cb41ac7d0 -r 15e0c935c91b src/monoize.sml --- a/src/monoize.sml Wed Nov 30 15:28:56 2011 -0500 +++ b/src/monoize.sml Sat Dec 03 09:44:07 2011 -0500 @@ -1031,7 +1031,7 @@ floatBin "+", floatBin "-", floatBin "*", - floatBin "/", + floatBin "fdiv", floatBin "fmod") end diff -r 705cb41ac7d0 -r 15e0c935c91b tests/div.ur --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/div.ur Sat Dec 03 09:44:07 2011 -0500 @@ -0,0 +1,43 @@ +functor Make(M : sig + type t + val read_t : read t + val show_t : show t + val num_t : num t + end) = struct + fun calculate (n1, n2) = return + {[readError n1 / readError n2 : M.t]}
+ {[readError n1 % readError n2 : M.t]}
+
+ + fun main () = + s1 <- source ""; + s2 <- source ""; + s3 <- source ""; + s4 <- source ""; + return +

Client-side

+ + / +