1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-13 01:43:50 +03:00
mal/ruby/env.rb
2014-04-10 22:24:58 -05:00

38 lines
771 B
Ruby

class Env
attr_accessor :data
def initialize(outer=nil, binds=[], exprs=[])
@data = {}
@outer = outer
binds.each_index do |i|
if binds[i] == :"&"
data[binds[i+1]] = exprs.drop(i)
break
else
data[binds[i]] = exprs[i]
end
end
return self
end
def find(key)
if @data.key? key
return self
elsif @outer
return @outer.find(key)
else
return nil
end
end
def set(key, value)
@data[key] = value
return value
end
def get(key)
env = find(key)
raise "'" + key.to_s + "' not found" if not env
env.data[key]
end
end