This patch improves the string_of_* functions in Bitmatch_persistent
authorRichard W.M. Jones <rich@annexia.org>
Tue, 26 Aug 2008 08:21:43 +0000 (08:21 +0000)
committerRichard W.M. Jones <rich@annexia.org>
Tue, 26 Aug 2008 08:21:43 +0000 (08:21 +0000)
so that they can print (many) expressions.  At the moment they can
only print stuff like "bind ([expr])" for most expressions.

bitstring_persistent.ml

index 6177b1d..032b2d8 100644 (file)
@@ -104,44 +104,40 @@ let patt_printer = function
   | <:patt< _ >> -> "_"
   | _ -> "[pattern]"
 
-let expr_printer = function
+let rec expr_printer = function
   | <:expr< $lid:id$ >> -> id
   | <:expr< $int:i$ >> -> i
-  | _ -> "[expression]"
+  | <:expr< $lid:op$ $a$ $b$ >> ->
+    sprintf "%s %s %s" op (expr_printer a) (expr_printer b)
+  | _ -> "[expr]"
 
 let _string_of_field { flen = flen;
                       endian = endian; signed = signed; t = t;
                       _loc = _loc;
                       offset = offset; check = check; bind = bind;
                       save_offset_to = save_offset_to } =
-  let flen =
-    match expr_is_constant flen with
-    | Some i -> string_of_int i
-    | None -> "[non-const-len]" in
+  let flen = expr_printer flen in
   let endian =
     match endian with
     | ConstantEndian endian -> Bitstring.string_of_endian endian
-    | EndianExpr _ -> "endian([expr])" in
+    | EndianExpr expr -> sprintf "endian(%s)" (expr_printer expr) in
   let signed = if signed then "signed" else "unsigned" in
   let t = string_of_field_type t in
 
   let offset =
     match offset with
     | None -> ""
-    | Some expr ->
-       match expr_is_constant expr with
-       | Some i -> sprintf ", offset(%d)" i
-       | None -> sprintf ", offset([expr])" in
+    | Some expr -> sprintf ", offset(%s)" (expr_printer expr) in
 
   let check =
     match check with
     | None -> ""
-    | Some expr -> sprintf ", check([expr])" in
+    | Some expr -> sprintf ", check(%s)" (expr_printer expr) in
 
   let bind =
     match bind with
     | None -> ""
-    | Some expr -> sprintf ", bind([expr])" in
+    | Some expr -> sprintf ", bind(%s)" (expr_printer expr) in
 
   let save_offset_to =
     match save_offset_to with