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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
require_relative "../test_helper"

require "lox/interpreter"
require "lox/parser"
require "lox/resolver"
require "lox/scanner"

class TestInterpreter < Lox::Test
  def setup
    @scanner = Lox::Scanner.new
  end

  # def test_literal
  #   assert_evaluated(42.0, "42")
  # end

  def test_grouping
    assert_evaluated "42", "(42)"
  end

  def test_unary
    assert_evaluated "-42", "-42"
    assert_evaluated "false", "!42"
    assert_evaluated "false", "!true"
    assert_evaluated "true", "!false"
    assert_evaluated "true", "!nil"
  end

  def test_binary
    assert_evaluated "42", "100 - 58"
    assert_evaluated "42", "84 / 2"
    assert_evaluated "42", "21 * 2"

    # precedence
    assert_evaluated "42", "2 * 25 - 8"

    assert_evaluated "42", "40 + 2"
    assert_evaluated "42", "\"4\" + \"2\""

    assert_evaluated "true", "1 > 0"
    assert_evaluated "false", "0 > 0"
    assert_evaluated "false", "0 > 1"

    assert_evaluated "true", "1 >= 0"
    assert_evaluated "true", "0 >= 0"
    assert_evaluated "false", "0 >= 1"

    assert_evaluated "false", "1 < 0"
    assert_evaluated "false", "0 < 0"
    assert_evaluated "true", "0 < 1"

    assert_evaluated "false", "1 <= 0"
    assert_evaluated "true", "0 <= 0"
    assert_evaluated "true", "0 <= 1"

    assert_evaluated "true", "0 != 1"
    assert_evaluated "false", "0 != 0"
    assert_evaluated "false", "nil != nil"
    assert_evaluated "true", "nil != 1"

    assert_evaluated "false", "0 == 1"
    assert_evaluated "true", "0 == 0"
    assert_evaluated "true", "nil == nil"
    assert_evaluated "false", "nil == 1"
  end

  def test_errors
    [
      "-true",
      "12 > true",
      "true < 23",
      "false * 23",
      "false + 23",
    ].each do |src|
      assert_raises Lox::RuntimeError do
        assert_evaluated nil, src
      end
    end
  end

  def test_stringify
    assert_evaluated "nil", "nil"
    assert_evaluated "42", "42"
    assert_evaluated "42.1", "42.1"
    assert_evaluated "foo", "\"foo\""
  end

  def test_multiple_statements
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      one
      true
      3
    EXPECTED
      print "one";
      print true;
      print 2 + 1;
    SRC
  end

  def test_environment
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      3
    EXPECTED
      var a = 1;
      var b = 2;
      print a + b;
    SRC
  end

  def test_assignment
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      2
    EXPECTED
      var a = 1;
      print a = 2;
    SRC
  end

  def test_block
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      inner a
      outer b
      global c
      outer a
      outer b
      global c
      global a
      global b
      global c
    EXPECTED
      var a = "global a";
      var b = "global b";
      var c = "global c";
      {
        var a = "outer a";
        var b = "outer b";
        {
          var a = "inner a";
          print a;
          print b;
          print c;
        }
        print a;
        print b;
        print c;
      }
      print a;
      print b;
      print c;
    SRC
  end

  def test_uninitialized_vars_are_nil
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      nil
    EXPECTED
      var a;
      print a;
    SRC
  end

  def test_if
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      true
      false
    EXPECTED
      if (true)
        print "true";
      else
        print "false";

      if (false)
        print "true";
      else
        print "false";
    SRC
  end

  def test_logical
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      hi
      yes
    EXPECTED
      print "hi" or 2; // "hi".
      print nil or "yes"; // "yes".
    SRC
  end

  def test_while
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      0
      1
      2
    EXPECTED
      var a = 0;
      while (a < 3) {
        print a;
        a = a + 1;
      }
    SRC
  end

  def test_for
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      0
      1
      1
      2
      3
      5
      8
      13
      21
      34
    EXPECTED
      var a = 0;
      var temp;

      for (var b = 1; a < 50; b = temp + b) {
        print a;
        temp = a;
        a = b;
      }
    SRC
  end

  def test_function
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      Hi, Dear Reader!
    EXPECTED
      fun sayHi(first, last) {
        print "Hi, " + first + " " + last + "!";
      }

      sayHi("Dear", "Reader");
    SRC

    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      0
      1
      2
    EXPECTED
      {
        var i = 0; while (i < 3) { { print i; } i = i + 1; }
      }
    SRC

    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      0
      1
      2
    EXPECTED
      for (var i = 0; i < 3; i = i + 1) print i;
    SRC

    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      0
      1
      1
      2
      3
      5
      8
    EXPECTED
      fun fib(n) {
        if (n <= 1) return n;
        return fib(n - 2) + fib(n - 1);
      }

      for (var i = 0; i < 7; i = i + 1) {
        print fib(i);
      }
    SRC
  end

  def test_local_functions_and_closures
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      1
      2
    EXPECTED
      fun makeCounter() {
        var i = 0;
        fun count() {
          i = i + 1;
          print i;
        }

        return count;
      }

      var counter = makeCounter();
      counter(); // "1".
      counter(); // "2".
    SRC
  end

  def test_resolver
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      1
    EXPECTED
      var foo = 1;
      {
        print foo;
        var foo = 2;
      }
    SRC
  end

  def test_class
    assert_interpreted "", <<~SRC
      class Breakfast {
        cook() {
          print "Eggs a-fryin'!";
        }

        serve(who) {
          print "Enjoy your breakfast, " + who + ".";
        }
      }
    SRC

    assert_interpreted "DevonshireCream", <<~SRC
      class DevonshireCream {
        serveOn() {
          return "Scones";
        }
      }

      print DevonshireCream; // Prints "DevonshireCream".
    SRC

    assert_interpreted "Bagel instance", <<~SRC
      class Bagel {}
      var bagel = Bagel();
      print bagel; // Prints "Bagel instance".
    SRC
  end

  def test_set
    assert_interpreted "bar", <<~SRC
      class Bagel {}
      var bagel = Bagel();
      bagel.foo = "bar";
      print bagel.foo;
    SRC
  end

  def test_class_methods
    assert_interpreted "Crunch crunch crunch!", <<~SRC
      class Bacon {
        eat() {
          print "Crunch crunch crunch!";
        }
      }

      Bacon().eat(); // Prints "Crunch crunch crunch!".
    SRC

    assert_interpreted "The German chocolate cake is delicious!", <<~SRC
      class Cake {
        taste() {
          var adjective = "delicious";
          print "The " + this.flavor + " cake is " + adjective + "!";
        }
      }

      var cake = Cake();
      cake.flavor = "German chocolate";
      cake.taste(); // Prints "The German chocolate cake is delicious!".
    SRC

    assert_interpreted "Thing instance", <<~SRC
      class Thing {
        getCallback() {
          fun localFunction() {
            print this;
          }

          return localFunction;
        }
      }

      var callback = Thing().getCallback();
      callback();
    SRC

    assert_interpreted "Jane", <<~SRC
      class Person {
        sayName() {
          print this.name;
        }
      }

      var jane = Person();
      jane.name = "Jane";

      var bill = Person();
      bill.name = "Bill";

      bill.sayName = jane.sayName;
      bill.sayName(); // "Jane"
    SRC
  end

  def test_invalid_this
    assert_raises Lox::ResolverError, "Can't use 'this' outside of a class." do
      interpret("print this;")
    end

    assert_raises Lox::ResolverError, "Can't use 'this' outside of a class." do
      interpret(<<~SRC)
        fun notAMethod() {
          print this;
        }
      SRC
    end
  end

  def test_init
    assert_interpreted <<~EXPECTED.chomp, <<~SRC
      Foo instance
      Foo instance
      Foo instance
    EXPECTED
      class Foo {
        init() {
          print this;
        }
      }

      var foo = Foo();
      print foo.init();
    SRC

    assert_raises Lox::ResolverError do
      interpret(<<~SRC)
        class Foo {
          init() {
            return "something else";
          }
        }
      SRC
    end

    assert_interpreted "", <<~SRC
      class Foo {
        init() {
          return;
        }
      }
    SRC
  end

  def test_inheritance
    assert_interpreted "", <<~SRC
      class Doughnut {}
      class BostonCream < Doughnut {}
    SRC

    assert_raises Lox::ResolverError do
      interpret("class Oops < Oops {}")
    end

    assert_raises Lox::RuntimeError do
      interpret(<<~SRC)
        var NotAClass = "I am totally not a class";

        class Subclass < NotAClass {} // ?!
      SRC
    end
  end

  def test_inheriting_methods
    assert_interpreted "Fry until golden brown.", <<~SRC
      class Doughnut {
        cook() {
          print "Fry until golden brown.";
        }
      }

      class BostonCream < Doughnut {}

      BostonCream().cook();
    SRC
  end

  def test_calling_superclass_methods
    assert_interpreted <<~OUT.chomp, <<~SRC
      Fry until golden brown.
      Pipe full of custard and coat with chocolate.
    OUT
      class Doughnut {
        cook() {
          print "Fry until golden brown.";
        }
      }

      class BostonCream < Doughnut {
        cook() {
          super.cook();
          print "Pipe full of custard and coat with chocolate.";
        }
      }

      BostonCream().cook();
    SRC

    assert_interpreted "A method", <<~SRC
      class A {
        method() {
          print "A method";
        }
      }

      class B < A {
        method() {
          print "B method";
        }

        test() {
          super.method();
        }
      }

      class C < B {}

      C().test();
    SRC
  end

  def test_invalid_super
    assert_raises Lox::ResolverError do
      interpret(<<~SRC)
        class Eclair {
          cook() {
            super.cook();
            print "Pipe full of crème pâtissière.";
          }
        }
      SRC
    end

    assert_raises Lox::ResolverError do
      interpret("super.notEvenInAClass();")
    end
  end

  private

  def assert_interpreted(expected, src)
    output = interpret(src)
    assert_equal expected, output
  end

  def assert_evaluated(expected, src)
    assert_interpreted(expected, "print #{src};")
  end

  def with_stdout
    original = $stdout
    $stdout = StringIO.new

    yield

    output = $stdout.string.chomp
    $stdout = original
    output
  end

  def interpret(src)
    with_stdout {
      stmts = Lox::Parser.new(@scanner.scan(src)).parse!
      interpreter = Lox::Interpreter.new

      resolver = Lox::Resolver.new(interpreter);
      resolver.resolve(*stmts);

      interpreter.interpret(stmts)
    }
  end

end