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
[@@@ocaml.text "/*"]

(** Copyright 2021-2024, Kakadu and contributors *)

(** SPDX-License-Identifier: LGPL-3.0-or-later *)

[@@@ocaml.text "/*"]

open Interpret_lib

let eval ?(steps = 10_000) src =
  match Interpret.run ~steps src with
  | Ok (v, output) ->
    List.iter print_endline output;
    print_endline (Interpret.format_value v)
  | Error e -> print_endline (Interpret.format_error e)
;;

let%expect_test "integer literal" =
  eval "42";
  [%expect {| 42 |}]
;;

let%expect_test "arithmetic precedence" =
  eval "2 + 3 * 4";
  [%expect {| 14 |}]
;;

let%expect_test "parenthesised expression" =
  eval "(2 + 3) * 4";
  [%expect {| 20 |}]
;;

let%expect_test "unary minus" =
  eval "-5 + 2";
  [%expect {| -3 |}]
;;

let%expect_test "comparisons yield ints" =
  eval "3 < 5";
  [%expect {| 1 |}];
  eval "5 < 3";
  [%expect {| 0 |}]
;;

let%expect_test "neq operator" =
  eval "3 <> 5";
  [%expect {| 1 |}];
  eval "3 <> 3";
  [%expect {| 0 |}]
;;

let%expect_test "le operator" =
  eval "3 <= 5";
  [%expect {| 1 |}];
  eval "5 <= 3";
  [%expect {| 0 |}]
;;

let%expect_test "gt operator" =
  eval "5 > 3";
  [%expect {| 1 |}];
  eval "3 > 5";
  [%expect {| 0 |}]
;;

let%expect_test "ge operator" =
  eval "5 >= 5";
  [%expect {| 1 |}];
  eval "3 >= 5";
  [%expect {| 0 |}]
;;

let%expect_test "let binding" =
  eval "let x = 10 in x + 7";
  [%expect {| 17 |}]
;;

let%expect_test "shadowing" =
  eval "let x = 1 in let x = 2 in x + 10";
  [%expect {| 12 |}]
;;

let%expect_test "closures capture environment" =
  eval {|
    let x = 10 in
    let f = fun y -> x + y in
    let x = 100 in
    f 5
  |};
  [%expect {| 15 |}]
;;

let%expect_test "lambda application" =
  eval "(fun x -> x + 1) 5";
  [%expect {| 6 |}]
;;

let%expect_test "multi-argument sugar" =
  eval "let add = fun x y -> x + y in add 3 4";
  [%expect {| 7 |}]
;;

let%expect_test "partial application" =
  eval "let add x y = x + y in let add5 = add 5 in add5 3 + add5 2";
  [%expect {| 15 |}]
;;

let%expect_test "higher-order function" =
  eval "let apply_twice f x = f (f x) in let inc x = x + 1 in apply_twice inc 0";
  [%expect {| 2 |}]
;;

let%expect_test "returning a function prints <fun>" =
  eval "fun x -> x";
  [%expect {| <fun> |}]
;;

let%expect_test "factorial" =
  eval "let rec fact n = if n = 0 then 1 else n * fact (n - 1) in fact 5";
  [%expect {| 120 |}]
;;

let%expect_test "fibonacci" =
  eval
    {|
    let rec fib n =
      if n = 0 then 0
      else if n = 1 then 1
      else fib (n - 1) + fib (n - 2)
    in fib 10
  |};
  [%expect {| 55 |}]
;;

let%expect_test "print side-effect and value" =
  eval "let _ = print 42 in 0";
  [%expect {|
    42
    0 |}]
;;

let%expect_test "multiple prints" =
  eval "let _ = print 1 in let _ = print 2 in 3";
  [%expect {|
    1
    2
    3 |}]
;;

let%expect_test "infinite loop hits step limit" =
  eval ~steps:50 "let rec loop x = loop x in loop 1";
  [%expect {| Error: step limit exceeded |}]
;;

let%expect_test "factorial with tight limit" =
  eval ~steps:20 "let rec fact n = if n = 0 then 1 else n * fact (n - 1) in fact 10";
  [%expect {| Error: step limit exceeded |}]
;;

let%expect_test "factorial with enough steps" =
  eval ~steps:5000 "let rec fact n = if n = 0 then 1 else n * fact (n - 1) in fact 10";
  [%expect {| 3628800 |}]
;;

let%expect_test "unbound variable" =
  eval "x + 1";
  [%expect {| Error: unbound variable x |}]
;;

let%expect_test "not a function" =
  eval "5 3";
  [%expect {| Error: not a function |}]
;;

let%expect_test "division by zero" =
  eval "10 / 0";
  [%expect {| Error: division by zero |}]
;;

let%expect_test "if condition is not int" =
  eval "if fun x -> x then 1 else 2";
  [%expect {| Error: expected an integer |}]
;;

let%expect_test "binop on non-int" =
  eval "1 + (fun x -> x)";
  [%expect {| Error: expected an integer |}]
;;

let%expect_test "print non-int" =
  eval "print (fun x -> x)";
  [%expect {| Error: expected an integer |}]
;;

let%expect_test "let rec with non-function" =
  eval "let rec x = 1 in x";
  [%expect {| Error: let rec requires a function on the right-hand side |}]
;;

let%expect_test "parse error" =
  eval "let x = in 5";
  [%expect {| Parse error |}]
;;

let%expect_test "pp_value for integers" =
  Format.printf "%a" Interpret.pp_value (Interpret.VInt 99);
  [%expect {| 99 |}]
;;

let%expect_test "pp_value for closure" =
  Format.printf "%a" Interpret.pp_value (Interpret.VClosure ("x", Ast.EVar "x", []));
  [%expect {| <closure> |}]
;;

let%expect_test "pp_value for builtin" =
  Format.printf
    "%a"
    Interpret.pp_value
    (Interpret.VBuiltin (fun _ -> Interpret.return (Interpret.VInt 0)));
  [%expect {| <builtin> |}]
;;

let%expect_test "format_value for closure" =
  print_endline (Interpret.format_value (Interpret.VClosure ("x", Ast.EVar "x", [])));
  [%expect {| <fun> |}]
;;

let%expect_test "format_value for builtin" =
  print_endline
    (Interpret.format_value
       (Interpret.VBuiltin (fun _ -> Interpret.return (Interpret.VInt 0))));
  [%expect {| <fun> |}]
;;

let%expect_test "pprint negative constant" =
  print_endline (Pprint.to_string (Ast.EConst (-5)));
  [%expect {| (-5) |}]
;;

let%expect_test "factorial via fix combinator" =
  eval
    {|
    let fix = fun f ->
      (fun x -> f (fun v -> x x v)) (fun x -> f (fun v -> x x v))
    in
    let fact = fix (fun self -> fun n ->
      if n = 0 then 1 else n * self (n - 1))
    in
    fact 5
  |};
  [%expect {| 120 |}]
;;

let%expect_test "fibonacci via fix combinator" =
  eval
    {|
    let fix = fun f ->
      (fun x -> f (fun v -> x x v)) (fun x -> f (fun v -> x x v))
    in
    let fib = fix (fun self -> fun n ->
      if n = 0 then 0
      else if n = 1 then 1
      else self (n - 1) + self (n - 2))
    in
    fib 10
  |};
  [%expect {| 55 |}]
;;

let%expect_test "shadow builtin print" =
  eval "let print = fun x -> x + 1 in print 5";
  [%expect {| 6 |}]
;;

let%expect_test "shadow builtin print restores after scope" =
  eval
    {|
    let _ =
      let print = fun x -> x * 2 in
      print 10
    in
    let _ = print 42 in 0
  |};
  [%expect {|
    42
    0 |}]
;;