-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.ml
93 lines (85 loc) · 2.91 KB
/
syntax.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
type e =
| Int of int
| Float of float
| Bool of bool
| Str of string
| Var of string
| Pre of string * e
| Bin of e * string * e
| If of e * e * e
| Fun of string list * e
| App of e * e list
| Let of string * e * e
| LetRec of string * e * e
| Raise of string
| Rec of (string * e) list
| Unit
| Get of e * e
| Put of e * e * e
| Array of e * e
| CApp of string * e
| Tuple of e list
| Match of e * (e * e option * e) list
| Open of string * e
let counter = ref 0
let genid s =
incr counter;
Printf.sprintf "T%s%d" s !counter
let gentmp () =
incr counter;
Printf.sprintf "T%d" !counter
let read_all file =
let inchan = open_in file in
let lines = ref [] in
let rc =
begin try
while true; do
lines := input_line inchan :: !lines
done;
""
with
| e -> String.concat "\n" (List.rev !lines)
end
in
close_in inchan;
rc
let rec show_e f = function
| Int i -> Format.fprintf f "Int(%d)@?" i
| Var i -> Format.fprintf f "Var(\"%s\")@?" i
| Str i -> Format.fprintf f "Str(\"%s\")@?" i
| Fun(ss,e) -> Format.fprintf f "Fun([%s],%a)@?" (String.concat ";" ss) show_e e
| App(e,es) -> Format.fprintf f "App(%a,[%a])@?" show_e e show_es es
| Rec(ses) -> Format.fprintf f "Rec([%a])@?" show_ses ses
| Float i -> Format.fprintf f "Float(%f)@?" i
| Bool i -> Format.fprintf f "Bool(%b)@?" i
| Bin(e1,op,e2) -> Format.fprintf f "Bin(%a,%s,%a)@?" show_e e1 op show_e e2
| If(e1,e2,e3) -> Format.fprintf f "If(%a,%a,%a)@?" show_e e1 show_e e2 show_e e3
| Let(s,e1,e2) -> Format.fprintf f "Let(\"%s\",%a,%a)@?" s show_e e1 show_e e2
| LetRec(s,e1,e2) -> Format.fprintf f "LetRec(\"%s\",%a,%a)@?" s show_e e1 show_e e2
| Pre(op,e2) -> Format.fprintf f "Pre(%s,%a)@?" op show_e e2
| Raise s -> Format.fprintf f "Raise(%s)@?" s
| Unit -> Format.fprintf f "Unit@?"
| Tuple es -> Format.fprintf f "Tuple[%a]@?" show_es es
| Array(e1,e2) -> Format.fprintf f "Array(%a,%a)" show_e e1 show_e e2
| Get(e1,e2) ->
Format.fprintf f "Get(%a,%a)@?" show_e e1 show_e e2
| Put(e1,e2, e3) ->
Format.fprintf f "Put(%a,%a,%a)@?" show_e e1 show_e e2 show_e e3
| Match(e1,eees) ->
Format.fprintf f "Match(%a,[%a])@?" show_e e1 show_eoees eees
| CApp(s,e) -> Format.fprintf f "CApp(%s,%a)@?" s show_e e
and show_es f = function
| [] -> ()
| [x] -> Format.fprintf f "%a" show_e x
| x::xs -> Format.fprintf f "%a;%a" show_e x show_es xs
and show_ses f = function
| [] -> ()
| [(s,x)] -> Format.fprintf f "(\"%s\",%a)" s show_e x
| (s,x)::xs -> Format.fprintf f "(\"%s\",%a);%a" s show_e x show_ses xs
and show_eoees f = function
| [] -> ()
| [x] -> Format.fprintf f "%a" show_eoee x
| x::xs -> Format.fprintf f "%a;%a" show_eoee x show_eoees xs
and show_eoee f = function
| (e1,Some e2,e3) -> Format.fprintf f "(%a,Some %a,%a)" show_e e1 show_e e2 show_e e3
| (e1,None, e3) -> Format.fprintf f "(%a,None,%a)" show_e e1 show_e e3