forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.rb
37 lines (34 loc) · 771 Bytes
/
env.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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