forked from MLstate/opalang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bslTypesGeneration.ml
273 lines (210 loc) · 7.43 KB
/
bslTypesGeneration.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA 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 Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)
(* wait for refactoring *)
module TypeVar = QmlTypeVars
(* dependencies *)
module String = Base.String
module Format = Base.Format
(* shorthand *)
module B = BslTypes
module Q = QmlAst
(* type alias for clarty *)
type 'a pprinter = 'a Format.pprinter
(* === *)
(* Error within a context of type [t] *)
let (!!) t fmt =
BslTypes.pp_citation OManager.oformatter.contents t ;
OManager.printf "The type is : %a@\n" B.pp t ;
OManager.error fmt
type ('env, 'a, 'ast) generator = 'env -> 'a -> 'env * 'ast
module Opa =
struct
let pp_scope ~scope fmt =
let rec aux parfun fmt t =
match t with
| B.Const _
| B.TypeVar _
| B.Void _
| B.Bool _ ->
BslTypes.pp_scope ~scope fmt t
| B.Option (_, t) ->
Format.fprintf fmt "option(%a)" (aux false) t
| B.OpaValue (_, t) ->
aux parfun fmt t
| B.Fun (_, args, ret) ->
if parfun then Format.fprintf fmt "(%a)" (aux false) t else
let paren_out = true in
Format.fprintf fmt "%a -> %a" (Format.pp_list ",@ " (aux true)) args (aux paren_out) ret
(* all external and record types from bsl should be named *)
| B.External (_, n, vs) ->
LangPrint.pp_parameters (aux true) n fmt vs
in aux false fmt
let pp fmt =
let scope = BslTypes.TypeVarPrint.new_scope () in
pp_scope ~scope fmt
let pp_definition fmt ty =
let scope = BslTypes.TypeVarPrint.new_scope () in
match ty with
| B.External (_, name, params) ->
Format.fprintf fmt "type %a = external" (LangPrint.pp_parameters (pp_scope ~scope) name) params
| t -> !! t "Unexpected type for an external type definition@\n"
end
(*
Note for hackers:
For coherence of line errors, the type must fit on one line
*)
module Ocaml =
struct
let nopos = FilePos.nopos "BslTypesGeneration.Ocaml"
(* with zero arity fun, in ocaml we need to add a unit argument *)
let unit_fun args =
match args with
| [] ->
[B.Void nopos]
| _ ->
args
let ty_void = "unit"
let ty_bool = "bool"
let ty_option = "option"
(* Support for utilizing values via the server lib *)
let serverlib = "ServerLib"
let serverlib fmt s = Format.fprintf fmt "%s.%s" serverlib s
(* QmlAst.const_ty *)
let opa_char = "ty_char"
let opa_float = "ty_float"
let opa_int = "ty_int"
let opa_null = "ty_null"
let opa_string = "ty_string"
(* Rest of bsl types *)
let opa_void = "ty_void"
let opa_bool = "ty_bool"
let opa_option = "ty_option"
(*
This is more likely a transformation, which can be done in AST(TODO).
This defines the semantic of register type, since it is used to generated
the mli of the bsl.
BslTypesMap is structural, it is exact, and not hacky.
*)
let pp fmt =
let scope = BslTypes.TypeVarPrint.new_scope () in
let typevar fmt = BslTypes.TypeVarPrint.pp scope fmt in
(* fresh type variable (string) for any same abstract type *)
(*
let map = ref BslTypesMap.empty in
let index = ref (-1) in
let memo fmt t =
let v =
match BslTypesMap.find_opt t !map with
| Some v -> v
| None ->
incr(index);
let v = Printf.sprintf "'opa_v%d" !index in
map := BslTypesMap.add t v !map ;
v
in
Format.pp_print_string fmt v
in
*)
let rec opavalue parfun fmt t =
match t with
| B.Const (_, c) -> (
match c with
| Q.TyFloat -> serverlib fmt opa_float
| Q.TyInt -> serverlib fmt opa_int
| Q.TyNull -> serverlib fmt opa_null
| Q.TyString -> serverlib fmt opa_string
)
| B.TypeVar (_, v) -> typevar fmt v
| B.Void _ -> serverlib fmt opa_void
| B.Bool _ ->
serverlib fmt opa_bool
| B.Option (_, t) ->
Format.fprintf fmt "%a %a" (opavalue true) t serverlib opa_option
| B.OpaValue _ ->
!! t "Imbrications of opa[] constructors does not make any sense@\n"
| B.Fun (_, args, ret) ->
if parfun then Format.fprintf fmt "(%a)" (opavalue false) t else
let paren_out = true in
let args = unit_fun args in
Format.fprintf fmt "%a -> %a" (Format.pp_list " -> " (opavalue true)) args (opavalue paren_out) ret
(* External and Record types should be column only *)
| B.External (_, n, vs) -> (
OcamlPrint.pp_parameters (opavalue true) n fmt vs
)
in
let rec aux parfun fmt t =
match t with
| B.Const (_, c) ->
Format.pp_print_string fmt (QmlAst.Const.ocamlbsl_string_of_ty c)
| B.TypeVar (_, v) -> typevar fmt v
| B.Void _ ->
Format.pp_print_string fmt ty_void
| B.Bool _ ->
Format.pp_print_string fmt ty_bool
| B.Option (_, t) ->
Format.fprintf fmt "%a %s" (aux true) t ty_option
| B.OpaValue (_, t) -> opavalue true fmt t
| B.Fun (_, args, ret) ->
if parfun then Format.fprintf fmt "(%a)" (aux false) t else
let paren_out = true in
let args = unit_fun args in
Format.fprintf fmt "%a -> %a" (Format.pp_list " -> " (aux true)) args (aux paren_out) ret
(* all external and record types from bsl should be named *)
| B.External (_, n, vs) ->
OcamlPrint.pp_parameters (aux true) n fmt vs
in aux false fmt
let pp_definition fmt = function
| B.External (_, name, params) ->
Format.fprintf fmt "type %a" (OcamlPrint.pp_parameters pp name) params
| t -> !! t "Unexpected type for an external type definition@\n"
end
module C =
struct
(* Prototype use only - Not maintained, not supported *)
(* <!> Hard link with Pervasives of C *)
let ty_alphaval = "ty_alphaval"
let ty_void = "ty_void"
let ty_bool = "ty_bool"
let ty_opavalue = "ty_opavalue"
let pp fmt =
let rec aux fmt = function
| B.Const (_, c) ->
Format.pp_print_string fmt (QmlAst.Const.cbsl_string_of_ty c)
| B.TypeVar _ ->
Format.pp_print_string fmt ty_alphaval
| B.Void _ ->
Format.pp_print_string fmt ty_void
| B.Bool _ ->
Format.pp_print_string fmt ty_bool
(* In the prototype, option('a) is a [('a)*] *)
| B.Option (_, o) ->
Format.fprintf fmt "%a*" aux o
(* FIXME: we can probably do better *)
| B.OpaValue _ ->
Format.pp_print_string fmt ty_opavalue
| B.Fun (_, args, ret) -> (
match args with
| [] ->
Format.fprintf fmt "%a(%s)" aux ret ty_void
| _ ->
Format.fprintf fmt "%a(%a)" aux ret (Format.pp_list ",@ " aux) args
)
| B.External (_, name, []) ->
Format.pp_print_string fmt name
(* wait for LLVM to decide what we want to do *)
| B.External _ -> assert false
in
aux fmt
end