Class: Fusion::Interpreter::Env
- Inherits:
-
Object
- Object
- Fusion::Interpreter::Env
- Defined in:
- lib/fusion/interpreter/env.rb
Defined Under Namespace
Classes: DuplicateBinding
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#bind(name, value) ⇒ Object
Insert a pattern binding, rejecting a duplicate binder.
- #child ⇒ Object
-
#define(name, value) ⇒ Object
Unchecked insert, for interpreter-internal names (__dir__, built-ins, …).
-
#initialize(parent = nil) ⇒ Env
constructor
A new instance of Env.
- #lookup(name) ⇒ Object
Constructor Details
#initialize(parent = nil) ⇒ Env
Returns a new instance of Env.
24 25 26 27 |
# File 'lib/fusion/interpreter/env.rb', line 24 def initialize(parent = nil) @vars = {} @parent = parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
22 23 24 |
# File 'lib/fusion/interpreter/env.rb', line 22 def parent @parent end |
Instance Method Details
#bind(name, value) ⇒ Object
Insert a pattern binding, rejecting a duplicate binder. Only this env’s own scope is checked: a binder may shadow a name from a parent env, but must be unique within one pattern/clause.
38 39 40 41 42 |
# File 'lib/fusion/interpreter/env.rb', line 38 def bind(name, value) raise DuplicateBinding, name if @vars.key?(name) @vars[name] = value end |
#child ⇒ Object
54 55 56 |
# File 'lib/fusion/interpreter/env.rb', line 54 def child Env.new(self) end |
#define(name, value) ⇒ Object
Unchecked insert, for interpreter-internal names (__dir__, built-ins, …).
30 31 32 33 |
# File 'lib/fusion/interpreter/env.rb', line 30 def define(name, value) @vars[name] = value self end |
#lookup(name) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/fusion/interpreter/env.rb', line 44 def lookup(name) if @vars.key?(name) @vars[name] elsif @parent @parent.lookup(name) else :__unbound__ end end |