From 5c7aa66dbdb32b4fc11d0f72a4cc028c7bfb1b55 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 23 Feb 2012 14:56:05 +0000 Subject: [PATCH] Add 'len ' operator. --- lib/whenexpr.ml | 13 ++++++++++++- lib/whenexpr.mli | 1 + tools/whenjobs.pod | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/whenexpr.ml b/lib/whenexpr.ml index f24e9ed..8ccde2c 100644 --- a/lib/whenexpr.ml +++ b/lib/whenexpr.ml @@ -47,6 +47,7 @@ type whenexpr = | Expr_mul of whenexpr * whenexpr | Expr_div of whenexpr * whenexpr | Expr_mod of whenexpr * whenexpr + | Expr_len of whenexpr | Expr_changes of string | Expr_increases of string | Expr_decreases of string @@ -202,6 +203,9 @@ and expr_of_iexpr _loc = function | IExpr_app ("mod", exprs) -> two_params _loc "+" exprs (fun e1 e2 -> Expr_mod (e1, e2)) + | IExpr_app (("len"|"length"|"size"), exprs) -> + one_param _loc "len" exprs (fun e1 -> Expr_len e1) + | IExpr_app (("change"|"changes"|"changed"), [IExpr_var v]) -> Expr_changes v @@ -274,6 +278,7 @@ let rec string_of_whenexpr = function sprintf "%s / %s" (string_of_whenexpr e1) (string_of_whenexpr e2) | Expr_mod (e1, e2) -> sprintf "%s mod %s" (string_of_whenexpr e1) (string_of_whenexpr e2) + | Expr_len e -> sprintf "len %s" (string_of_whenexpr e) | Expr_changes v -> sprintf "changes %s" v | Expr_increases v -> sprintf "increases %s" v | Expr_decreases v -> sprintf "decreases %s" v @@ -310,7 +315,8 @@ let rec dependencies_of_whenexpr = function | Expr_div (e1, e2) | Expr_mod (e1, e2) -> dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2 - | Expr_not e -> + | Expr_not e + | Expr_len e -> dependencies_of_whenexpr e | Expr_changes v | Expr_increases v @@ -417,6 +423,11 @@ let rec eval_whenexpr variables prev_variables onload = function and e2 = eval_whenexpr variables prev_variables onload e2 in mod_values e1 e2 + | Expr_len e -> + let e = eval_whenexpr variables prev_variables onload e in + let e = string_of_variable e in + T_int (big_int_of_int (String.length e)) + | Expr_changes v -> let prev_value, curr_value = get_prev_curr_value variables prev_variables v in if compare_values prev_value curr_value <> 0 then diff --git a/lib/whenexpr.mli b/lib/whenexpr.mli index c0c6fb2..12ba584 100644 --- a/lib/whenexpr.mli +++ b/lib/whenexpr.mli @@ -39,6 +39,7 @@ type whenexpr = | Expr_mul of whenexpr * whenexpr (** arithmetic multiplication *) | Expr_div of whenexpr * whenexpr (** arithmetic division *) | Expr_mod of whenexpr * whenexpr (** arithmetic modulo *) + | Expr_len of whenexpr (** length *) | Expr_changes of string (** changes var *) | Expr_increases of string (** increases var *) | Expr_decreases of string (** decreases var *) diff --git a/tools/whenjobs.pod b/tools/whenjobs.pod index 5a9ac72..ad60c0d 100644 --- a/tools/whenjobs.pod +++ b/tools/whenjobs.pod @@ -357,6 +357,10 @@ result is subtraction, multiplication, division or modulo. Other types give an error. Note that I really is an infix operator. +=item B I + +If I is a string, this returns the length of the string. + =item I The value of the named variable. -- 1.8.3.1