-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
functions.re
388 lines (375 loc) 路 9.82 KB
/
functions.re
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
open Grain_tests.TestFramework;
open Grain_tests.Runner;
describe("functions", ({test, testSkip}) => {
let test_or_skip =
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;
let assertSnapshot = makeSnapshotRunner(test);
let assertCompileError = makeCompileErrorRunner(test);
let assertRun = makeRunner(test_or_skip);
let assertFileRun = makeFileRunner(test_or_skip);
assertFileRun("fib1", "fib", "55\n");
assertFileRun("fib2", "fib-better", "75025\n");
assertFileRun("fib_big", "fib-bigint", "354224848179261915075\n");
assertFileRun("indirect", "indirect-tail", "10\n");
/* NOTE: This file also will test that we're doing tail calls
and mutual recursion properly (should stack overflow otherwise) */
/* Tests tail calls on only on one branch */
assertFileRun("one_branch_tail_call", "oneBranchTail", "[2]\n");
assertFileRun("forward_decl", "forward-decl", "true\n");
/* This will test that we are doing tail calls for arbitrary-arity
functions correctly */
assertFileRun("sinister_tail_call", "sinister-tail-call", "true\n");
assertRun("func_no_args", "let foo = (() => {print(5)});\nfoo()", "5\n");
assertCompileError(
"multi_bind",
"let x = 2 and y = x + 1; y",
"Unbound value x",
);
assertSnapshot("multi_bind2", "let x = 2 and y = 3; y");
assertSnapshot("curried_func", "let add = a => b => a + b; add(2)(3)");
assertCompileError("unbound_fun", "2 + foo()", "unbound");
assertCompileError("unbound_id_simple", "5 - x", "unbound");
assertCompileError("unbound_id_let", "let x = x; 2 + 2", "unbound");
assertCompileError(
"shadow_multi",
"let x = 12 and x = 14; x",
"Variable x is bound several times",
);
assertSnapshot(
"dup_func",
"let rec foo = (() => {5});\nlet bar = (() => { 7 });\nlet rec foo = (() => {9});\nfoo()",
);
assertCompileError("arity_1", "let foo = (() => {5});\nfoo(6)", "type");
assertCompileError(
"arity_2",
"let foo = ((x) => {x + 5});\nfoo()",
"missing an argument of type x: Number",
);
assertCompileError(
"arity_3",
"let foo = ((x) => {x});\nfoo(1, 2, 3)",
"type",
);
assertSnapshot("shorthand_1", "let foo = (x) => x; foo(1)");
assertSnapshot("shorthand_2", "let foo = (x) => x + 3; foo(1)");
assertSnapshot("shorthand_3", "let foo = x => x; foo(1)");
assertSnapshot("shorthand_4", "let foo = x => x + 3; foo(1)");
// Trailing commas
assertSnapshot(
"fn_trailing_comma",
"let testFn = (x, y,) => x + y; testFn(2, 3,)",
);
assertRun(
"adt_trailing_comma",
"enum Topping { Cheese, Pepperoni, Peppers, Pineapple, }
enum Dough { WholeWheat, GlutenFree }
enum Menu { Pizza(Topping,Dough,), Calzone(Topping,Dough,) }
let item = Calzone(Peppers, WholeWheat,)
print(item)
",
"Calzone(Peppers, WholeWheat)\n",
);
assertSnapshot("lam_destructure_1", "((_) => 5)(\"foo\")");
assertSnapshot("lam_destructure_2", "let foo = (_) => 5; foo(\"foo\")");
assertSnapshot(
"lam_destructure_3",
"(((a, b, c)) => a + b + c)((1, 2, 3))",
);
assertSnapshot(
"lam_destructure_4",
"let foo = ((a, b, c)) => a + b + c; foo((1, 2, 3))",
);
assertSnapshot(
"lam_destructure_5",
"(((a, b, c), (x, y)) => a + b + c + x + y)((1, 2, 3), (4, 5))",
);
assertSnapshot(
"lam_destructure_6",
"let foo = ((a, b, c), (x, y)) => a + b + c + x + y; foo((1, 2, 3), (4, 5))",
);
assertSnapshot(
"lam_destructure_7",
"(((a, b, (c, d))) => a + b + c + d)((1, 2, (3, 4)))",
);
assertSnapshot(
"lam_destructure_8",
"let foo = ((a, b, (c, d))) => a + b + c + d; foo((1, 2, (3, 4)))",
);
assertRun("lambda_1", "print((x) => {x})", "<lambda>\n");
assertSnapshot("app_1", "((x) => {x})(1)");
assertRun(
"letrec_1",
"let rec x = ((n) => {if (n > 3) {n} else {x(n + 2)}})\n and y = ((n) => {x(n + 1)});\n print(y(2))",
"5\n",
);
/* Check that recursion is order-independent */
assertRun(
"letrec_2",
"let rec y = ((n) => {x(n + 1)})\n and x = ((n) => {if (n > 3) {n} else {x(n + 2)}});\n print(y(2))",
"5\n",
);
assertRun(
"let_1",
"let rec x = ((n) => {n + 1})\n and y = (() => x(3))\n and z = ((n) => {x(n) + y()});\n print(z(5))",
"10\n",
);
assertCompileError(
"let_norec_1",
"let x = ((n) => {if (n > 3) {n} else {x(n + 2)}})\n and y = ((n) => {x(n + 1)});\n print(y(2))",
"Unbound value x.",
);
assertCompileError(
"lambda_dup_args",
"((x, y, x) => {5})",
"Variable x is bound several times",
);
assertCompileError(
"lambda_arity_1",
"((x) => {6})()",
"missing an argument",
);
assertCompileError("lambda_arity_2", "((x) => {5})(1, 2)", "type");
assertCompileError(
"letrec_nonstatic_const",
"let rec x = 5; x",
"let rec may only be used with recursive function definitions",
);
assertCompileError(
"letrec_nonstatic_same",
"let x = x; x",
"Unbound value x.\n Hint: You are probably missing the `rec' keyword on line 1.",
);
assertCompileError(
"letrec_nonstatic_other",
"let rec x = ((z) => {z + 1}) and y = x; y(2)",
"let rec may only be used with recursive function definitions",
);
assertCompileError("nonfunction_1", "let x = 5; x(3)", "type");
assertSnapshot("lambda_pat_any", "let x = (_) => 5; x(\"foo\")");
assertCompileError(
"unknown_toplevel_attribute",
"@unknown let x = () => 5",
"Unknown top-level attribute",
);
assertCompileError(
~module_header="@unsafe module Test",
"unknown_module_attribute",
"",
"Unknown module attribute",
);
assertSnapshot(
"func_record_associativity1",
"record Foo { f: () => Bool }; let foo = {f: () => false}; !foo.f()",
);
assertSnapshot(
"func_record_associativity2",
"record Foo { g: () => Bool }; record Bar { f: Foo }; let foo = {f: {g: () => false}}; !foo.f.g()",
);
assertSnapshot(
"func_recursive_closure",
{|let makeAdder = (n) => (x) => x + n
provide let truc = () => {
let rec foo = (x) => {
let baz = makeAdder(1);
let bar = y => foo(0) + baz(1);
if (x == 0) {
0
} else if (x == 1) {
bar(1)
} else {
foo(x - 1)
}
}
foo(5)
}
truc()|},
);
assertRun(
"func_mutually_recursive_closure",
{|
let main = () => {
let closureScope = "closureScope"
let rec isEven = n => {
print(closureScope)
if (n <= 1) {
n == 0
} else {
isOdd(n - 1)
}
}
and isOdd = n => {
if (n <= 1) {
n == 1
} else {
isEven(n - 1)
}
}
print(isOdd(3))
}
main()
|},
"closureScope\ntrue\n",
);
assertCompileError(
"newline_before_arrow",
{|
let x = ()
=> 1
|},
"Expected an expression.",
);
assertSnapshot(
"regression_1725",
{|
let foo = () => {
let bar = return 5
return 6
}
foo()
|},
);
assertRun(
"labeled_args1",
{|
let concat = (a, b) => a ++ b
print(concat(a="1", b="2"))
|},
"12\n",
);
assertRun(
"labeled_args2",
{|
let concat = (a, b) => a ++ b
print(concat(a="1", "2"))
|},
"12\n",
);
assertRun(
"labeled_args3",
{|
let concat = (a, b) => a ++ b
print(concat("2", a="1"))
|},
"12\n",
);
assertRun(
"labeled_args4",
{|
let concat = (a, b) => a ++ b
print(concat("1", b="2"))
|},
"12\n",
);
assertRun(
"labeled_args5",
{|
let concat = (a, b) => a ++ b
print(concat(b="2", "1"))
|},
"12\n",
);
assertRun(
"labeled_args6",
{|
let nothing = (a, b) => void
nothing(b=print(1), print(2))
|},
"1\n2\n",
);
assertRun(
"default_args1",
{|
let concat = (a="1", b) => a ++ b
print(concat(a="3", "2"))
|},
"32\n",
);
assertRun(
"default_args2",
{|
let concat = (a="1", b) => a ++ b
print(concat(a="3", b="2"))
|},
"32\n",
);
assertRun(
"default_args3",
{|
let concat = (a="1", b) => a ++ b
print(concat(b="2", a="3"))
|},
"32\n",
);
assertRun(
"default_args4",
{|
let concat = (a="1", b) => a ++ b
print(concat("2"))
|},
"12\n",
);
assertRun(
"default_args5",
{|
let concat = (a="1", b) => a ++ b
print(concat(b="2"))
|},
"12\n",
);
assertRun(
"default_args6",
{|
let concat = (a=b ++ b, b) => a ++ b
print(concat(b="9"))
|},
"999\n",
);
assertRun(
"labeled_args_typecheck1",
{|
let apply = (f: (arg1: Number) => Number) => f(arg1=5)
print(apply(notarg1 => notarg1))
|},
"5\n",
);
assertCompileError(
"labeled_args_err1",
{|
let concat = (a, b) => a ++ b
print(concat(c="3"))
|},
"This argument cannot be supplied with label c",
);
assertCompileError(
"labeled_args_err2",
{|
let concat = (a, b) => a ++ b
print(concat("1", "2", c="3"))
|},
"This argument cannot be supplied with label c",
);
assertCompileError(
"labeled_args_err3",
{|
let concat = (a="1", b) => a ++ b
print(concat("1", "2"))
|},
"Did you mean to supply an argument with label a?",
);
assertCompileError(
"labeled_args_err4",
{|
let apply = (f: (?arg1: Number) => Number) => f(arg1=5)
print(apply(notarg1 => notarg1))
|},
"The expected function type contains the argument \\?arg1",
);
assertCompileError(
"labeled_args_err5",
{|
let apply = (f: (?arg1: Number) => Number) => f(arg1=5)
print(apply(notarg1 => notarg1))
|},
"which has a default value, but the matching argument does not.",
);
});