Class: Plasma::Interpreter::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/plasma/interpreter/plasma_grammarnode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = {}, default = nil) ⇒ Env

Returns a new instance of Env.



7
8
9
10
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 7

def initialize(init={}, default=nil)
  @state = [init]
  @default = default
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



5
6
7
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 5

def default
  @default
end

#stateObject

Returns the value of attribute state.



4
5
6
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 4

def state
  @state
end

Instance Method Details

#bind!(key, value) ⇒ Object



20
21
22
23
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 20

def bind!(key, value)
  @state.last[key] = value
  self
end

#merge!(hash) ⇒ Object



25
26
27
28
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 25

def merge!(hash)
  @state.last.merge!(hash)
  self
end

#release!Object



35
36
37
38
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 35

def release!()
  @state.pop
  self
end

#resolve(key) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 54

def resolve(key)
  @state.reverse_each do |layer|
    return layer[key] if layer.include?(key)
  end

  # nothing to find
  return default
end

#scope(inner = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 40

def scope(inner={})
  self.scope!(inner)

  begin
    value = yield self
    self.release!

    return value
  rescue => detail
    self.release!
    detail
  end
end

#scope!(inner = {}) ⇒ Object



30
31
32
33
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 30

def scope!(inner={})
  @state.push(inner)
  self
end

#to_plasmaObject



16
17
18
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 16

def to_plasma
  @state.to_plasma
end

#to_sObject



12
13
14
# File 'lib/plasma/interpreter/plasma_grammarnode.rb', line 12

def to_s
  @state.map {|s| s.inspect}.join("\n")
end