Class: Twostroke::Runtime::Scope
- Inherits:
-
Object
- Object
- Twostroke::Runtime::Scope
- Defined in:
- lib/twostroke/runtime/scope.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #close ⇒ Object
- #declare(var) ⇒ Object
- #delete(var) ⇒ Object
- #get_var(var) ⇒ Object
- #global_scope ⇒ Object
- #has_var(var) ⇒ Object
-
#initialize(parent = nil) ⇒ Scope
constructor
A new instance of Scope.
- #set_var(var, value) ⇒ Object
Constructor Details
#initialize(parent = nil) ⇒ Scope
Returns a new instance of Scope.
5 6 7 8 |
# File 'lib/twostroke/runtime/scope.rb', line 5 def initialize(parent = nil) @locals = {} @parent = parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/twostroke/runtime/scope.rb', line 3 def parent @parent end |
Instance Method Details
#close ⇒ Object
42 43 44 |
# File 'lib/twostroke/runtime/scope.rb', line 42 def close Scope.new self end |
#declare(var) ⇒ Object
30 31 32 |
# File 'lib/twostroke/runtime/scope.rb', line 30 def declare(var) @locals[var] = Types::Undefined.new end |
#delete(var) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/twostroke/runtime/scope.rb', line 34 def delete(var) if has_var var @locals.delete var else parent.delete var end end |
#get_var(var) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/twostroke/runtime/scope.rb', line 10 def get_var(var) if @locals.has_key? var @locals[var] else @parent.get_var(var) end end |
#global_scope ⇒ Object
46 47 48 |
# File 'lib/twostroke/runtime/scope.rb', line 46 def global_scope @global_scope ||= parent.global_scope end |
#has_var(var) ⇒ Object
26 27 28 |
# File 'lib/twostroke/runtime/scope.rb', line 26 def has_var(var) @locals.has_key?(var) || parent.has_var(var) end |
#set_var(var, value) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/twostroke/runtime/scope.rb', line 18 def set_var(var, value) if @locals.has_key? var @locals[var] = value else @parent.set_var(var, value) end end |