~tim/scheme-vm

ref: c5110102ce2df2d78a0c87f293edf20f5a3984e7 scheme-vm/spec/program_spec.rb -rw-r--r-- 6.9 KiB
c5110102Tim Morgan Add more char functions 3 years ago
                                                                                
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
require_relative './spec_helper'
require 'stringio'

describe Program do
  let(:stdout) { StringIO.new }

  describe '#run' do
    subject do
      described_class.new(
        code,
        filename: __FILE__,
        stdout: stdout
      )
    end

    let(:import_exit) { '(import (only (scheme process-context) exit)) ' }

    context 'when the program successfully finishes' do
      let(:code) { '(import (only (scheme base) +)) (+ 1 1)' }

      it 'returns 0' do
        expect(subject.run).to eq(0)
      end
    end

    context 'when the program writes a string' do
      let(:code) do
        <<-END
          (import (only (scheme base) write-string))
          (write-string 1)
        END
      end

      it 'writes to stdout' do
        subject.run
        stdout.rewind
        expect(stdout.read).to eq('1')
      end
    end

    context 'when the program imports other libraries' do
      let(:code) do
        <<-END
          (define-library (base)
            (export
             define
             define-syntax
             if
             write-string)

            (begin
              (--define-native apply base_apply)
              (--define-native car base_car)
              (--define-native cdr base_cdr)
              (--define-native define base_define)
              (--define-native define-syntax base_define_syntax)
              (--define-native empty? base_null?)
              (--define-native if base_if)
              (--define-native lambda base_lambda)

              (define-syntax begin
                (syntax-rules ()
                  ((begin exp ...)
                  ((lambda () exp ...)))))

              (define not
                (lambda (condition)
                  (if condition
                      #f
                      #t)))

              (--define-native write write) ; don't export this

              (define (newline)
                (write #\\newline))

              (define (write-string . args)
                (if (not (empty? args))
                    (begin
                      (write (car args))
                      (apply write-string (cdr args)))))
            ))

          (define-library (my-lib 1)
            (import (base))
            (begin
              (define foo "foo")
              (define-syntax macro1
                (syntax-rules ()
                  ((macro1) (write-string "from my-lib 1")))))
            (export foo macro1))

          (define-library (my-lib 2)
            (import (base))
            (import (my-lib 1))
            (begin
              (define baz "baz")
              (define-syntax macro2
                (syntax-rules ()
                  ((macro2) (macro1)))))
            (export foo baz macro2 (rename foo bar)))

          (import (my-lib 2))

          (macro2)
        END
      end

      it 'allows one library to reference another one' do
        subject.run
        stdout.rewind
        expect(stdout.read).to eq('from my-lib 1')
      end
    end

    context 'when the program fails' do
      let(:code) { 'foo' }

      it 'returns 1' do
        expect(subject.run).to eq(1)
      end
    end

    describe 'exit' do
      context 'when the program calls (exit)' do
        let(:code) { import_exit + '(exit)' }

        it 'returns 0' do
          expect(subject.run).to eq(0)
        end
      end

      context 'when the program calls (exit #t)' do
        let(:code) { import_exit + '(exit #t)' }

        it 'returns 0' do
          expect(subject.run).to eq(0)
        end
      end

      context 'when the program calls (exit 10)' do
        let(:code) { import_exit + '(exit 10)' }

        it 'returns 10' do
          expect(subject.run).to eq(10)
        end
      end

      context 'when the program calls (exit #f)' do
        let(:code) { import_exit + '(exit #f)' }

        it 'returns 1' do
          expect(subject.run).to eq(1)
        end
      end
    end

    context 'when the stack overflows' do
      let(:code) do
        "(import (only (scheme base) define if < + -))\n" \
        "(define (fib n)\n" \
        "  (if (< n 2)\n" \
        "      n\n" \
        "      (+\n" \
        "        (fib (- n 1))\n" \
        "        (fib (- n 2)))))\n" \
        '(fib 3)'
      end

      it 'sets the exit code to 2' do
        expect(subject.run).to eq(2)
      end

      it 'returns 2 and prints the stack' do
        stub_const('VM::MAX_CALL_DEPTH', 3)
        subject.run
        stdout.rewind
        expect(stdout.read).to eq(
          "Error: call stack too deep\n" \
          "#{__FILE__}#6\n" \
          "          (fib (- n 1))\n" \
          "           ^\n" \
          "#{__FILE__}#6\n" \
          "          (fib (- n 1))\n" \
          "           ^\n" \
          "#{__FILE__}#8\n" \
          "  (fib 3)\n" \
          "   ^\n"
        )
      end
    end

    context 'when an undefined variable is referenced' do
      let(:code) do
        "; undefined variable\n" \
        '(foo)'
      end

      it 'sets the exit code to 1' do
        expect(subject.run).to eq(1)
      end

      it 'shows the filename, line and column of the error' do
        subject.run
        stdout.rewind
        expect(stdout.read).to eq(
          "Error: foo is not defined\n\n" \
            "#{__FILE__}#2\n\n" \
            "  ; undefined variable\n" \
            "  (foo)\n" \
            "   ^ foo is not defined\n"
        )
      end
    end

    context 'when an undefined variable is referenced on line 1 with weird spacing' do
      let(:code) do
        ' ( foo)'
      end

      it 'shows the cursor at the proper position' do
        subject.run
        stdout.rewind
        expect(stdout.read).to eq(
          "Error: foo is not defined\n\n" \
            "#{__FILE__}#1\n\n" \
            "   ( foo)\n" \
            "     ^ foo is not defined\n"
        )
      end
    end

    context 'exception in macro in another file' do
      let(:code) do
        '(include "./fixtures/bad-macro") ' \
        '(bad-macro)'
      end

      it 'shows the filename, line and column number' do
        subject.run
        stdout.rewind
        expect(stdout.read).to eq(
          "Error: foo is not defined\n\n" \
            "./fixtures/bad-macro.scm#5\n\n" \
            "    (syntax-rules ()\n" \
            "      ((bad-macro) (foo))))\n" \
            "                    ^ foo is not defined\n"
        )
      end
    end

    context 'when a syntax error occurs' do
      let(:code) do
        "(foo)\n" \
        "\n" \
        '      (()'
      end

      it 'sets the exit code to 3' do
        expect(subject.run).to eq(3)
      end

      it 'shows the filename, line and column of the error' do
        subject.run
        stdout.rewind
        expected = ["\"", "#;", "#|", "'", "(", ")", ",", ",@", ";", "[ \t\n]", "[^() \t\n[]{}|\"]", "`", "|"]
        expect(stdout.read).to eq(
          "Syntax Error:\n\n" \
            "#{__FILE__}#3\n\n" \
            "  \n" \
            "        (()\n" \
            "           ^ expected one of: #{expected.inspect}\n"
        )
      end
    end
  end
end