~tim/scheme-vm

4b6cddbe8f80fdc484fa9b0beee75998c9d9390a — Tim Morgan 4 years ago 20d4cd7
Fix bug compiling an empty list
2 files changed, 18 insertions(+), 1 deletions(-)

M compiler.rb
M spec/compiler_spec.rb
M compiler.rb => compiler.rb +1 -1
@@ 78,7 78,7 @@ class Compiler
    sexp = sexp.to_ruby if sexp.is_a?(VM::Pair)
    return compile_literal(sexp, options) unless sexp.is_a?(Array)
    sexp.compact! # datum comments #;(...) come in as nil due to our parser :-(
    return [] if sexp.empty?
    return [] if sexp.empty? && !options[:quote]
    dispatch(sexp, options)
  end


M spec/compiler_spec.rb => spec/compiler_spec.rb +17 -0
@@ 362,6 362,23 @@ describe Compiler do
        end
      end

      context 'given an empty list' do
        before do
          @result = subject.compile(<<-END)
            (quote ())
          END
        end

        it 'compiles into vm instructions' do
          expect(d(@result)).to eq([
            'VM::PUSH_NUM', 0,
            'VM::PUSH_LIST',
            'VM::POP',
            'VM::HALT'
          ])
        end
      end

      context 'given an atom' do
        before do
          @result = subject.compile(<<-END)