state.rb 1.35 KB
Newer Older
1 2 3 4 5 6 7 8 9
# frozen_string_literal: true

module Inkoc
  class State
    attr_reader :config

    # Any diagnostics that were produced when compiling modules.
    attr_reader :diagnostics

10
    # The modules that have been compiled.
11 12 13 14 15
    attr_reader :modules

    # The database storing all type information.
    attr_reader :typedb

16 17 18 19
    # A cache containing relative module paths and their corresponding absolute
    # paths.
    attr_reader :module_paths_cache

20 21 22 23
    def initialize(config)
      @config = config
      @diagnostics = Diagnostics.new
      @modules = {}
24
      @typedb = TypeSystem::Database.new
25
      @module_paths_cache = ModulePathsCache.new(config)
26
    end
27

28
    def module_exists?(name)
29 30 31
      @modules.key?(name)
    end

32
    def module(name)
33
      @modules[name.to_s]
34 35
    end

36 37 38 39
    def store_module(mod)
      @modules[mod.name.to_s] = mod
    end

40 41 42 43
    def type_of_module_global(mod_name, global_name)
      self.module(mod_name)&.lookup_global(global_name)
    end

44 45
    def diagnostics?
      @diagnostics.any?
46 47
    end

48 49 50 51
    def display_diagnostics
      formatter = Formatter::Pretty.new
      output = formatter.format(@diagnostics)

52
      STDERR.puts(output) unless output.empty?
53 54 55
    end

    def find_module_path(path)
56
      @module_paths_cache.absolute_path_for(path)
57
    end
58 59 60 61

    def inspect
      "Inkoc::State(modules: [#{modules.keys.join(', ')}])"
    end
62 63
  end
end