monkey-interpreter 2.0.1

an interpreter for monkeylang
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::rc::Rc;

    use object::environment::*;
    use parser::*;

    use crate::eval;

    fn apply_test(test_cases: &[(&str, &str)]) {
        let env: Env = Rc::new(RefCell::new(Default::default()));
        for (input, expected) in test_cases {
            match parse(input) {
                Ok(node) => match eval(node, &env) {
                    Ok(evaluated) => assert_eq!(&format!("{}", evaluated), expected),
                    Err(e) => assert_eq!(&e.to_string(), expected),
                },
                Err(e) => panic!("parse error: {}", e[0]),
            }
        }
    }

    fn apply_error_test(test_cases: &[(&str, &str)]) {
        let env: Env = Rc::new(RefCell::new(Default::default()));
        for (input, expected) in test_cases {
            match parse(input) {
                Ok(node) => match eval(node, &env) {
                    Ok(evaluated) => panic!("expected `{}` to fail, got {}", input, evaluated),
                    Err(e) => assert_eq!(&e.to_string(), expected),
                },
                Err(e) => panic!("parse error: {}", e[0]),
            }
        }
    }

    #[test]
    fn test_integer_expressions() {
        let test_case = [
            ("1", "1"),
            ("-10", "-10"),
            ("5 + 5 + 5 + 5 - 10", "10"),
            ("2 * 2 * 2 * 2 * 2", "32"),
            ("(5 + 10 * 2 + 15 / 3) * 2 + -10", "50"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_boolean_expressions() {
        let test_case = [
            ("true", "true"),
            ("false", "false"),
            ("1 < 2", "true"),
            ("1 > 2", "false"),
            ("1 < 1", "false"),
            ("1 > 1", "false"),
            ("1 == 1", "true"),
            ("1 != 1", "false"),
            ("1 == 2", "false"),
            ("1 != 2", "true"),
            ("true == true", "true"),
            ("false == false", "true"),
            ("true == false", "false"),
            ("true != false", "true"),
            ("false != true", "true"),
            ("(1 < 2) == true", "true"),
            ("(1 < 2) == false", "false"),
            ("(1 > 2) == true", "false"),
            ("(1 > 2) == false", "true"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_bang_operators() {
        let test_case = [
            ("!true", "false"),
            ("!false", "true"),
            ("!5", "false"),
            ("!!true", "true"),
            ("!!false", "false"),
            ("!!5", "true"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_if_else_expressions() {
        let test_case = [
            ("if (true) { 10 }", "10"),
            ("if (false) { 10 }", "null"),
            ("if (1) { 10 }", "10"),
            ("if (1 < 2) { 10 }", "10"),
            ("if (1 > 2) { 10 }", "null"),
            ("if (1 > 2) { 10 } else { 20 }", "20"),
            ("if (1 < 2) { 10 } else { 20 }", "10"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_debugger_statements_are_completion_transparent() {
        let test_case = [
            ("debugger;", "null"),
            ("debugger; debugger;", "null"),
            ("1; debugger;", "1"),
            ("debugger; 1;", "1"),
            ("let a = 5; debugger; a;", "5"),
            ("if (true) { debugger; }", "null"),
            ("if (true) { 10; debugger; }", "10"),
            ("fn() { debugger; }()", "null"),
            ("fn() { 1; debugger; }()", "1"),
            ("fn() { let x = 1; debugger; }()", "null"),
            ("fn() { return 2; debugger; }()", "2"),
            ("fn(x) { if (x) { 1; debugger; } }(true)", "1"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_return_statements() {
        let test_case = [
            ("return 10;", "10"),
            ("return 10; 9;", "10"),
            ("return 2 * 5; 9;", "10"),
            ("9; return 2 * 5; 9;", "10"),
            ("if (10 > 1) { return 10; }", "10"),
            (
                "if (10 > 1) { \
                 if (10 > 1) { \
                 return 10; \
                 } \
                 return 1; \
                 }",
                "10",
            ),
            (
                "let f = fn(x) { \
                 return x; \
                 x + 10; \
                 }; \
                 f(10);",
                "10",
            ),
            (
                "let f = fn(x) { \
                 let result = x + 10; \
                 return result; \
                 return 10; \
                 }; \
                 f(10);",
                "20",
            ),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_let_statements() {
        let test_case = [
            ("let a = 5; a;", "5"),
            ("let a = 5 * 5; a;", "25"),
            ("let a = 5; let b = a; b;", "5"),
            ("let a = 5; let b = a; let c = a + b + 5; c;", "15"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_function_object() {
        let test_case = [("fn(x) { x + 2; };", "fn(x) { (x + 2) }")];
        apply_test(&test_case);
    }

    #[test]
    fn test_annotated_function_object() {
        // The tree-walking interpreter keeps the parameter list it was built
        // from, so printing a function shows its annotations. This is the one
        // declared exception to type erasure: see docs/type-system-design.md
        // section 6.1.
        let test_case = [
            ("fn(x: int): int { x + 2; };", "fn(x: int) { (x + 2) }"),
            ("fn(x: [int]?, y): bool { x; };", "fn(x: [int]?, y) { x }"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_annotations_do_not_change_results() {
        let test_case = [
            ("let add = fn(x: int, y: int): int { x + y; }; add(5, 5);", "10"),
            ("let xs: [int] = [1, 2, 3]; xs[1]", "2"),
            ("let m: {string: int} = {\"a\": 1}; m[\"a\"]", "1"),
            (
                "let apply = fn(f: fn(int): int, v: int): int { f(v) }; apply(fn(n: int): int { n * 2 }, 4)",
                "8",
            ),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_function_application() {
        let test_case = [
            ("let identity = fn(x) { x; }; identity(5);", "5"),
            ("let identity = fn(x) { return x; }; identity(5);", "5"),
            ("let double = fn(x) { x * 2; }; double(5);", "10"),
            ("let add = fn(x, y) { x + y; }; add(5, 5);", "10"),
            ("let add = fn(x, y) { x + y; }; add(5 + 5, add(5, 5));", "20"),
            ("fn(x) { x; }(5)", "5"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_closures_capture_binding_identity() {
        let test_case = [
            (
                r#"let captured: int = 1;
let read: fn(): int = fn(): int { captured + 1; };
let captured: string = "later";
read();"#,
                "2",
            ),
            (
                r#"let outer_value = 1;
let make_reader = fn() { fn() { outer_value; }; };
let nested_reader = make_reader();
let outer_value = 2;
nested_reader();"#,
                "1",
            ),
            (
                r#"let factorial = fn(n) {
  if (n == 0) { 1 } else { n * factorial(n - 1) }
};
factorial(5);"#,
                "120",
            ),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_string_concatenation() {
        let test_case = [
            (r#""Hello" + " " + "World!""#, "Hello World!"),
            (r#""Hello" == "Hello""#, "true"),
            (r#""Hello" == "Hi""#, "false"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_builtin_functions() {
        let test_case = [
            (r#"len("")"#, "0"),
            (r#"len("four")"#, "4"),
            (r#"len("hello world")"#, "11"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_array_literals() {
        let test_case = [("[1, 2 * 2, 3 + 3]", "[1, 4, 6]")];
        apply_test(&test_case);
    }

    #[test]
    fn test_array_index_expressions() {
        let test_case = [
            ("let i = 0; [1][i];", "1"),
            ("[1, 2, 3][1 + 1];", "3"),
            ("let myArray = [1, 2, 3]; myArray[2];", "3"),
            ("let myArray = [1, 2, 3]; myArray[0] + myArray[1] + myArray[2];", "6"),
            ("let myArray = [1, 2, 3]; let i = myArray[0]; myArray[i]", "2"),
            ("[1, 2, 3][3]", "null"),
            ("[1, 2, 3][-1]", "null"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_array_builtin_functions() {
        let test_case = [
            ("len([1, 2, 3])", "3"),
            ("len([])", "0"),
            (r#"puts("hello", "world!")"#, "null"),
            ("first([1, 2, 3])", "1"),
            ("first([])", "null"),
            ("last([1, 2, 3])", "3"),
            ("last([])", "null"),
            ("rest([1, 2, 3])", "[2, 3]"),
            ("rest([])", "null"),
            ("push([], 1)", "[1]"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_builtin_errors_stop_evaluation() {
        let test_case = [
            ("len(1)", "builtin len not supported for for type 1"),
            (r#"len("one", "two")"#, "builtin len expected 1 argument, got 2"),
            ("first(1)", "builtin first not supported for for type 1"),
            ("last(1)", "builtin last not supported for for type 1"),
            ("rest(1)", "builtin rest not supported for for type 1"),
            ("push(1, 1)", "builtin push not supported for for type 1"),
            // a failed builtin must abort the program instead of flowing on as a value
            ("if (len(1)) { 10 } else { 20 }", "builtin len not supported for for type 1"),
            ("len(1) == len(1)", "builtin len not supported for for type 1"),
            ("len(1); 42", "builtin len not supported for for type 1"),
            ("let broken = len(1); broken", "builtin len not supported for for type 1"),
            (
                "let identity = fn(x) { x }; identity(len(1))",
                "builtin len not supported for for type 1",
            ),
        ];
        apply_error_test(&test_case);
    }

    #[test]
    fn test_integer_arithmetic_errors() {
        // Raw i64 arithmetic used to panic in debug builds (and wrap in
        // release builds) for all of these; `1 / 0` and `i64::MIN / -1`
        // panicked in release builds too. They must be runtime errors, with
        // the same wording the bytecode VM uses.
        let test_case = [
            ("1 / 0;", "division by zero"),
            ("9223372036854775807 + 1;", "integer overflow in addition"),
            ("let m = -9223372036854775807 - 1; m - 1;", "integer overflow in subtraction"),
            ("9223372036854775807 * 2;", "integer overflow in multiplication"),
            ("let m = -9223372036854775807 - 1; m / -1;", "integer overflow in division"),
            ("let m = -9223372036854775807 - 1; -m;", "integer overflow in negation"),
        ];
        apply_error_test(&test_case);
    }

    #[test]
    fn test_integer_arithmetic_boundaries_still_evaluate() {
        let test_case = [
            ("9223372036854775807 + 0;", "9223372036854775807"),
            ("-9223372036854775807 - 1;", "-9223372036854775808"),
            ("(-9223372036854775807 - 1) / 1;", "-9223372036854775808"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_array_builtin_arity_errors() {
        // A wrong argument count must produce an error, not a panic and not a
        // silently truncated call. `push` used to read args.first()/args.last(),
        // so `push([1])` appended the array to itself and `push([1], 2, 3)`
        // dropped the middle argument; `first`/`last`/`rest` indexed args[0]
        // unchecked and panicked when called with none. The gc VM already
        // rejected all of these, with the wording pinned here.
        let test_case = [
            ("first()", "builtin first expected 1 argument, got 0"),
            ("first([1], [2])", "builtin first expected 1 argument, got 2"),
            ("last()", "builtin last expected 1 argument, got 0"),
            ("last([1], [2])", "builtin last expected 1 argument, got 2"),
            ("rest()", "builtin rest expected 1 argument, got 0"),
            ("rest([1], [2])", "builtin rest expected 1 argument, got 2"),
            ("push()", "builtin push expected 2 arguments, got 0"),
            ("push([1])", "builtin push expected 2 arguments, got 1"),
            ("push([1], 2, 3)", "builtin push expected 2 arguments, got 3"),
            ("len()", "builtin len expected 1 argument, got 0"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_hash_index_expressions() {
        let test_case = [
            (r#"{"foo": 5}["foo"]"#, "5"),
            (r#"{"foo": 5}["bar"]"#, "null"),
            (r#"let key = "foo"; {"foo": 5}[key]"#, "5"),
            (r#"{}["foo"]"#, "null"),
            (r#"{5: 5}[5]"#, "5"),
            (r#"{true: 5}[true]"#, "5"),
            (r#"{false: 5}[false]"#, "5"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_class_constructor_methods_and_dynamic_fields() {
        let test_case = [
            (
                r#"class Point {
  constructor(x, y) { this.x = x; this.y = y; }
  sum() { this.x + this.y; }
}
let point = new Point(20, 22);
point.sum();"#,
                "42",
            ),
            (
                r#"class Empty { value() { 42; } }
let empty = new Empty();
empty.value();"#,
                "42",
            ),
            (
                r#"class Value { value() { 1; } }
let value = new Value();
value.value = 42;
value.value;"#,
                "42",
            ),
            (
                r#"class Mutable { constructor(value) { this.value = value; } }
let value = new Mutable(1);
value.value = 42;
value.value;"#,
                "42",
            ),
            (
                r#"class Trace {
  constructor() { this.order = 0; }
  mark(value) { this.order = this.order * 10 + value; value; }
  target() { this.mark(1); this; }
}
class Pair { constructor(left, right) { this.value = left + right; } }
let trace = new Trace();
trace.target().value = trace.mark(2);
let pair = new Pair(trace.mark(3), trace.mark(4));
trace.order;"#,
                "1234",
            ),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_class_methods_capture_the_declared_class_binding() {
        let test_case = [(
            r#"class CapturedClass {
  make() { new CapturedClass(); }
  value() { 1; }
}
let old_instance = new CapturedClass();
class CapturedClass { value() { 2; } }
old_instance.make().value();"#,
            "1",
        )];
        apply_test(&test_case);
    }

    #[test]
    fn test_detached_method_and_lexical_this_capture() {
        let test_case = [
            (
                r#"class Counter {
  constructor(value) { this.value = value; }
  current() { this.value; }
}
let current = new Counter(42).current;
current();"#,
                "42",
            ),
            (
                r#"class Box {
  constructor(value) { this.value = value; }
  reader() { fn() { fn() { this.value; }; }; }
}
let reader = new Box(42).reader();
reader()();"#,
                "42",
            ),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_class_instance_and_bound_method_identity() {
        let test_case = [
            ("class A {} let Alias = A; A == Alias;", "true"),
            ("class A {} new A() == new A();", "false"),
            ("class A { method() { 1; } } let a = new A(); let f = a.method; f == f;", "true"),
            ("class A { method() { 1; } } let a = new A(); a.method == a.method;", "false"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn test_class_errors_and_strict_arity() {
        let test_case = [
            (
                "class Empty {} new Empty(1);",
                "wrong number of arguments for Empty.constructor: want=0, got=1",
            ),
            (
                "class A { method(value) { value; } } new A().method();",
                "wrong number of arguments for method: want=1, got=0",
            ),
            ("class A {} A();", "class A must be constructed with new"),
            ("let f = fn() {}; new f();", "cannot construct fn() {  }"),
            ("class A {} new A().missing;", "property 'missing' does not exist on A"),
            ("1.value;", "cannot read property 'value' of 1"),
            ("let f = fn(a) { a; }; f();", "wrong number of arguments: want=1, got=0"),
            ("class A { constructor() { return 1; } }", "constructor cannot return a value"),
        ];
        apply_test(&test_case);
    }

    #[test]
    fn class_cycle_display_is_opaque() {
        apply_test(&[(
            "class Node {} let node = new Node(); node.next = node; node;",
            "[object Node]",
        )]);
    }

    #[test]
    fn validation_accepts_bindings_from_previous_eval() {
        let env: Env = Rc::new(RefCell::new(Default::default()));
        eval(parse("let answer = 41;").unwrap(), &env).unwrap();

        let result = eval(parse("answer + 1;").unwrap(), &env).unwrap();
        assert_eq!(result.as_ref(), &object::Object::Integer(42));
    }
}