Class: Twostroke::Runtime::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/twostroke/runtime/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#parentObject (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

#closeObject



38
39
40
# File 'lib/twostroke/runtime/scope.rb', line 38

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
# File 'lib/twostroke/runtime/scope.rb', line 34

def delete(var)
  parent.delete var
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_scopeObject



42
43
44
# File 'lib/twostroke/runtime/scope.rb', line 42

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