Class: Yadriggy::TypeChecker::TypeEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/yadriggy/typecheck.rb

Overview

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

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

Direct Known Subclasses

FreeVarFinder

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ TypeEnv

Returns a new instance of TypeEnv.

Parameters:

  • parent (TypeEnv|nil) (defaults to: nil)

    the parent environment.



28
29
30
31
# File 'lib/yadriggy/typecheck.rb', line 28

def initialize(parent=nil)
  @parent = parent
  @names = {}
end

Instance Method Details

#bind_name(name, type) ⇒ Type

Binds name to type.

Parameters:

  • name (Name|String|Symbol|nil)

    the name.

  • type (Type)

    the type.

Returns:

  • (Type)

    the type.



62
63
64
# File 'lib/yadriggy/typecheck.rb', line 62

def bind_name(name, type)
  @names[name.to_sym] = type unless name.nil?
end

#bound_name?(name) ⇒ Type|nil

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

Parameters:

  • name (Name|String|Symbol)

    the name.

Returns:

  • (Type|nil)

    the type bound to name.



71
72
73
74
75
76
77
78
# 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

#contextModule|nil

Gets context class (enclosing class) or nil.

Returns:

  • (Module|nil)

    the context class.



83
84
85
# File 'lib/yadriggy/typecheck.rb', line 83

def context
  @parent&.context
end

#each {|name, type| ... } ⇒ Object

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

Yields:

  • (name, type)

    gives a Symbol (name) and a Type.



37
38
39
# File 'lib/yadriggy/typecheck.rb', line 37

def each(&block)
  @names.each(&block)
end

#new_base_tenv(klass = nil) ⇒ Object

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

Parameters:

  • klass (Module) (defaults to: nil)

    the context class. If it is nil, then self's context class is passed.



53
54
55
# File 'lib/yadriggy/typecheck.rb', line 53

def new_base_tenv(klass=nil)
  BaseTypeEnv.new(klass.nil? ? context : klass)
end

#new_tenvObject

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



44
45
46
# File 'lib/yadriggy/typecheck.rb', line 44

def new_tenv
  TypeEnv.new(self)
end