Debian Codegraph @

test/examples.mlt

raw
1 module Test_simple_if = struct
2
3 open Core
4
5 [%% if true ]
6 let x = "OK"
7 [%% else ]
8 let x = "BAD"
9 [%% endif]
10
11 let () = printf "%s" x
12 end
13 [%%expect{|
14 OK
15 |}]
16
17 module Test_comments = struct
18
19 open Core
20
21 [%% if (* comment *) true ]
22 let x = "OK"
23 [%% else ]
24 let x = "BAD"
25 (* comment
26 *)
27 [%% endif]
28
29 let () = printf "%s" x
30
31 end
32 [%%expect{|
33 OK
34 |}]
35 module Test_nested_if = struct
36 open Core
37
38 [%% define ABC true ]
39 [%% define xyz false ]
40 [%% if ABC ]
41 let x = "OK1"
42 [%% else ]
43 [%% if xyz ]
44 let x = "BAD1"
45 [%% endif]
46 let x = "BAD2"
47 [%% endif]
48 let y = "OK2"
49
50 let () = printf "%s %s" x y
51 end
52 [%%expect{|
53 OK1 OK2
54 |}]
55
56
57 module Test_defined = struct
58 open Core
59
60 [%% define FOO ]
61 [%% undef BAR ]
62
63 [%% if (defined FOO) ]
64 let x = "OK1"
65 [%% else ]
66 let x = "BAD1"
67 [%% endif ]
68
69 [%% if (defined BAR) ]
70 let y = "BAD2"
71 [%% else ]
72 let y = "OK2"
73 [%% endif ]
74
75 let () = printf "%s %s" x y
76 end
77 [%%expect{|
78 OK1 OK2
79 |}]
80
81 module Test_error_simple = struct
82 [%% warning "Ooops."]
83 [%% error "Big ooooooops."]
84 end
85 [%%expect{|
86 Line _, characters _-_:
87 Error: Big ooooooops.
88 File "examples.mlt", line 82, characters 6-13::
89 Warning Ooops.
90 |}]
91
92 module Test_optional_variant = struct
93 open Core
94
95 [%%define FOO]
96 [%%undef BAR]
97 [%%define BAZ 3]
98
99 type t =
100 | OK_1
101 | OK_2 [@if defined FOO]
102 | OK_3 [@if not (defined BAR)]
103 | BAD_4 [@if BAZ < 0]
104 | OK_5
105
106 let _ : t*t*t*t = OK_1, OK_2, OK_3, OK_5
107 let _ : t = BAD_4
108
109 end
110 [%%expect{|
111 Line _, characters _-_:
112 Error: This variant expression is expected to have type t
113 There is no constructor BAD_4 within type t
114 |}]
115
116 module Test_import_order = struct
117 [%% define A true]
118 [%% define B A]
119 [%% import "test_imported/order/cd.ml"]
120 [%% define E D]
121 end
122 [%%expect{| |}]
123
124 module Test_nested_error = struct
125 [%% import "test_imported/error/a.ml" ]
126 end
127 [%%expect{|
128 Line _, characters _-_:
129 Error: nested error
130 |}]
131
132 module Test_import_config = struct
133 open Core
134
135 [%% import "test_imported/config.h"]
136 [%% import "test_imported/config.h"] (* try importing twice *)
137
138 [%% if (defined FOO) ]
139 let x = "OK"
140 [%% else]
141 let x = "BAD"
142 [%% endif]
143
144 let () = printf "%s" x
145 end
146 [%%expect{|
147 OK
148 |}]
149
150 module Test_order = struct
151 open Core
152
153 let () = printf "%s" "a"
154 let () = printf "%s" "b"
155
156 [%% if true ]
157 let () = printf "%s" "c"
158 let () = printf "%s" "d"
159 [%% define ABC]
160 let () = printf "%s" "e"
161 let () = printf "%s" "f"
162 [%% endif ]
163
164 [%% if false ]
165 [%% else ]
166 let () = printf "%s" "g"
167 let () = printf "%s" "h"
168 [%% define ABC]
169 let () = printf "%s" "i"
170 let () = printf "%s" "j"
171 [%% endif ]
172
173 [%% if false ]
174 [%% elif true ]
175 let () = printf "%s" "k"
176 let () = printf "%s" "l"
177 [%% define ABC]
178 let () = printf "%s" "m"
179 let () = printf "%s" "n"
180 [%% endif ]
181
182 let () = printf "%s" "o"
183 let () = printf "%s" "p"
184 [%% define ABC]
185 let () = printf "%s" "q"
186 let () = printf "%s" "r"
187 end
188 [%%expect{|
189 abcdefghijklmnopqr
190 |}]
191
192 module Test_show = struct
193 open Core
194
195 [%% if (show (3+3*3)) > 0]
196 let () = printf "%s" "OK";
197 [%% endif ]
198
199 [%% define x "OK"]
200 module A = struct
201 [%% define y (show x)]
202 end
203 [%% define x "BAD"]
204 end
205 [%%expect{|
206 File "examples.mlt", line 195, characters 9-23::
207 SHOW 12
208 File "examples.mlt", line 201, characters 17-25::
209 SHOW "OK"
210 OK
211 |}]
212
213 module Test_optional_match = struct
214 open Core
215
216 [%%define FOO]
217 [%%undef BAR]
218
219 type t =
220 | OK1 of string
221 | OK2 of string * int [@if defined FOO]
222 | BAD of int [@if defined BAR]
223
224 let () = match ((OK2 ("OK", 2)): t) with
225 | OK1 s -> printf "%s" s
226 | OK2 (y,z) [@if defined FOO] -> printf "%s %i\n" y z
227 | BAD _ [@if defined BAR] -> printf "This will cause error if not dropped %s" undefined_ident
228
229 let f = function
230 | OK1 _ -> "OK"
231 | OK2 _ -> "OK"
232 | BAD _ [@if defined BAR] -> "BAD"
233
234 let () = printf "%s" (f (OK1 "abc"))
235 end
236 [%%expect{|
237 OK 2
238 OK
239 |}]
240
241 [%%define FOO]
242
243 module Test_signature : sig
244 [%% if defined FOO]
245 type t
246 [%%else]
247 type s
248 [%%endif]
249 type u
250 end = struct
251 [%% if defined FOO]
252 type t
253 [%%else]
254 type s
255 [%%endif]
256 type u
257 end
258 [%%expect{| |}]
259
260 module Test_drop_attr = struct
261 [%%if false]
262 external abc : unit -> int = "abc" [@@noalloc]
263 [%%endif]
264
265 type s
266 end
267 [%%expect{| |}]
268
269 module Test_qualified_name = struct
270 [%% optcomp.define FOO 3 ]
271
272 type t = A | B [@optcomp.if FOO = 3]
273 end
274 [%%expect{| |}]
275
276 module Test_optcomp_first = struct
277 type t = A | B [@if false]
278 [@@deriving sexp]
279 end
280 [%%expect{| |}]
281
282 module Test_ifndef_quirk = struct
283 [%%ifndef FOO_NOT_USED_BEFORE]
284 [%%define FOO_NOT_USED_BEFORE]
285 [%%endif]
286 end
287 [%%expect{| |}]
288
289 module Test_scoping = struct
290 open Core
291 [%%define x "OK"]
292 module B = struct
293 module A = struct
294 [%%define x "BAD1"]
295 [%%ifdef x]
296 let () = printf "%s" "OK2"
297 [%%endif]
298 end
299 [%%define y (show x)]
300 end
301 [%%define x "BAD2"]
302 end
303
304 [%%expect{|
305 File "examples.mlt", line 299, characters 16-24::
306 SHOW "OK"
307 OK2
308 |}]
309
310 module Test_scoping2 = struct
311 [%%define x "<top>"]
312
313 module A = struct
314 [%%define x "A"]
315
316 [%%define y (show ("in A", x))]
317 end
318
319 module B = struct
320 [%%define y (show ("in B", x))]
321 end
322
323 [%%define y (show ("at toplevel", x))]
324 end
325
326 [%%expect{|
327 File "examples.mlt", line 316, characters 16-34::
328 SHOW ("in A", "A")
329 File "examples.mlt", line 320, characters 16-34::
330 SHOW ("in B", "<top>")
331 File "examples.mlt", line 323, characters 14-39::
332 SHOW ("at toplevel", "<top>")
333 |}]
334
335 module Test_scoping3 = struct
336
337 module A = struct
338 [%%define x "A"]
339
340 [%%define y (show ("in A", x))]
341 end
342
343 let () = ()
344
345 [%%define x "top"]
346
347 let () = ()
348
349 [%%define y (show ("between", x))]
350
351 let () = ()
352
353 module B = struct
354 [%%define y (show ("in B", x))]
355 end
356
357 end
358
359 [%%expect{|
360 File "examples.mlt", line 340, characters 16-34::
361 SHOW ("in A", "A")
362 File "examples.mlt", line 349, characters 14-35::
363 SHOW ("between", "top")
364 File "examples.mlt", line 354, characters 16-34::
365 SHOW ("in B", "top")
366 |}]
367
368 (* Test env is preserved in toplevel *)
369 [%%define x "test_toplevel"]
370 [%%define y x]
371 [%%define y (show y)]
372
373 [%%expect{|
374 File "examples.mlt", line 371, characters 12-20::
375 SHOW "test_toplevel"
376 |}]