Class: Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/support.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



6
7
8
# File 'lib/support.rb', line 6

def initialize
  @env = [{}]
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



4
5
6
# File 'lib/support.rb', line 4

def env
  @env
end

Instance Method Details

#add(id, val, depth = 0) ⇒ Object



14
15
16
17
# File 'lib/support.rb', line 14

def add(id, val, depth = 0)
  raise "Adding illegal identifier #{id.inspect}" unless Symbol === id
  @env[depth][id.to_s.sub(/^\*/, '').intern] = val
end

#allObject



47
48
49
# File 'lib/support.rb', line 47

def all
  @env.reverse.inject { |env, scope| env.merge scope }
end

#currentObject



43
44
45
# File 'lib/support.rb', line 43

def current
  @env.first
end

#depthObject



10
11
12
# File 'lib/support.rb', line 10

def depth
  @env.length
end

#extendObject



19
20
21
# File 'lib/support.rb', line 19

def extend
  @env.unshift({})
end

#lookup(id) ⇒ Object

Raises:

  • (NameError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/support.rb', line 27

def lookup(id)

  warn "#{id} is a string from #{caller[0]}" if String === id

  # HACK: if id is :self, cheat for now until we have full defn remapping
  if id == :self then
    return Type.fucked
  end

  @env.each do |closure|
    return closure[id] if closure.has_key? id
  end

  raise NameError, "Unbound var: #{id.inspect} in #{@env.inspect}"
end

#scopeObject



51
52
53
54
55
56
57
58
# File 'lib/support.rb', line 51

def scope
  self.extend
  begin
    yield
  ensure
    self.unextend
  end
end

#unextendObject



23
24
25
# File 'lib/support.rb', line 23

def unextend
  @env.shift
end