~tim/scheme-vm

scheme-vm/loader.rb -rw-r--r-- 727 bytes View raw
                                                                                
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
require 'pathname'
require_relative './vm'

class Loader
  ROOT_PATH = VM::ROOT_PATH
  LOAD_PATH = [File.join(ROOT_PATH, 'lib')].freeze

  def initialize(filename, relative_to: nil)
    @filename = filename
    @relative_to = relative_to
  end

  attr_reader :filename, :relative_to, :path, :code

  def load
    @path = find_path
    raise "File #{filename} not found in load path #{LOAD_PATH.join(';')}" unless @path
    @code = File.read(@path)
  end

  private

  def find_path
    if filename.start_with?('.') && relative_to
      File.expand_path(File.join(File.dirname(relative_to), filename))
    else
      LOAD_PATH.map { |p| File.expand_path(File.join(p, filename)) }.detect { |p| File.exist?(p) }
    end
  end
end