From 1d0b030e3a62a905dce16d05e8816cb3da8c49eb Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 30 Jun 2012 15:06:18 +0100 Subject: [PATCH] Implement inequality operator (use: != or <>) --- lib/whenexpr.ml | 15 +++++++++++++++ lib/whenexpr.mli | 1 + tests/parsing/t020_simple.ml | 10 ++++++++++ tools/whenjobs.pod | 6 ++++++ 4 files changed, 32 insertions(+) diff --git a/lib/whenexpr.ml b/lib/whenexpr.ml index c2ffa33..1301e5e 100644 --- a/lib/whenexpr.ml +++ b/lib/whenexpr.ml @@ -41,6 +41,7 @@ type whenexpr = | Expr_eq of whenexpr * whenexpr | Expr_ge of whenexpr * whenexpr | Expr_gt of whenexpr * whenexpr + | Expr_ne of whenexpr * whenexpr | Expr_not of whenexpr | Expr_add of whenexpr * whenexpr | Expr_sub of whenexpr * whenexpr @@ -212,6 +213,9 @@ and expr_of_iexpr _loc = function | IExpr_app (">", exprs) -> two_params _loc ">" exprs (fun e1 e2 -> Expr_gt (e1, e2)) + | IExpr_app (("!="|"<>"), exprs) -> + two_params _loc "<>" exprs (fun e1 e2 -> Expr_ne (e1, e2)) + | IExpr_app ("!", exprs) -> one_param _loc "!" exprs (fun e1 -> Expr_not e1) @@ -294,6 +298,8 @@ let rec string_of_whenexpr = function sprintf "%s >= %s" (string_of_whenexpr e1) (string_of_whenexpr e2) | Expr_gt (e1, e2) -> sprintf "%s > %s" (string_of_whenexpr e1) (string_of_whenexpr e2) + | Expr_ne (e1, e2) -> + sprintf "%s <> %s" (string_of_whenexpr e1) (string_of_whenexpr e2) | Expr_not e -> sprintf "! %s" (string_of_whenexpr e) | Expr_add (e1, e2) -> sprintf "%s + %s" (string_of_whenexpr e1) (string_of_whenexpr e2) @@ -336,6 +342,7 @@ let rec dependencies_of_whenexpr = function | Expr_eq (e1, e2) | Expr_ge (e1, e2) | Expr_gt (e1, e2) + | Expr_ne (e1, e2) | Expr_add (e1, e2) | Expr_sub (e1, e2) | Expr_mul (e1, e2) @@ -419,6 +426,14 @@ let rec eval_whenexpr variables prev_variables onload = function else T_bool false + | Expr_ne (e1, e2) -> + let e1 = eval_whenexpr variables prev_variables onload e1 + and e2 = eval_whenexpr variables prev_variables onload e2 in + if compare_values e1 e2 <> 0 then + T_bool true + else + T_bool false + | Expr_not e -> if not (eval_whenexpr_as_bool variables prev_variables onload e) then T_bool true diff --git a/lib/whenexpr.mli b/lib/whenexpr.mli index 7310387..c84f091 100644 --- a/lib/whenexpr.mli +++ b/lib/whenexpr.mli @@ -33,6 +33,7 @@ type whenexpr = | Expr_eq of whenexpr * whenexpr (** == *) | Expr_ge of whenexpr * whenexpr (** >= *) | Expr_gt of whenexpr * whenexpr (** > *) + | Expr_ne of whenexpr * whenexpr (** != *) | Expr_not of whenexpr (** boolean not *) | Expr_add of whenexpr * whenexpr (** arithmetic addition or string cat *) | Expr_sub of whenexpr * whenexpr (** arithmetic subtraction *) diff --git a/tests/parsing/t020_simple.ml b/tests/parsing/t020_simple.ml index f7f0778..19ad3f9 100644 --- a/tests/parsing/t020_simple.ml +++ b/tests/parsing/t020_simple.ml @@ -59,3 +59,13 @@ when false == true : << # nothing >> + +when true != true : +<< + # nothing +>> + +when true <> (false || true) : +<< + # nothing +>> diff --git a/tools/whenjobs.pod b/tools/whenjobs.pod index df17e69..77b26bc 100644 --- a/tools/whenjobs.pod +++ b/tools/whenjobs.pod @@ -447,6 +447,12 @@ If the sub-expressions are numeric, then numeric comparison is done. If either sub-expression is non-numeric, then both expressions are converted (if necessary) to strings and string comparison is done. +=item I BE> I + +=item I B I + +Either form can be used to test the two expressions for inequality. + =item B I Boolean negative of I. -- 1.8.3.1