Class: R2CEnvironment

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

Constant Summary collapse

TYPE =
0
VALUE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/r2cenvironment.rb', line 8

def env
  @env
end

Instance Method Details

#_get(name) ⇒ Object

Raises:

  • (NameError)


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

def _get(name)
  @env.each do |closure|
    return closure[name] if closure.has_key? name
  end

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

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

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/r2cenvironment.rb', line 10

def add(id, type, depth = 0)
  raise "Adding illegal identifier #{id.inspect}" unless
    Symbol === id
  raise ArgumentError, "type must be a valid Type instance" unless
    Type === type

  @env[depth][id.to_s.sub(/^\*/, '').intern][TYPE] = type
end

#depthObject



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

def depth
  @env.length
end

#extendObject

override



24
25
26
# File 'lib/r2cenvironment.rb', line 24

def extend # override
  @env.unshift(Hash.new { |h,k| h[k] = [] })
end

#get_val(name) ⇒ Object



28
29
30
# File 'lib/r2cenvironment.rb', line 28

def get_val(name)
  self._get(name)[VALUE]
end

#lookup(name) ⇒ Object



32
33
34
35
36
37
# File 'lib/r2cenvironment.rb', line 32

def lookup(name)
  # HACK: if name is :self, cheat for now until we have full defn remapping
  return Type.fucked if name == :self

  return self._get(name)[TYPE]
end

#old_extendObject



23
# File 'lib/r2cenvironment.rb', line 23

alias :old_extend :extend

#scopeObject



43
44
45
46
47
48
49
50
# File 'lib/r2cenvironment.rb', line 43

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

#set_val(name, val) ⇒ Object



39
40
41
# File 'lib/r2cenvironment.rb', line 39

def set_val(name, val)
  self._get(name)[VALUE] = val
end