bool_op.ml 1.0 KB

1234567891011121314151617181920212223242526272829
  1. (**
  2. * The phase applies the transformations shown below. These transformations are
  3. * required because the CiviC VM does not offer instructions that perform these
  4. * operations. Another reason is that conjunction and disjunction require
  5. * short-circuit evaluation. In the examples below, `b1` and `b2` are
  6. * expressions of type boolean, `i` is of type int and `f` is of type float.
  7. *
  8. * > b1 == b2 ==> (int)b1 == (int)b2
  9. * > b1 != b2 ==> (int)b1 != (int)b2
  10. * > b1 && b2 ==> b1 ? b2 : false
  11. * > b1 || b2 ==> b1 ? true : b2
  12. * > b1 + b2 ==> (bool)((int)b1 + (int)b2)
  13. * > b1 * b2 ==> (bool)((int)b1 * (int)b2)
  14. * > (bool)i ==> i != 0
  15. * > (bool)f ==> f != 0.0
  16. * > (int)b1 ==> b1 ? 1 : 0
  17. * > (float)b1 ==> b1 ? 1.0 : 0.0
  18. *)
  19. open Ast
  20. open Util
  21. let rec bool_op = function
  22. | node -> transform_children bool_op node
  23. let rec phase input =
  24. prerr_endline "- Convert bool operations";
  25. match input with
  26. | Ast (node, args) -> Ast (bool_op node, args)
  27. | _ -> raise (InvalidInput "bool operations")