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 or any parent Scopes.
-
#[]=(k, v) ⇒ Object
Re-assign a variable in this Scope, or any parent Scope.
-
#define(k, v) ⇒ Object
Define a new variable.
-
#initialize(values = nil, parent = nil) ⇒ Scope
constructor
Create a new Scope with
values
as available variables. -
#push_scope(values = {}) ⇒ Scope
Create a new Scope with this Scope as its parent.
Constructor Details
#initialize(values = nil, 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 = nil, parent = nil) @values = values || Core.definitions @parent = parent end |
Instance Method Details
#[](k) ⇒ Object
Reference a variable in this Scope or any parent Scopes.
Raises a RuntimeError if the variable does not exist.
57 58 59 60 61 62 63 64 65 |
# File 'lib/iode/scope.rb', line 57 def [](k) if @values.key?(k) @values[k] elsif @parent @parent[k] else raise "Reference to undefined variable `#{k}`" end end |
#[]=(k, v) ⇒ Object
Re-assign a variable in this Scope, or any parent Scope.
Raises a RuntimeError if the variable does not exist.
79 80 81 82 83 84 85 86 87 |
# File 'lib/iode/scope.rb', line 79 def []=(k, v) if @values.key?(k) @values[k] = v elsif @parent @parent[k] = v else raise "Reference to undefined variable `#{k}`" end end |
#define(k, v) ⇒ Object
Define a new variable.
44 45 46 |
# File 'lib/iode/scope.rb', line 44 def define(k, v) @values[k] = v end |