-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.ml
175 lines (166 loc) · 4.09 KB
/
ast.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module U = Util
module Type = struct
type exp =
| None
| Anon
| Ctor of {ctor:U.sym; params:exp list}
| Var of U.sym
| Fun of exp list
| Product of exp list
type variant = {variant:U.sym; typ:exp}
type field = {field:U.sym; typ:exp}
type body =
| Sum of variant list
| Record of field list
| Exp of exp
type decl = {ctor:U.sym; params:U.sym list; body:body}
(*
let rec dump =
let module P = Printf in
function
| Anon -> P.printf "(\"Anon type\")"
| Constr {exps; constr} ->
P.printf "(\"Type constructor\" (%s " (U.quote constr);
List.iter (fun t -> dump t; print_char ' ') exps;
print_string "))\n";
| Var s -> P.printf "(\"Type variable\" %s)" (U.quote s)
| Fun l ->
print_string "(\"Function type\" ";
List.iter (fun t -> dump t; print_char ' ') l;
print_string ")\n"
| Tuple l ->
print_string "(\"Tuple type\" ";
List.iter (fun t -> dump t; print_char ' ') l;
print_string ")\n"
| Record l ->
let f {field_name; typ} =
P.printf "((Field %s) (Type " (U.quote field_name);
dump typ;
print_string ")) ";
in
print_string "(\"Record type\" ";
List.iter f l;
print_string ")\n";
| Variant {ctor; param} ->
P.printf "((Ctor %s) (" (U.quote sym);
match param with
| Some t -> dump t
| None -> ();
print_string ")\n"
let f = function
| Untyped sym -> P.printf "((Ctor %s) ())" (U.quote sym)
| Typed {sym; typ} ->
P.printf "((Ctor %s) " (U.quote sym);
dump typ;
print_string ")";
in
print_string "(\"Variant Type\" ";
List.iter f l;
print_string ")\n";
*)
end
module Constant = struct
type t =
| Bool of bool
| Char of char
| Error of string
| Float of float
| Int of int
| String of string
| Unit
end
module Pattern = struct
type t =
| Alt of t list
| As of {pattern:t; bound_var:U.sym}
| Cons of t * t
| Constant of Constant.t
| Name of U.sym
| Tuple of t list
| Variant of {variant:U.sym; body:t}
| Typed of {pattern:t; typ:Type.exp}
end
module Exp = struct
type t =
| Apply of {fun_name:t; params:t list}
| Array of t list
| Asr of binary_op
| Assign of binary_op
| Cons of binary_op
| Constant of Constant.t
| Divide of binary_op
| Equals of binary_op
| Fun of fun_def
| Function of matching list
| Greater of binary_op
| Greater_eq of binary_op
| If of {ante:t; cons:t; alt:t}
| Land of binary_op
| Less of binary_op
| Less_eq of binary_op
| Let of {decls:decls; scope:t}
| List of t list
| Lnot of t
| Lor of binary_op
| Lsl of binary_op
| Lsr of binary_op
| Lxor of binary_op
| Match of {exp:t; matches:matching list}
| Minus of binary_op
| Mod of binary_op
| Multiply of binary_op
| Neg of t
| Not of t
| Not_eq of binary_op
| Or of binary_op
| Plus of binary_op
| Record of field list
| Sequence of t list
| Tuple of t list
| Type_constr of {constr:U.sym; exp:t}
| Typed of {exp:t; typ:Type.exp}
| Unit
| Var of U.sym
and decls =
| Non_rec of decl list
| Rec of decl list
and binary_op = {lhs:t; rhs:t}
and field = {field:U.sym; exp:t}
and fun_def = {params:Pattern.t list; body:t; result_type:Type.exp}
and matching = {pattern:Pattern.t; result:t}
and decl =
| Value_decl of {pattern:Pattern.t; value:t}
| Function_decl of {name:U.sym; def:fun_def}
end
type top =
| Exp of Exp.decls
| Type of Type.decl list
type prog = top list
(*
let dump_e e =
let f = function
| Exp.Function_def (sym, _) -> Printf.printf "(Fun %s)\n" (U.quote sym)
| Exp.Value_def _ -> print_string "(Value)\n"
in
let l = match e with
| Exp.Let_binding l -> print_string "(\"Let binding\" "; l
| Exp.Rec_binding l -> print_string "(\"Letrec binding\" "; l
in
List.iter f l;
print_string ")\n"
let dump_t t =
let f Type_exp.{ctor=U.{n}; params; body} =
Printf.printf "(\"%s\" (" n;
List.iter (fun U.{n} -> Printf.printf "\"%s\" " n) params;
print_string ")";
Type_exp.dump body;
print_string ")\n";
in
print_string "(\"Type binding\" ";
List.iter f t;
print_string ")\n"
let dump prog =
print_endline "(";
List.iter (function Exp e -> dump_e e | Type t -> dump_t t) prog;
print_endline ")";
*)