Class: Yadriggy::TypeChecker::TypeEnv
- Inherits:
-
Object
- Object
- Yadriggy::TypeChecker::TypeEnv
- 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
Defined Under Namespace
Classes: BaseTypeEnv
Instance Method Summary collapse
-
#bind_name(name, type) ⇒ Type
Binds name to type.
-
#bound_name?(name) ⇒ Type|nil
Gets the type bound to ‘name`, or nil if `name` is not bound to any type.
-
#context ⇒ Module|nil
Gets context class (enclosing class) or nil.
-
#each {|name, type| ... } ⇒ Object
Executes ‘block` for each name in this environment.
-
#initialize(parent = nil) ⇒ TypeEnv
constructor
A new instance of TypeEnv.
-
#new_base_tenv(klass = nil) ⇒ Object
Makes a new type environment.
-
#new_tenv ⇒ Object
Makes a new type environment where all the bindings are copied from the current type environment.
Constructor Details
#initialize(parent = nil) ⇒ TypeEnv
Returns a new instance of TypeEnv.
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.
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.
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 |
#context ⇒ Module|nil
Gets context class (enclosing class) or nil.
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.
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.
50 51 52 |
# File 'lib/yadriggy/typecheck.rb', line 50 def new_base_tenv(klass=nil) BaseTypeEnv.new(klass.nil? ? context : klass) end |