class Yadriggy::TypeChecker::TypeEnv

TypeEnv (type environment) holds bindings between names and types.

If you define a subclass of {TypeEnv}, override {#new_tenv} and {#new_base_tenv}. {TypeChecker#make_base_env} has to be overridden as well.

Public Class Methods

new(parent=nil) click to toggle source

@param [TypeEnv|nil] parent the parent environment.

# File lib/yadriggy/typecheck.rb, line 28
def initialize(parent=nil)
  @parent = parent
  @names = {}
end

Public Instance Methods

bind_name(name, type) click to toggle source

Binds name to type.

@param [Name|String|Symbol|nil] name the name. @param [Type] type the type. @return [Type] the type.

# File lib/yadriggy/typecheck.rb, line 62
def bind_name(name, type)
  @names[name.to_sym] = type unless name.nil?
end
bound_name?(name) click to toggle source

Gets the type bound to `name`, or nil if `name` is not bound to any type.

@param [Name|String|Symbol] name the name. @return [Type|nil] the type bound to `name`.

# File lib/yadriggy/typecheck.rb, line 71
def bound_name?(name)
  type = @names[name.to_sym]
  if type.nil?
    @parent&.bound_name?(name)
  else
    type
  end
end
context() click to toggle source

Gets context class (enclosing class) or nil.

@return [Module|nil] the context class.

# File lib/yadriggy/typecheck.rb, line 83
def context
  @parent&.context
end
each(&block) click to toggle source

Executes `block` for each name in this environment. It passes the name-type pair as parameters to the block.

@yield [name, type] gives a `Symbol` (name) and a `Type`.

# File lib/yadriggy/typecheck.rb, line 37
def each(&block)
  @names.each(&block)
end
new_base_tenv(klass=nil) click to toggle source

Makes a new type environment. klass is set to the context class of that new environment.

@param [Module] klass the context class. If it is `nil`, then

`self`'s context class is passed.
# File lib/yadriggy/typecheck.rb, line 53
def new_base_tenv(klass=nil)
  BaseTypeEnv.new(klass.nil? ? context : klass)
end
new_tenv() click to toggle source

Makes a new type environment where all the bindings are copied from the current type environment.

# File lib/yadriggy/typecheck.rb, line 44
def new_tenv
  TypeEnv.new(self)
end