aboutsummaryrefslogtreecommitdiff
path: root/src/expressions/show_expr.ml
blob: a191d2d31f1a13e9e0015a2d361725e124ef7fcb (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(*
This file is part of licht.

licht is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

licht is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with licht.  If not, see <http://www.gnu.org/licenses/>.
*)

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 = 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: t 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