Class: Environment
- Inherits:
-
Object
- Object
- Environment
- Defined in:
- lib/support.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
Instance Method Summary collapse
- #add(id, val, depth = 0) ⇒ Object
- #all ⇒ Object
- #current ⇒ Object
- #depth ⇒ Object
- #extend ⇒ Object
-
#initialize ⇒ Environment
constructor
A new instance of Environment.
- #lookup(id) ⇒ Object
- #scope ⇒ Object
- #unextend ⇒ Object
Constructor Details
#initialize ⇒ Environment
Returns a new instance of Environment.
6 7 8 |
# File 'lib/support.rb', line 6 def initialize @env = [{}] end |
Instance Attribute Details
#env ⇒ Object
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 |
#all ⇒ Object
47 48 49 |
# File 'lib/support.rb', line 47 def all @env.reverse.inject { |env, scope| env.merge scope } end |
#current ⇒ Object
43 44 45 |
# File 'lib/support.rb', line 43 def current @env.first end |
#depth ⇒ Object
10 11 12 |
# File 'lib/support.rb', line 10 def depth @env.length end |
#extend ⇒ Object
19 20 21 |
# File 'lib/support.rb', line 19 def extend @env.unshift({}) end |
#lookup(id) ⇒ Object
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 |
#scope ⇒ Object
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 |
#unextend ⇒ Object
23 24 25 |
# File 'lib/support.rb', line 23 def unextend @env.shift end |