aboutsummaryrefslogtreecommitdiff
path: root/src/expressions/show_expr.ml
blob: 3a54929e2e978177dcc8161cf03f837f79fd4c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let u = UTF8.from_utf8string

module Show_Expr
  (R:Sym_ref.SYM_REF with type 'a obs = (UTF8.Buffer.buffer -> unit))
  (T:Sym_type.SYM_TYPE with type 'a obs = (UTF8.Buffer.buffer -> unit)) = struct

  module T = T
  module R = R

  type t = unit
  type repr = UTF8.Buffer.buffer -> unit
  type obs = UTF8.Buffer.buffer -> unit

  let observe buffer value = buffer value

  let value v () buffer = T.observe v buffer

  let ref r () buffer = R.observe r buffer

  let call0 ident () buffer =
    let utf8ident = UTF8.to_utf8string ident in
    UTF8.Printf.bprintf buffer "%s()" utf8ident

  let call1 ident p1 () buffer =
    let utf8ident = UTF8.to_utf8string ident in
    UTF8.Printf.bprintf buffer "%s(%a)"
      utf8ident
      (fun x b -> observe b x) p1

  let call2 ident p1 p2 () buffer =
    let utf8ident = UTF8.to_utf8string ident in
    begin match utf8ident with
    | "+" | "*" | "-" | "/" | "^" | "="
    | "<>" | "<=" | ">=" | "<" | ">" ->
        UTF8.Printf.bprintf buffer "%a%s%a"
        (fun x b -> observe b x) p1
          utf8ident
        (fun x b -> observe b x) p2
    | _ ->
      UTF8.Printf.bprintf buffer "%s(%a;%a)"
        utf8ident
        (fun x b -> observe b x) p1
        (fun x b -> observe b x) p2
    end

  let call3 ident p1 p2 p3 () buffer =
    let utf8ident = UTF8.to_utf8string ident in
    UTF8.Printf.bprintf buffer "%s(%a;%a;%a)"
      utf8ident
      (fun x b -> observe b x) p1
      (fun x b -> observe b x) p2
      (fun x b -> observe b x) p3

  let callN ident (params: repr list) () buffer =
    UTF8.Buffer.add_string buffer ident;
    Tools.List.printb ~sep:(u";") (fun buffer value -> value buffer) buffer params

  let expression e () buffer =
    UTF8.Printf.bprintf buffer "(%a)"
    (fun x b -> b x) e

end