~tim/scheme-vm

f23168670146fb6810d3293d5539579fe261db4d — Tim Morgan 6 years ago ff6e359
Add boolean? predicate
3 files changed, 56 insertions(+), 0 deletions(-)

A lib/bool.scm
A spec/lib/bool_spec.rb
M vm.rb
A lib/bool.scm => lib/bool.scm +7 -0
@@ 0,0 1,7 @@
(define boolean?
  (lambda (b)
    (if (eq? b #t)
        #t
        (if (eq? b #f)
            #t
            #f))))

A spec/lib/bool_spec.rb => spec/lib/bool_spec.rb +48 -0
@@ 0,0 1,48 @@
require_relative '../spec_helper'
require 'stringio'

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

  let(:subject) { Program.new(code, stdout: stdout) }

  before { subject.run }

  describe 'boolean?' do
    context 'given #t' do
      let(:code) do
        <<-END
          (boolean? #t)
        END
      end

      it 'returns #t' do
        expect(subject.vm.pop_val).to eq(VM::BoolTrue.instance)
      end
    end

    context 'given #f' do
      let(:code) do
        <<-END
          (boolean? #f)
        END
      end

      it 'returns #t' do
        expect(subject.vm.pop_val).to eq(VM::BoolTrue.instance)
      end
    end

    context 'given a list' do
      let(:code) do
        <<-END
          (boolean? (list))
        END
      end

      it 'returns #f' do
        expect(subject.vm.pop_val).to eq(VM::BoolFalse.instance)
      end
    end
  end
end

M vm.rb => vm.rb +1 -0
@@ 396,6 396,7 @@ class VM
  LIBRARIES = %w(
    list
    pair
    bool
  )

  def lib_sexps(lib)