update misc
[jackhill/mal.git] / skew / env.sk
1 class Env {
2 const _outer Env
3 var _data StringMap<MalVal> = {}
4
5 def new(outer Env) {
6 _outer = outer
7 }
8
9 def new(outer Env, binds List<MalVal>, exprs List<MalVal>) {
10 _outer = outer
11 for i in 0..binds.count {
12 const name = (binds[i] as MalSymbol).val
13 if name == "&" {
14 const restName = (binds[i + 1] as MalSymbol).val
15 _data[restName] = MalList.new(exprs.slice(i))
16 break
17 } else {
18 _data[name] = exprs[i]
19 }
20 }
21 }
22
23 def find(key MalSymbol) Env {
24 if key.val in _data { return self }
25 return _outer?.find(key)
26 }
27
28 def get(key MalSymbol) MalVal {
29 const env = find(key)
30 if env == null { throw MalError.new("'" + key.val + "' not found") }
31 return env._data[key.val]
32 }
33
34 def set(key MalSymbol, value MalVal) MalVal {
35 _data[key.val] = value
36 return value
37 }
38 }