~tim/scheme-vm

114daadd500388de4cb0f0682c953db978f72a3d — Tim Morgan 5 years ago 04b66f9
Speed up specs by caching program
4 files changed, 32 insertions(+), 6 deletions(-)

M compiler.rb
M program.rb
M spec/lib_spec.rb
A spec/support/dumpable_string_io.rb
M compiler.rb => compiler.rb +2 -1
@@ 32,7 32,8 @@ class Compiler
    @sexps += Parser.new(code, filename: filename).parse if code
  end

  attr_reader :variables, :filename, :arguments, :syntax, :source, :libs
  attr_reader :variables, :arguments, :syntax, :source, :libs
  attr_accessor :filename

  def compile(code = nil, keep_last: false, halt: true)
    @sexps = Parser.new(code, filename: filename).parse if code

M program.rb => program.rb +9 -2
@@ 9,8 9,8 @@ class Program
    @compiler = Compiler.new(code, filename: filename)
  end

  def run(debug: 0)
    @instr = @compiler.compile
  def run(code: nil, debug: 0)
    @instr = @compiler.compile(code)
    VM::PrettyPrinter.new(@instr).print if debug >= 1
    vm.execute(@instr, debug: debug)
    vm.return_value


@@ 19,6 19,13 @@ class Program
    1
  end

  def filename=(f)
    @filename = f
    @compiler.filename = f
  end

  private

  def vm
    @vm ||= VM.new(stdout: @stdout, args: @args)
  end

M spec/lib_spec.rb => spec/lib_spec.rb +12 -3
@@ 1,15 1,24 @@
require_relative './spec_helper'
require_relative './support/dumpable_string_io'
require 'stringio'

out = DumpableStringIO.new

program = Program.new('(import (scheme base) (assert))', stdout: out)
program.run
cached_program = Marshal.dump(program)

Dir[File.expand_path('../lib/**/*.scm', __FILE__)].each do |path|
  describe File.split(path).last do
    code = File.read(path)
    code = File.read(path).sub(/\(import \(scheme base\)\s+\(assert\)\s*\)/, '')
    focus = !(code =~ /^; focus/).nil?
    skip = !(code =~ /^; skip/).nil?
    it 'passes all tests', focus: focus, skip: skip do
      failed = false
      out = StringIO.new
      Program.new(code, filename: path, stdout: out).run
      out.rewind
      program = Marshal.load(cached_program)
      program.filename = path
      program.run(code: code)
      out.rewind
      result = out.read
      if result != ''

A spec/support/dumpable_string_io.rb => spec/support/dumpable_string_io.rb +9 -0
@@ 0,0 1,9 @@
# we don't actually care to dump or load this
class DumpableStringIO < StringIO
  def marshal_dump
    []
  end

  def marshal_load(_data)
  end
end