bool_op.ml 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  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. | Binop (Eq, (Type (_, Bool) as left), (Type (_, Bool) as right), loc) ->
  23. let left = Type (TypeCast (Int, left, noloc), Int) in
  24. let right = Type (TypeCast (Int, right, noloc), Int) in
  25. Binop (Eq, left, right, loc)
  26. | node -> transform_children bool_op node
  27. let rec phase input =
  28. prerr_endline "- Convert bool operations";
  29. match input with
  30. | Ast node -> Ast (bool_op node)
  31. | _ -> raise (InvalidInput "bool operations")