From aad9ce3bda23efbab03595868bda7fc6dc3e03bf Mon Sep 17 00:00:00 2001 From: Alasdair Armstrong Date: Fri, 2 Mar 2018 14:24:00 +0000 Subject: Fix off-by-one error in OCaml for loop compilation Fix for loop in interactive interpreter Fixes #8 --- src/interpreter.ml | 17 +++++++++++++++++ src/ocaml_backend.ml | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/interpreter.ml b/src/interpreter.ml index e39c90f0..55dcbed0 100644 --- a/src/interpreter.ml +++ b/src/interpreter.ml @@ -516,6 +516,23 @@ let rec step (E_aux (e_aux, annot) as orig_exp) = | Right exp' -> wrap (E_try (exp', pexps)) end + | E_for (id, exp_from, exp_to, exp_step, ord, body) when is_value exp_from && is_value exp_to && is_value exp_step -> + let v_from = value_of_exp exp_from in + let v_to = value_of_exp exp_to in + let v_step = value_of_exp exp_step in + begin match value_gt [v_from; v_to] with + | V_bool true -> wrap (E_lit (L_aux (L_unit, Parse_ast.Unknown))) + | V_bool false -> + wrap (E_block [subst id v_from body; E_aux (E_for (id, exp_of_value (value_add_int [v_from; v_step]), exp_to, exp_step, ord, body), annot)]) + | _ -> assert false + end + | E_for (id, exp_from, exp_to, exp_step, ord, body) when is_value exp_to && is_value exp_step -> + step exp_from >>= fun exp_from' -> wrap (E_for (id, exp_from', exp_to, exp_step, ord, body)) + | E_for (id, exp_from, exp_to, exp_step, ord, body) when is_value exp_step -> + step exp_to >>= fun exp_to' -> wrap (E_for (id, exp_from, exp_to', exp_step, ord, body)) + | E_for (id, exp_from, exp_to, exp_step, ord, body) -> + step exp_step >>= fun exp_step' -> wrap (E_for (id, exp_from, exp_to, exp_step', ord, body)) + | E_sizeof _ | E_constraint _ -> assert false (* Must be re-written before interpreting *) | _ -> failwith ("Unimplemented " ^ string_of_exp orig_exp) diff --git a/src/ocaml_backend.ml b/src/ocaml_backend.ml index 22bb7de6..795b6300 100644 --- a/src/ocaml_backend.ml +++ b/src/ocaml_backend.ml @@ -273,8 +273,8 @@ let rec ocaml_exp ctx (E_aux (exp_aux, _) as exp) = in let loop_compare = match ord with - | Ord_aux (Ord_inc, _) -> string "Big_int.less" - | Ord_aux (Ord_dec, _) -> string "Big_int.greater" + | Ord_aux (Ord_inc, _) -> string "Big_int.less_equal" + | Ord_aux (Ord_dec, _) -> string "Big_int.greater_equal" | Ord_aux (Ord_var _, _) -> failwith "Cannot have variable loop order!" in let loop_body = -- cgit v1.2.3