-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmlpp.ml
52 lines (47 loc) · 2.04 KB
/
mmlpp.ml
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
open Format
open Mml
let rec print_fields ppf = function
| [] -> fprintf ppf ""
| (x, t, m) :: l -> let mut = if m then "mutable " else "" in
fprintf ppf "%s %s: %s;@ %a" mut x (typ_to_string t) print_fields l
let rec print_types ppf = function
| [] -> fprintf ppf "@."
| (t, s) :: l -> fprintf ppf "type %s = { @[%a}@]@.%a" t print_fields s print_types l
let uop_to_string = function
| Neg -> "-"
| Not -> "not "
let bop_to_string = function
| Add -> "+"
| Sub -> "-"
| Mul -> "*"
| Div -> "/"
| Mod -> "mod"
| Eq -> "=="
| Neq -> "!="
| Lt -> "<"
| Le -> "<="
| And -> "&&"
| Or -> "||"
let rec print_expr ppf = function
| Int n -> fprintf ppf "%i" n
| Bool b -> fprintf ppf "%b" b
| Unit -> fprintf ppf "()"
| Uop(op, e) -> fprintf ppf "(%s %a)" (uop_to_string op) print_expr e
| Bop(op, e1, e2) -> fprintf ppf "(@[%a %s %a)@]" print_expr e1 (bop_to_string op) print_expr e2
| Var x -> fprintf ppf "%s" x
| Let(x, e1, e2) -> fprintf ppf "@[(let %s =@ %a in@ %a)@]" x print_expr e1 print_expr e2
| If(c, e1, e2) -> fprintf ppf "@[if %a then@ %a else@ %a@]" print_expr c print_expr e1 print_expr e2
| Fun(x, t, e) -> fprintf ppf "fun (%s: %s) -> %a" x (typ_to_string t) print_expr e
| App(e1, e2) -> fprintf ppf "(%a %a)" print_expr e1 print_expr e2
| Fix(x, t, e) -> fprintf ppf "fix (%s: %s) = %a" x (typ_to_string t) print_expr e
| Strct l -> fprintf ppf "{ @[%a}@]" print_defs l
| GetF(e, x) -> fprintf ppf "(%a).%s" print_expr e x
| SetF(e1, x, e2) -> fprintf ppf "(%a).%s <- %a" print_expr e1 x print_expr e2
| Seq(e1, e2) -> fprintf ppf "%a; %a" print_expr e1 print_expr e2
| For(x, e1, e2, e3) -> fprintf ppf "for %s = %a to %a do %a done" x print_expr e1 print_expr e2 print_expr e3
| While(e1, e2) -> fprintf ppf "while %a do %a done" print_expr e1 print_expr e2
and print_defs ppf = function
| [] -> fprintf ppf ""
| (x, e) :: l -> fprintf ppf "%s = %a; %a" x print_expr e print_defs l
let print_prog ppf prog =
fprintf ppf "%a@.%a@." print_types prog.types print_expr prog.code