Class: Yadriggy::TypeChecker::FreeVarFinder

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

Overview

A type environement that collects free variables. #bound_name? records the given symbol as a free variable name when it obtains the type of that symbol from its parent type environment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TypeEnv

#bind_name, #context, #each, #new_base_tenv, #new_tenv

Constructor Details

#initialize(parent) ⇒ FreeVarFinder

Returns a new instance of FreeVarFinder.



110
111
112
113
# File 'lib/yadriggy/typecheck.rb', line 110

def initialize(parent)
  super
  @free_variables = {}
end

Instance Attribute Details

#free_variablesHash<Symbol,Type> (readonly)

Obtains collected free variables.

Returns:

  • (Hash<Symbol,Type>)

    a map from variable names to their types.



108
109
110
# File 'lib/yadriggy/typecheck.rb', line 108

def free_variables
  @free_variables
end

Instance Method Details

#bound_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
# File 'lib/yadriggy/typecheck.rb', line 115

def bound_name?(name)
  type = @names[name.to_sym]
  if type.nil?
    t = @parent&.bound_name?(name)
    @free_variables[name.to_sym] = t unless t.nil?
    t
  else
    type
  end
end