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
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 ifname
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.
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.
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.
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 |
#context ⇒ Module|nil
Gets context class (enclosing class) or nil.
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.
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.
53 54 55 |
# File 'lib/yadriggy/typecheck.rb', line 53 def new_base_tenv(klass=nil) BaseTypeEnv.new(klass.nil? ? context : klass) end |