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, BaseTypeEnv

Defined Under Namespace

Classes: BaseTypeEnv

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.



25
26
27
28
# File 'lib/yadriggy/typecheck.rb', line 25

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.



59
60
61
# File 'lib/yadriggy/typecheck.rb', line 59

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`.



68
69
70
71
72
73
74
75
# File 'lib/yadriggy/typecheck.rb', line 68

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.



80
81
82
# File 'lib/yadriggy/typecheck.rb', line 80

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`.



34
35
36
# File 'lib/yadriggy/typecheck.rb', line 34

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.



50
51
52
# File 'lib/yadriggy/typecheck.rb', line 50

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.



41
42
43
# File 'lib/yadriggy/typecheck.rb', line 41

def new_tenv
  TypeEnv.new(self)
end