Class: Iode::Scope
- Inherits:
-
Object
- Object
- Iode::Scope
- Defined in:
- lib/iode/scope.rb
Overview
Lexical scope environment for iode.
Maintains a stack of execution contexts.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Reference a variable in this scope.
-
#[]=(k, v) ⇒ Object
Re-assign a variable in this scope.
-
#initialize(values = {}, parent = nil) ⇒ Scope
constructor
Create a new Scope with
valuesas available variables. -
#push_scope(values = {}) ⇒ Scope
Create a new Scope with this Scope as its parent.
Constructor Details
#initialize(values = {}, parent = nil) ⇒ Scope
Create a new Scope with values as available variables.
29 30 31 32 |
# File 'lib/iode/scope.rb', line 29 def initialize(values = {}, parent = nil) @values = values @parent = parent end |
Instance Method Details
#[](k) ⇒ Object
Reference a variable in this scope.
Raises a RuntimeError if the variable does not exist.
43 44 45 46 47 48 49 50 51 |
# File 'lib/iode/scope.rb', line 43 def [](k) if @values.key?(k) @values[k] elsif @parent @parent[k] else raise "Undefined variable `#{k}`" end end |
#[]=(k, v) ⇒ Object
Re-assign a variable in this scope.
If the variable does not exist, it will be initialized.
65 66 67 |
# File 'lib/iode/scope.rb', line 65 def []=(k, v) @values[k] = v end |
#push_scope(values = {}) ⇒ Scope
Create a new Scope with this Scope as its parent.
The new Scope will have access to all variables in this Scope, but assignments will only mask variables, not replace them.
79 80 81 |
# File 'lib/iode/scope.rb', line 79 def push_scope(values = {}) Scope.new(values, self) end |