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
@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
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
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
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
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
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
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